-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy path1010_Radix (25).cpp
130 lines (113 loc) · 2.42 KB
/
1010_Radix (25).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
//第七组样例超时
//1、需要二分答案....
//2、结果用long long
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <vector>
#include <cstdlib>
using namespace std;
long long val['z'+1];
long long rBaseToDec(long long p[],long long len, long long radix)
{
long long m = 1;
long long num = 1;
long long sum = 0;
for(long long i=len-1;i>=0;i--)
{
sum += p[i]*m;
m *= radix;
}
return sum;
}
void Init()
{
long long i;
for(i = '0'; i <= '9'; i++)
{
val[i] = i-'0';
}
for(i = 'a'; i <= 'z'; i++)
{
val[i] = i-'a'+10;
}
}
int compare(long long p[],long long len,long long radix ,long long target)
{
long long m = 1;
long long num = 1;
long long sum = 0;
for(long long i = len-1; i >= 0; i--)
{
sum += p[i]*m;
m *= radix;
if(sum > target) //avoid overflow
return 1;
}
if(sum > target)
return 1;
else if(sum < target)
return -1;
else
return 0;
}
long long binarySearch(long long p[],long long n,long long low,long long high,long long key)
{
long long mid = low;
long long tmp;
while(low <= high)
{
tmp = compare(p, n, mid, key);
if(tmp > 0)
{
high = mid - 1;
}
else if(tmp < 0)
{
low = mid +1;
}
else return mid;
mid = (low + high) >> 1;
}
return -1;
}
int main()
{
string n1,n2;
long long N1[15],N2[15],lenN1,lenN2;
long long i,tag,radix;
Init();
while(cin >> n1 >> n2 >> tag >> radix)
{
if(tag == 2)
{
string tmp = n1;
n1 = n2;
n2 = tmp;
}
for(i = 0; i < n1.size(); i++)
{
N1[i] = val[n1[i]];
}
lenN1 = n1.size();
long long max = 0;
for(i = 0; i < n2.size(); i++)
{
N2[i] = val[n2[i]];
if(N2[i]+1 > max)
max = N2[i]+1;
}
lenN2 = n2.size();
long long defVal = rBaseToDec(N1, lenN1, radix);
long long low = max;
long long high = (defVal+1 > max+1) ? defVal+1 : max+1;
long long res = binarySearch(N2, lenN2, low, high, defVal);
if(res == -1)
cout<< "Impossible" <<endl;
else
cout<< res <<endl;
}
return 0;
}