https://vjudge.net/contest/218366#problem/A
中间溢出,注意求模。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cstdlib> 6 #include<cmath> 7 #include<vector> 8 #include<stack> 9 #include<queue> 10 #define lson l, m, rt<<1 11 #define rson m+1, r, rt<<1|1 12 #define IO ios::sync_with_stdio(false);cin.tie(0); 13 #define INF 0x3f3f3f3f 14 #define MAXN 100010 15 const int MOD=1e9+7; 16 typedef __int64 ll; 17 using namespace std; 18 char s[MAXN]; 19 ll a[MAXN],maxm; 20 int main() 21 { 22 IO; 23 while(cin >> s){ 24 maxm = -INF ; 25 ll len = strlen(s); 26 for(int i = 0; i < len; i++){ 27 if(s[i] >= '0'&&s[i] <= '9'){ 28 a[i] = s[i]-'0'; 29 maxm = max(maxm, a[i]); 30 } 31 else{ 32 a[i] = s[i]-'A'+10; 33 maxm = max(maxm, a[i]); 34 } 35 } 36 ll flag=0, loc, r, sum; 37 ll beg = max((ll)2, maxm)+1;//起点必须加一 38 for(int i = beg; i <= 36; i++){ 39 r=1;sum=0; 40 for(int j = 0; j < len; j++){//中间溢出,求模的技巧 41 sum = (sum+a[len-1-j]*r%(i-1))%(i-1); 42 r = r*i%(i-1); 43 } 44 if(sum==0){ 45 flag=1;loc=i; 46 break; 47 } 48 } 49 if(!flag) cout << "No Solution" << endl; 50 else cout << loc << endl; 51 } 52 return 0; 53 }