Timus 1104. Don’t Ask Woman about Her Age题解

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 ksatisfying 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 10 6digits 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 能除尽k base数中k-1的数,必然各位加起来也能除尽k-1

2 一个数的base必然大于一个位的数(不能等于)

尤其是第一个知识点,不知道的话程序就会变得很复杂或者是甚至解不出来。


还有注意特例的时候:全零


int maxKNum(string s, int &k)
{
	int sum = 0, a = 0;
	for (int i = 0; i < s.size(); i++)
	{
		if (s[i] >= 'A') a = s[i] - 'A' + 10;
		else a = s[i] - '0';
		k = max(k, a);
		sum += a;
	}
	return sum;
}

void DonAskWomanaboutHerAge1104()
{
	string s;
	cin>>s;

	int k = 0;
	int sum = maxKNum(s, k);
	if (0 == k) k++;//特例:全零的时候
	for ( ; k < 36; k++)
	{
		if (sum % (k) == 0)
		{
			cout<<k+1;
			return;
		}
	}
	cout<<"No solution.";
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值