Link: poj 1152 an easy problem!
Evaluate the number r of an n-base to ensure that R can be divisible by (N-1) with the smallest n.
The first reaction was violence. The size of N ranges from 0 to 62. It is found that data overflows when n is converted into 10. Here there is a division, that is, (N-1) modulo is 0.
Example: a1a2a3 represents an N-base number R and converts it to a 10-digit number:
(A1 * n + A2 * n + A3) % (N-1) = (A1 * n) % (N-1) + (A2 * n) % (N-1) + (A3) % (N-1) % (N-1) = (A1 + A2 + A3) % (N-1 );
This prevents data overflow.
AC code:
#include<stdio.h>#include<string.h>#define ll __int64#include<map>using namespace std;map<char,ll> mm;int main(){ll max,ans;ll n,i,len;char s[100]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";char ss[50000+10];mm.clear();for(i=0;i<strlen(s);i++){mm[s[i]]=i;//printf("(%c %d)\n",s[i],mm[s[i]]);}while(scanf("%s",ss)!=EOF){len=strlen(ss);ans=0;max=2;for(i=0;i<len;i++){if(max<mm[ss[i]])max=mm[ss[i]];ans+=mm[ss[i]];}for(i=max+1;i<63;i++){if(ans%(i-1)==0)break;}if(i>=63)printf("such number is impossible!\n");elseprintf("%d\n",i);}return 0;}