-
Notifications
You must be signed in to change notification settings - Fork 152
/
300C-BeautifulNumbers.cpp
39 lines (30 loc) · 1006 Bytes
/
300C-BeautifulNumbers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <cstdio>
#include <vector>
typedef long long ll;
bool checkDigits(long x, int a, int b){
while(x > 0){
int d = x % 10; x /= 10;
if((d != a) && (d != b)){return false;}
}
return true;
}
int main(){
const ll M = 1000000007;
int a, b; long n; scanf("%d %d %ld\n", &a, &b, &n);
std::vector<ll> invMod(n + 1, 1);
for(long p = 2; p <= n; p++){invMod[p] = M - ((M / p) * invMod[M % p]) % M;}
//Proof: a(m/a) + m % a = m = 0 mod m; Multiply both sides by inv[a] * inv[m % a] => inv[m % a] * (m / a) + inv[a] = 0 mod m;
std::vector<ll> fact(n + 1, 1);
for(long p = 1; p <= n; p++){
if(p > n - p){fact[p] = fact[n - p]; continue;}
fact[p] = (((fact[p - 1] * (n - p + 1)) % M) * invMod[p]) % M;
}
ll total(0);
for(long p = 0; p <= n; p++){
long x = p * a + (n - p) * b;
if(!checkDigits(x, a, b)){continue;}
total += fact[p]; total %= M;
}
printf("%lld\n", total);
return 0;
}