電腦學院大學生程式設計競賽(2015’12)Bitwise Equations

來源:互聯網
上載者:User
Bitwise Equations Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 633    Accepted Submission(s): 335


Problem Description You are given two positive integers X and K. Return the K-th smallest positive integer Y, for which the following equation holds: X + Y =X | Y
Where '|' denotes the bitwise OR operator.  
Input The first line of the input contains an integer T (T <= 100) which means the number of test cases. 
For each case, there are two integers X and K (1 <= X, K <= 2000000000) in one line.  
Output For each case, output one line containing the number Y.  
Sample Input
 3 5 1 5 5 2000000000 2000000000   

Sample Output
 2 18 16383165351936   

總是望著曾經的空間發獃,那些說好不分開的朋友不在了,轉身,陌路。 熟悉的,安靜了, 安靜的,離開了, 離開的,陌生了, 陌生的,消失了, 消失的,陌路了。

#include <string.h>#include <iostream>using namespace std;long long rets=0;class jisuan{public:    long long kthPlusOrSolution(int, int);    string getBin(int);};string jisuan::getBin(int x){    string ret="";    if(x==0)    {        ret="0";        return ret;    }    while(x>0)    {        ret=char(x%2+'0')+ret;        x/=2;    }    return ret;}long long jisuan::kthPlusOrSolution(int x, int k){    string strx, strk;    strx=getBin(x);    strk=getBin(k);    int lx=strx.length()-1;    int lk=strk.length()-1;    string ret="";    while(lx>=0 && lk>=0)    {        if(strx[lx]=='1')        {            ret="0"+ret;            --lx;        }        else        {            ret=strk[lk]+ret;            --lx;            --lk;        }    }    while(lk>=0)        ret=strk[lk--]+ret;    for(int i=0;i<(int)ret.length();i++)        rets=(rets<<1)+ret[i]-'0';    return rets;}int main(){    int n,x,k;    jisuan bit;    cin>>n;    while(n--)    {        rets=0;        cin>>x>>k;        bit.kthPlusOrSolution(x,k);        cout<<rets<<endl;    }}

@執念  "@☆但求“”安★ 下次我們做的一定會更好。。。。
為什 麼這次的題目是英文的。。。。QAQ...

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.