ural 1104. Don’t Ask Woman about Her Age

1104. Don’t Ask Woman about Her Age

Time limit: 1.0 second
Memory limit: 64 MB
Mrs Little likes digits most of all. Every year she tries to make the best number of the year. She tries to become more and more intelligent and every year studies a new digit. And the number she makes is written in numeric system which base equals to her age. To make her life more beautiful she writes only numbers that are divisible by her age minus one. Mrs Little wants to hold her age in secret.
You are given a number consisting of digits 0, …, 9 and Latin letters A, …, Z, where A equals 10, B equals 11 etc. Your task is to find the minimal number k satisfying the following condition: the given number, written in k-based system is divisible by k−1.

Input

Input consists of one string containing no more than 106 digits or uppercase Latin letters.

Output

Output the only number k, or "No solution." if for all 2 ≤ k ≤ 36 condition written above can't be satisfied. By the way, you should write your answer in decimal system.

Sample

inputoutput
A1A
22

题意:

给你一个未知进制表示的数(小于等于36进制,长度最长100W),求一个进制d,使得d进制的这个数可以被d-1整除.比如样例,22进制的A1A可以整除21.

后来btw说他的方法了,因为这个数等于 b*x+a,所以它模(b-1)就是(x+a)%b

思路:

因为最多只有100位,所以,直接暴力也可以过的

第一步:将字符串翻译成数字的形式,存储在一个数组中;

第二步;从第一位开始到最后一位进行整除测试tem存储本位的余数,tem=tem*d+number[i],然后用tem对d求余在存储到tem中;

AC代码:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<string>
 7 #include<cmath>
 8 
 9 using namespace std;
10 const int MAX = 1000010;
11 int number[MAX],len;
12 string str;
13 int ma=0;
14 
15 
16 int sks(char a)
17 {
18     if(a>='A')
19         return (int)(a-'A'+10);
20     else
21         return (int)(a-'0');
22 }
23 int sod()
24 {
25     ma=0;
26     for(int i=0;i<str.length();i++){
27         number[i]=sks(str[i]);
28         if(ma<number[i])
29             ma=number[i];
30     }
31     return 0;
32 }
33 bool song(int a,int b)
34 {
35     int tem=0;
36     for(int i=0;i<str.length();i++){
37         tem*=a;
38         tem+=number[i];
39         tem%=b;
40     }
41     return tem;
42 }
43 
44 
45 int main()
46 {
47 //    freopen("1.txt","r",stdin);
48     cin>>str;
49     sod();
50     if(ma<=1){
51         cout<<2<<endl;
52         return 0;
53     }
54     for(int i=ma+1;i<=36;i++){
55         if(!song(i,i-1)){
56             cout<<i<<endl;
57             return 0;
58         }
59     }
60     cout<<"No solution."<<endl;
61     return 0;
62 }
View Code
 
原文地址:https://www.cnblogs.com/zhangchengbing/p/3357621.html