-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoj1208.cpp
146 lines (140 loc) · 2.59 KB
/
oj1208.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <string.h>
struct bigInteger{
int digit[1000];
int size;
void init(){
for(int i=0;i<1000;i++)
digit[i]=0;
size=0;
}
void input(char a[]){
init();
int len=strlen(a);
for(int i=len-1,w=1,f=0,tmp=0;i>=0;i--){
tmp+=(a[i]-'0')*w;
w*=10;
f++;
if(f>3||i==0){
digit[size++]=tmp;
tmp=0;
w=1;
f=0;
}
}
return;
}
void set(int x){
init();
do{
digit[size++]=x%10000;
x/=10000;
}while(x);
return;
}
bigInteger operator + (const bigInteger &A) const{
int carry=0;
bigInteger ret;
ret.init();
for(int i=0;i<size||i<A.size;i++){
ret.digit[i]=digit[i]+A.digit[i]+carry;
carry=ret.digit[i]/10000;
ret.digit[ret.size++]%=10000;
}
if(carry)
ret.digit[ret.size++]=carry;
return ret;
}
bigInteger operator * (int a) const{
int carry=0;
bigInteger ret;
ret.init();
for(int i=0;i<size;i++){
ret.digit[i]=digit[i]*a+carry;
carry=ret.digit[i]/10000;
ret.digit[ret.size++]%=10000;
}
if(carry)
ret.digit[ret.size++]=carry;
return ret;
}
bigInteger operator / (int a) const{
int remainder=0;
bigInteger ret;
ret.init();
for(int i=size;i>=0;i--){
ret.digit[i]=(digit[i]+remainder*10000)/a;
remainder=(digit[i]+remainder*10000)%a;
}
for(int i=0;i<size;i++)
if(ret.digit[i])
ret.size=i;
ret.size++;
return ret;
}
int operator % (int a) const{
int remainder=0;
for(int i=size;i>=0;i--){
int t=(digit[i]+remainder*10000)/a;
remainder=(digit[i]+remainder*10000)%a;
}
return remainder;
}
void display(){
printf("%d",digit[size-1]);
for(int i=size-2;i>=0;i--)
printf("%04d",digit[i]);
printf("\n");
return;
}
} a;
struct bigBin{
int digit[10001];
int size;
void init(){
for(int i=0;i<10001;i++)
digit[i]=0;
size=0;
return;
}
} b;
bigBin DtoB(bigInteger a){
bigBin ret;
ret.init();
do{
ret.digit[ret.size++]=a%2;
a=a/2;
}while(a.size!=1||a.digit[0]!=0);
return ret;
}
bigBin reversion(bigBin a){
bigBin b;
b.init();
b.size=a.size;
for(int i=0;i<a.size;i++){
b.digit[i]=a.digit[a.size-i-1];
}
while(b.digit[b.size-1]==0) b.size--;
return b;
}
bigInteger BtoD(bigBin x){
bigInteger a,b;
a.set(0);
b.set(1);
for(int i=0;i<x.size;i++){
a=a+(b*x.digit[i]);
b=b*2;
}
return a;
}
char str[1001];
int main(){
while(scanf("%s",str)!=EOF){
a.input(str);
b=DtoB(a);
b=reversion(b);
a=BtoD(b);
a.display();
}
return 0;
}