`

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

题目大意:给定一个数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\n",k);return 0;}
    }
    printf("No solution.\n");
    return 0;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics