Vitaly有一些奇怪的癖好,比如他特别爱两个小于10的数字a和b。Vitaly定义十进制表示下每一位都是a或b的数为“好数”,一个每一位数加起来为“好数”的“好数”被称为“极好的数”。
举个栗子=w=,如果偏爱数字为1和3,那么1212不是“好数”,13和311是“好数”,111是“极好的数”。
现在Vitaly想知道,长度为n(长度不包括前导0)的“极好的数”有多少个。对1e9+7取模。
n≤10^6
输入 #1
1 3 3
输出 #1
1
因为判断一个数是否为极好的数仅与它的位数和有关。
所以当一个整数 abb为极好的数时,bab,bba 也为极好的数
所以我们可以枚举 a 的个数,设 a 有 i 个,则 b 有 n−i 个。判断这些数组成的数位数和能否满足条件,然后用排列组合求可以组成多少个数,就是在 n 个数中选择 i 个,也就是 C(n,i),对每一个满足条件的数答案求和即可。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define int long long
using namespace std;
typedef long long ll;
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
int n, a, b, ans;
int f[N];
bool judge(int x)
{
int k;
while(x > 0)
{
k = x % 10;
if(k != a && k != b)
return false;
x /= 10;
}
return true;
}
int quick(int x, int y)
{
int res = 1, base = x;
while(y > 0)
{
if(y & 1)
res = (res * base) % mod;
base = (base * base) % mod;
y /= 2;
}
return res;
}
int C(int n, int m)
{
int x = 1, y = 1;
x = f[n];
y = (f[m] * f[n - m]) % mod;
y = quick(y, mod - 2);
return x * y;
}
signed main()
{
cin >> a >> b >> n;
f[0] = f[1] = 1;
for(int i = 2; i <= n; ++i)
f[i] = (f[i - 1] * i) % mod;
for(int i = 0; i <= n; ++i)
if(judge(a * i + b * (n - i)))
ans = (ans + C(n, i)) % mod;
printf("%lld", ans);
return 0;
}