Atcoder Base -2 Number

C - Base -2 Number


Time limit : 2sec / Memory limit : 1024MB

Score : 300 points

Problem Statement

Given an integer N, find the base −2 representation of N.

Here, S is the base −2 representation of N when the following are all satisfied:

  • S is a string consisting of 0 and 1.
  • Unless S0, the initial character of S is 1.
  • Let S=SkSk−1…S0, then S0×(−2)0+S1×(−2)1+…+Sk×(−2)k=N.

It can be proved that, for any integer M, the base −2 representation of M is uniquely determined.

Constraints

  • Every value in input is integer.
  • −109≤N≤109

Input

Input is given from Standard Input in the following format:

N

Output

Print the base −2 representation of N.


Sample Input 1

-9

Sample Output 1

1011

As (−2)0+(−2)1+(−2)3=1+(−2)+(−8)=−9, 1011 is the base −2 representation of −9.


Sample Input 2

123456789

Sample Output 2

11000101011001101110100010101

Sample Input 3

0

Sample Output 3

0

 

 

这题不算是一个难题,但是这道题的思路很有意思,所以我想来分享一下

这题就和我朋友赛后讨论了一下,就存在两种方法,所以我想这题一定有非常多的解法

这题我的思想是求前缀和

 

正数的时候 前缀和1,2^2,2^4,2^6,……形成序列v

找到 v[i]<n<=v[j] (j>i)

最高位标为1,然后从最高位枚举,看条件s=v[j];s-pow(2,x)>=n 是否满足 满足的话如果当前是奇数位则是1,偶数位是0

反之则反一下

 

 

负数的时候 前缀和2^1,2^3,2^5,……形成序列v

找到 v[i]<|n|<=v[j] (j>i)

最高位标为1,然后从最高位枚举,看条件s=v[j];s-pow(2,x)>=-n 是否满足 满足的话如果当前是奇数位则是0,偶数位是1

反之则反一下

 

1就是0,0就是1,让我想到了犬夜叉里桔梗说的 干净就是脏,脏就是干净,善就是恶,恶就是善,生就是死,死就是生

呵呵,扯远了,上代码

 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
typedef long long LL;
LL v[40]={1,2},n;
int main() {
    for(int i=2;i<=32;i+=2)
        v[i]=v[i-2]+LL(pow(2,i));
    for(int i=3;i<=32;i+=2)
        v[i]=v[i-2]+LL(pow(2,i));
    scanf("%lld",&n);
    if(n==0)
        printf("0");
    else if(n>0){
        int m=0;
        for(int i=2;i<=32;i+=2){
            if(n>v[i-2]&&v[i]>=n){
                m=i;
                break;
            }
        }
        LL sum=v[m];
        printf("1");
        for(int i=m-1;i>=0;i--){
            if(sum-LL(pow(2, i))>=n){
                sum-=LL(pow(2, i));
                if(i%2==0)
                    printf("0");
                else
                    printf("1");
            }
            else{
                if(i%2==0)
                    printf("1");
                else
                    printf("0");
            }
        }
    }
    else{
        int m=0;
        n=-n;
        for(int i=3;i<=32;i+=2){
            if(n>v[i-2]&&v[i]>=n){
                m=i;
                break;
            }
        }
        LL sum=v[m];
        printf("1");
        for(int i=m-1;i>=0;i--){
            if(sum-LL(pow(2, i))>=n){
                sum-=LL(pow(2, i));
                if(i%2==1)
                    printf("0");
                else
                    printf("1");
            }
            else{
                if(i%2==1)
                    printf("1");
                else
                    printf("0");
            }
        }
    }
    printf("\n");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值