Question: Calculate the expression of the number based on-2.
Analysis: number theory. You can find the rule by writing the number intervals that can be expressed by different digits.
1: []; 2: [-2,-1]; 3: [];
It is observed that the interval length increases to 1, 2, 4, 8,..., 2 ^ K, and the parity interval is enabled;
In this way, we can find the corresponding location of 1 in order, and find the next location every time minus the corresponding base (-2 ^ K.
Note: It is also number theory.
#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;long long l[40],r[40],b[40];int ans[40];int main(){l[0] = 1; r[0] = 1; b[0] = 1;l[1] = -2;r[1] = -1;b[1] = -2;long long base = 4;for (int i = 2 ; i < 33 ; ++ i) {if (i%2) {r[i] = l[i-2]-1;l[i] = l[i-2]-base;}else {l[i] = r[i-2]+1;r[i] = r[i-2]+base;}base <<= 1;b[i] = b[i-1]*-2;}int n,m,s,e;while (~scanf("%d",&n))for (int i = 1 ; i <= n ; ++ i) {scanf("%d",&m);printf("Case #%d: ",i);for (int i = 0 ; i < 33 ; ++ i)ans[i] = 0;s = 32;while (s >= 0) {if (m >= l[s] && m <= r[s]) {ans[s] = 1;m -= b[s];}s --;}e = 32;while (e > 0 && !ans[e]) e --;while (e >= 0) printf("%d",ans[e --]);printf("\n");}return 0;}
Ultraviolet A 11121-base-2