题意:长度为n的数字,n<=1e6 合法数字:只含有数字a,b&&所有数字之和也只包含a,b,求合法数字的个数?
暴力枚举:如果包含了x个a 则b有n-x个,若数字之和也合法,则该方案有C(n,x)种取法,累加即可.mod为素数,组合数取模求逆元时用到费马小定理
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e6+20;
const ll mod=1e9+7;
ll n,a,b;
ll ans;
ll exp_mod(ll a, ll b, ll p) {
ll res = 1;
while(b != 0) {
if(b&1) res = (res * a) % p;
a = (a*a) % p;
b >>= 1;
}
return res;
}
ll Comb(ll a, ll b, ll p)
{
if(a < b) return 0;
if(a == b) return 1;
if(b > a - b) b = a - b;
ll ans = 1, ca = 1, cb = 1;
//可以预先求出阶乘优化下
for(ll i = 0; i < b; ++i)
{
ca = (ca * (a - i))%p;
cb = (cb * (b - i))%p;//有除法,用费马小求出逆元后在取模
}
ans = (ca*exp_mod(cb, p - 2, p)) % p;//
return ans;
}
bool check(ll x,ll y)
{
ll s=x*a+y*b;
ll k=0;
while(s)
{
int r=s%10;
if(r!=a&&r!=b)
return false;
s/=10;
}
return true;
}
int main()
{
while(cin>>a>>b>>n)
{
ans=0;
for(int i=0;i<=n;i++)
{
if(check(i,n-i))
{
ans=(ans+Comb(n,i,mod))%mod;
}
}
cout<<ans<<endl;
}
return 0;
}