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
一开始看到是比较蒙的,实在不懂去看了别人的题解,才发现很简单。
题意:给一个N进制的数R,求最小的N使得这个数%(n-1)==0;进制范围是 2<=N<=62 分别用0–9,A–Z,a—z表示。超出范围输出“such number is impossible!”。
举一个简单的例子:输入1A3B5 ///运算中要将每一个字符转换成相应的数字。
则要求的就是
(1*n*n*n*n+A*n*n*n+3*n*n+B*n+5)%(n-1) == 0;
找到最小的n使这个式子成立。根据%符号的运算性质。可以把式子转换成
(1*n*n*n*n%(n-1) n-1+A*n*n*n%(n-1) +3*n*n+B*n%(n-1) +5%(n-1) )%(n-1) == 0;
进一步
(1*n%(n-1) *n%(n-1) *n%(n-1) *n%(n-1) n-1+A*n%(n-1) *n%(n-1) *n%(n-1) +3*n%(n-1) *n%(n-1) +B*n%(n-1) +5%(n-1) )%(n-1) == 0;
可知n%(n-1) == 1;
所以可以把上式演变为(1+A+3+B+5)%(n-1)==0 ///其实这里可以写成(1+A+3+B+5%(n-1))%(n-1) ==0 ;但根据最终结果来看5%(n-1)是不影响最后结果的,而且写的时候也要少些步骤,所以才去这种写法;
然后能推出一个公式就是sum%(n-1)==0 ,sum为每一位数字转换成10进制加出来的和,枚举n就可以找到相应的进制数的值。
#include"stdio.h"
#include"iostream"
#include"algorithm"
#include"string.h"
#include"math.h"
#include"stdlib.h"
#include"queue"
using namespace std;
int main(void)
{
char s[30010];
while(~scanf("%s",s))
{
int i;
int len = strlen(s);
int minjz = -1; ///找到的进制数必须大于这个数里面表示出来的最小进制数
int sum = 0;
for(i = 0;i < len;i++)
{
if(isupper(s[i]))
s[i] = s[i]-'A'+10; ///A为第11个数,但是数值为10
else if(islower(s[i]))
s[i] = s[i]-'a'+36; ///a为第37个数,但是数值为36
else
s[i] = s[i]-'0'; ///0到9十个数
if(minjz < s[i]) minjz = s[i];
sum+=s[i];
}
for(i = minjz;i <= 62;i++)
if(sum%i == 0)
break;
if(i > 61)
printf("such number is impossible!\n");
else
printf("%d\n",i+1);
}
return 0;
}