2 seconds
256 megabytes
standard input
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.
The first line contains three integers: a,
b, n
(1 ≤ a < b ≤ 9, 1 ≤ n ≤ 106).
Print a single integer — the answer to the problem modulo
1000000007 (109 + 7).
1 3 3
1
2 3 10
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; }