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, number111 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
方法一,通过学习数论乘法逆元可以快速的解决问题,
方法二,在deal(num[1] * i + (n - i) * num[2])将所有满足条件的所需要的a,b的个数先预处理用数组存储起来,然后,通过DFS()不断遍历满足条件的个数,用ans记录数据,
这里讲解一下乘法逆元的相关知识:
乘法逆元的主要运用是将除法变为乘法:
F / A mod C = D
存在一个数K 使得 A * K == 1(mod C)
那么两种乘起来就是F * K mod C = D
如此我们就将除法变为了乘法,现在只需要求解K的值
然后说明 a/x mod p=a*x^(p-2) mod p其中p为质数
的原因,这里要运用到费马小定理,也许读者可能不知道,我无法给出证明,但是大家可以先记住费马小定理就可以了:x^(p - 1) mod p == 1其中p是质数,
所以之前说的a / x mod p = a * x^(p - 2) mod p
通过前面的乘法逆元可以知道,x ^ (p - 1) mod p = 1 - > a / x mod p = a / x * x ^ (p - 1) mod p
-> a * x ^ (p - 2) mod p
接下来的解答,大家可以通过看代码了解
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; typedef long long LL; const LL mod = 1e9 + 7; const int MAXN = 1e6 + 5; LL n; LL num[3]; LL ans,f[MAXN]; LL mod_pow(LL x,LL n,LL mod) { LL res = 1; while(n > 0) { if(n & 1)res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } bool deal(LL x) { while(x > 0) { LL tmp = x % 10L; if(tmp != (LL)num[1] && tmp != (LL)num[2]) return false; x /= 10; } return true; } int main() { scanf("%I64d%I64d%I64d", &num[1], &num[2], &n); LL ans = 0; f[0] = 1; for(int i = 1; i <= n; i ++)f[i] = f[i - 1] * i % mod; for(int i = 0; i <= n; i ++) { if(deal(num[1] * i + (n - i) * num[2])) { ans = (ans + mod_pow(f[i] * f[n - i] % mod,mod - 2, mod) % mod) % mod; } } printf("%I64d\n",f[n] * ans % mod); return 0; }