现在的位置: 首页 > 综合 > 正文

Codeforces Round #181 (Div. 2)—C. Beautiful Numbers

2019年02月17日 ⁄ 综合 ⁄ 共 2070字 ⁄ 字号 评论关闭
C. Beautiful Numbers
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Vitaly is a very weird man. He's got two favorite digits
a
and b. Vitaly calls a positive integer
good, if the decimal representation of this integer only contains digits
a and b. Vitaly calls a good number
excellent, if the sum of its digits is a good number.

For example, let's say that Vitaly's favourite digits are
1
and 3, then number
12
isn't good and numbers 13 or
311
are. Also, number 111 is excellent and number
11 isn't.

Now Vitaly is wondering, how many excellent numbers of length exactly
n
are there. As this number can be rather large, he asks you to count the remainder after dividing it by
1000000007 (109 + 7).

A number's length is the number of digits in its decimal representation without leading zeroes.

Input

The first line contains three integers: a,
b, n
(1 ≤ a < b ≤ 9, 1 ≤ n ≤ 106).

Output

Print a single integer — the answer to the problem modulo
1000000007
(109 + 7).

Sample test(s)
Input
1 3 3
Output
1
Input
2 3 10
Output
165

此题首先要列出方程 sum = x * a + (n - x) * b;

表示一个n位的数里有x个a, 1 - x 个 b,如此以来只要一遍for就行了,然后判断sum是否是good数,如果是, 剩下的就是一个组合数取模的问题了,这个可以用扩展欧几里得求逆元搞定

/*************************************************************************
    > File Name: tmp.cpp
    > Author: ALex
    > Mail: 405045132@qq.com 
    > Created Time: 2014年12月26日 星期五 19时22分14秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long 

using namespace std;

const int mod = 1000000007;

ll extend_gcd(ll a, ll b, ll &x, ll &y)
{
	if (!b)
	{
		x = 1;
		y = 0;
		return a;
	}
	ll gcd = extend_gcd(b, a % b, x, y);
	ll t = x;
	x = y;
	y = t - (a / b) * x;
}

ll get_inverse(ll num)
{
	ll x, y;
	extend_gcd(num, mod, x, y);
	return (x % mod + mod) % mod;
}

ll combine(ll n, ll m)
{
	ll t1 = 1, t2 = 1;
	for (ll i = n; i > m; --i)
	{
		t1 = (t1 * i) % mod;
		t2 = (t2 * (i - m)) % mod;
	}
	return t1 * get_inverse(t2) % mod;
}

int main()
{
	int a, b, n;
	while (~scanf("%d%d%d", &a, &b, &n))
	{
		int ans = 0;
		for (int i = 0; i <= n; ++i)
		{
			ll sum = i * a + (n - i) * b;
			bool flag = true;
			while (sum != 0)
			{
				if (sum % 10 == a || sum % 10 == b)
				{
					sum /= 10;
					continue;
				}
				flag = false;
				break;
			}
			if (!flag)
			{
				continue;
			}
			if (i == 0)
			{
				ans++;
				ans %= mod;
				continue;
			}
			ans += combine(n, i); 
			ans %= mod;
		}
		printf("%d\n", ans);
	}
	return 0;
}

抱歉!评论已关闭.