`
bcyy
  • 浏览: 1824657 次
文章分类
社区版块
存档分类
最新评论

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 numberksatisfying the following condition: the given number, written ink-based system is divisible byk−1.

Input

Input consists of one string containing no more than 106digits or uppercase Latin letters.

Output

Output the only numberk, 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.";
}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics