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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值