SRM DIV 430문제에서....시간 많이 남으시는 분~~~~봐주세요~~~

  • UNKI
    UNKI

    제가 디버깅 해야 하는데....
    -_-; 여기가 군대다 보니...이클립스 받아서 실행하는데 시간적인 제약이 너무 크네요....
    시간 남으시는 분들 이것좀.....
    500-Point문제 입니다.
    간단하게 2진수로 입력 들어온 x랑 k를.... x(2)를 x의 2진수라고 할때
    x(2)의 각 자리중 1인 자리를 제외하고 0인 자리에 k(2)를 채워넣는 문제입니다. 길이에 비해 해법이 참 쉬운 문제...
    그런데...
    아래 주석달린걸로 원래 트라이 했었는데 안되더라구요;;
    결국 다른분 소스 보고 고쳐서 결국 submit하긴 했는데....
    결국 500점짜리 150점 크리 ㄳㄳ
    원래 짰던 코드가 뭐가 문제인지 제 머리로는 잘 모르겠습니다..ㅠㅠ 맞게 짠거 같은데....
    지금 또 근무를 들어가야 할 시간이라서...ㅡ.ㅡ;; 여기서 아레나 한번 들어갈려면 항상 jre를 깔아야 되는 열악한 환경이라서요.
    혹시 시간 남으시는 분들 오류좀 찾아주십시오~~~~~~~~
    아무도 없으시면 그냥 제가 하겠습니다 크크.....
    문제는 가장 아래에 첨부합니다.
    public class BitwiseEquations {
    public long kthPlusOrSolution(int x, int k) {
    long ret = 0;
    int j = 0;
    for (int i = 0 ; k >> j != 0; i++) {
    if (((x >> i) & 1) != 1) {
    if(((k >> j++) & 1) == 1)
    ret |= 1L << i;
    }
    }
    return ret;
    }
    }
    /**
    public class BitwiseEquations {
    public long kthPlusOrSolution(int x, int k) {
    long ret = 0;
    int j = 0;
    for (int i = 0 ; i < 64; i++) {
    if (((x >> i) & 1) != 1) {
    ret |= ((k >> j++) & 1) << i;
    }
    }
    return ret;
    }
    }
    */

    Problem Statement

         You are given two positive integers x and k. Return the k-th smallest positive integer y (where k is 1-based) for which the following equation holds:
    x + y = x | y          
    where '|' denotes the bitwise OR operator.

    Definition

        
    Class: BitwiseEquations
    Method: kthPlusOrSolution
    Parameters: int, int
    Returns: long
    Method signature: long kthPlusOrSolution(int x, int k)
    (be sure your method is public)

    Constraints

    • x will be between 1 and 2,000,000,000, inclusive.

    • k will be between 1 and 2,000,000,000, inclusive.

    Examples

    0)

    5                      
    1                      

    Returns: 2

    The first positive integer for which the equation holds is 2. You can check that 5+2=7 as well as 5|2=7. Both plus and bitwise OR look like the following:
     101
    + 10
     ---
     111                      

    1)

    5                      
    5                      

    Returns: 18

    The fifth number for which the equation 5 + y = 5 | y holds is 18. The first four solutions are 2,8,10,16. The binary sum for 18 looks like the following:
       101
    +10010
     -----
     10111                      

    2)

    10                      
    3                      

    Returns: 5

    The third solution is 5. The first two solutions are 1 and 4.

    3)

    1                      
    1000000000                      

    Returns: 2000000000

    This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

    [이 글은 과거 홈페이지에서 이전된 글입니다. 원문보기]

    15년 전
    3개의 댓글이 있습니다.
    • Kureyo
      Kureyo

      integer를 32비트 이상 시프트를 시켜서 문제가 되는게 아닐까하는데요,
      ret |= ((k >> j++) & 1) << i; 을 ret |= ((k >> j++) & 1) L<< i; 로 고치면 될거같습니다...
      (해보진않았습니다 ㅠㅠ)


      15년 전 link
    • UNKI
      UNKI

      그렇군요!!!!
      결국 L만 붙이면 컴파일에러가 떨어져서
      ret |= (((long)k >> j++) & 1) << i;
      으로 고치니까 되네요!!
      고맙습니다~~~


      15년 전 link
    • Being
      Being

      L 붙여서 쓰는 건 상수에는 가능하죠 ㅎㅎ 0xFFFFFFFFFFFFFFFFL 이런 식으로.. 일반적으로는 캐스팅을 하셔야 해요 : )


      15년 전 link
    • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.