正文

练手题2008-04-27 20:33:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lingdlz/34651.html

分享到:

Don't ask woman about her age
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Problem description
Mrs Little likes digits most of all. Every year she tries to make the best number of the year. She tryes 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 file should contain the only number k, or "No solution." if for all 2 ≤ k ≤ 36 condition written above can't be satisfyed. By the way, you should write your answer in decimal system.

Sample Input
A1A
Sample Output
22

 

///////////// 

原理: ( A*B*C*D)%K = (A%K)*(B%K)*(C%K)*(D%K)

注意细节问题

/////////

#include <stdio.h>
#include <string.h>
#include <limits.h>
char str[1000010];

int main(){
    int t,i,j,k,sum,max,len;
    while(scanf("%s",str)!=EOF){
        len = strlen(str);
        for(j=0,i=len-1; i>=0; i--){
            if(str[i]>='0' && str[i]<='9') str[i]-='0';
            else str[i]=str[i]-'A'+10;
            if(str[i]>j) j=str[i];
        }
        if(j==0){
            printf("2\n");
            continue;
        }
        for(j++; j<=36; j++){
            max = INT_MAX/(j*36);
            for(sum=0,i=len-1,k=1; i>=0; i--){
                t = str[i]*k;
                if(t+sum<0) sum=sum%(j-1)+t%(j-1);
                else sum+=t;
                k*=j;
                if(k>max) k%=(j-1);
            }
            if(sum%(j-1)==0){
                printf("%d\n",j);
                break;
            }
        }
        if(j>36) printf("No solution.\n");
    }
    return 0;
}

阅读(2532) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册