Timus 1104. Don’t Ask Woman about Her Age

来源:互联网 发布:芬兰内战 知乎 编辑:程序博客网 时间:2024/06/17 05:57
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 numberk satisfying the following condition: the given number, written in k-based system is divisible byk−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
A1A
outout
22

这道题关键就是这句话了:the given number, written in k-based system is divisible byk−1.
由于我英语太水,导致走了好多弯路。
这个k-based的意思是k进制。
题的意思就是给你一个字符串,然后求这样一个数(2 ≤ k ≤ 36),它减1能被字符串所表达的数整除。就是这样的。

解决题需要知道两个知识(数论):
1.如果这个数(k进制数)能整除k-1,则这个数每位加起来也能整除k-1。
啥意思呢,以我们熟悉的十进制为例:
k=10,则为10进制数,99能整除k-1(即9),则9+9也能整除k-1(即9);
                               18能整除k-1(即9),则1+8也能整除k-1(即9);
举一个不是10进制的例子:
k=8,则为8进制数,115(10进制时为77)能整除k-1(即7),则1+1+5也能整除k-1(即7)
这个定理不知道是谁发现的。。。
2.k一定大于字符串中最大的数字。这很好理解,10进制数,10一定大于10进制数中每一位数。

代码:
#include <iostream>#include <string.h>#include <stdio.h>using namespace std;char str[1000005];int main(){    while(gets(str))    {        int l=strlen(str);        int minbase=0;        int sum=0;        for(int i=0;i<l;i++)        {            if(str[i]>='0'&&str[i]<='9')                {                    sum+=str[i]-'0';                    if(str[i]-'0'>minbase)                        minbase=str[i]-'0';                }            else                {                    sum+=str[i]-'A'+10;                    if(str[i]-'A'+10>minbase)                        minbase=str[i]-'A'+10;                }        }        minbase++;        if(minbase<=2)            cout<<2<<endl;        else        {            int i;            for(i=minbase;i<=36;i++)            {                if(sum%(i-1)==0)                {                    cout<<i<<endl;break;                }            }            if(i==36+1)            cout<<"No solution."<<endl;        }    }    return 0;}

加油!

0 0
原创粉丝点击