1104. Don’t Ask Woman about Her Age


1104. Don’t Ask Woman about Her Age
Time Limit: 1.0 second
Memory Limit: 16 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
input output
A1A
22
1つの数xを与えて、数を求めるのはk(2<=k<=36)進数でx%(k-1)=0を満たすかどうか.このようなkがある場合はkを出力、そうでない場合は「No solution.」を出力する.
アルゴリズム分析:この問題は簡単な問題で、考察の進数表現問題です.


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
using namespace std;
const int maxn=1000010;
char s[maxn];
int getValue(char c)
{
    if(isdigit(c))  return c-'0';
    return c-'A'+10;
}
int main()
{
    gets(s);
    int len=strlen(s);
    int m=0;
    for(int i=0;i<len;i++)
    {
        m=max(m,getValue(s[i]));
    }
    for(int k=max(2,m+1);k<=36;k++)
    {
        int sum=0;
        for(int i=len-1;i>=0;i--)
        {
            sum=(sum*k+getValue(s[i]))%(k-1);
        }
        if(sum==0)  {printf("%d
",k);return 0;} } printf("No solution.
"); return 0; }