Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9273 | Accepted: 2378 |
Description
Have you heard the fact "The base of every normal number system is 10" ? Of course, I am not talking about number systems like Stern Brockot Number System. This problem has nothing to do with this fact but may have some similarity.
You will be given an N based integer number R and you are given the guaranty that R is divisible by (N-1). You will have to print the smallest possible value for N. The range for N is 2 <= N <= 62 and the digit symbols for 62 based number is (0..9 and A..Z and a..z). Similarly, the digit symbols for 61 based number system is (0..9 and A..Z and a..y) and so on.
Input
Each line in the input will contain an integer (as defined in mathematics) number of any integer base (2..62). You will have to determine what is the smallest possible base of that number for the given conditions. No invalid number will be given as input. The largest size of the input file will be 32KB.
Output
If number with such condition is not possible output the line "such number is impossible!" For each line of input there will be only a single line of output. The output will always be in decimal number system.
Sample Input
3
5
A
Sample Output
4
6
11
问题链接:POJ1152 UVA10093 An Easy Problem!
问题简述:
给定一个数R,其最大的进制为62进制,要找一个最小的进制N,使得数R若为N进制数满足(n-1)|R(n-1能够整除R)。如果找不到N则输出 "such number is impossible!"。
问题分析:
直接进行进制转换的话,计算比较复杂,需要先做一番数学推导。
考虑四位的62进制数R,设其各位数字为abcd。那么所求的N,满足:
(a*N^3+b*N^2+c*N+d)%(N-1) = 0
=> (a*N^3%(N-1)+b*N^2%(N-1)+c*N%(N-1)+d)%(N-1) = 0
=> (a*N%(N-1)*N%(N-1)*N%(N-1)
+b*N%(N-1)*N%(N-1)
+c*N%(N-1)
+d)%(N-1) = 0
其中 N%(N-1) = 1
得 (a+b+c+d)%(N-1) = 0
最后问题转换为各位数字之和能够被N-1整除。上述推导过程中,使用的模除运算的定律,(a*b)%N=((a%N)*(b%N))%N和(a+b)%N=(a%N+b%N)%N。这两个有个模除的定律是可以证明的。
实际解决该问题时还需要使用枚举法进制查找,求得N
程序说明:
程序中的26行的语句continue是必要的,否则在UVA10093中会WA,需要考虑输入中有空格或正负符号字符的存在等。POJ1152则该语句可有可无。
其他都是套路。
题记:(略)
参考链接:(略)
AC的C++语言程序如下:
/* POJ1152 UVA10093 An Easy Problem! */
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
const int N = 62;
int main()
{
string s;
while(cin >> s) {
int d, sumd = 0, maxd = -1, mind;
for(int i=0; s[i]; i++) {
if(isdigit(s[i]))
d = s[i] - '0';
else if(isupper(s[i]))
d = s[i] - 'A' + 10;
else if(islower(s[i]))
d = s[i] - 'a' + 10 + 26;
else
continue;
sumd += d;
maxd = max(maxd, d);
}
if(maxd == 0)
cout << 2 << endl;
else {
mind = N + 1;
for(int i=maxd; i<=N; i++)
if(sumd % i == 0) {
mind = i + 1;
break;
}
if(mind <= N)
cout << mind << endl;
else
cout << "such number is impossible!\n";
}
}
return 0;
}