Time Limit: 2000MS | Memory Limit: 262144KB | 64bit IO Format: %I64d & %I64u |
Description
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 Input
1 3 3
1
2 3 10
165
Source
#include <cmath> #include <iostream> const int N = 1e6+100; typedef long long LL; const LL MOD = 1e9+ 7; LL f[N]; using namespace std; int a, b, n; LL deal_pow(LL a, LL k) { LL ans= 1; while(k) { if(k & 1) ans=(ans*a) %MOD; a=(a*a) %MOD; k >>=1; } return ans; } bool judge(LL Q) { while(Q) { int digit= Q%10; if(digit != a && digit != b) return 0; Q /= 10; } return 1; } int main() { while(scanf("%d%d%d",&a,&b,&n) != EOF) { f[0]= 1; for(int i=1; i<= n; i++) f[i]=(f[i-1]*i)% MOD; LL ans =0; for(int i=0; i<= n; i++) { LL sum= a*i + b*(n-i); if(!judge(sum)) continue; LL temp= (f[n-i]*f[i])%MOD; ans=(ans+(f[n]* deal_pow(temp, MOD-2)) %MOD) %MOD; //乘法逆元 ; } printf("%I64d\n",ans); } return 0; }