您的位置:首页 > 其它

POJ 1152 An Easy Problem

2009-09-28 12:51 483 查看
An Easy Problem!

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 6264Accepted: 1429
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

Source
uva 10093

/* http://acm.pku.edu.cn/JudgeOnline/problem?id=1152 数论知识
给定一个N进制数R(ri, ri-1, ri-1, ... , r2, r1, r0)
则 R = ri * (N ^ i) + ri-1 * (N ^ i-1) + ri-2 * (N ^ i - 2) + ... + r2 * (N ^ 2) + r1 * (N ^ 1) + r0 * (N ^ 0)
= ri * {[(N - 1) + 1] ^ i} + ri-1 * {[(N - 1) + 1] ^ i-1} + ri-2 * {[(N - 1) + 1] ^ i - 2} + ... + r2 * {[(N - 1) + 1] * ^ 2} + r1 * {[(N - 1) + 1] ^ 1} + r0 * {[(N - 1) + 1] ^ 0}
所以 R mod (N - 1) = (ri + ri-1 + ri-2 + ... + r2 + r1 + r0) mod (N - 1)
so, the rest of the work is really easy...
不过要注意输入为单个0的情况,由于N必须>=2, 所以0应该输出2
*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
int p, maxNum;
string input;
char ch;
while(cin>>input)
{
int total = 0, curNum;
maxNum = INT_MIN;
for(p = 0; p < input.length(); p++)
{
ch = input[p];
if(ch >= '0' && ch <= '9')  curNum = int(ch - '0');
else if(ch >= 'A' && ch <= 'Z') curNum = (ch - 'A') + 10;
else curNum = (ch - 'a') + 36;
if(curNum > maxNum) maxNum = curNum;
total += curNum;
}
if(maxNum == 0) maxNum = 1;
for(p = maxNum; p <= 61; p++)
{
if(total % p == 0)
{
cout<<p + 1;
break;
}
}
if(p > 61)
cout<<"such number is impossible!";
cout<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: