题目大意:
给出a和b,如果一个数每一位都是a或b,那么我们称这个数为good,在good的基础上,如果这个数的每一位之和也是good,那么这个数是excellent。求长度为n的excellent数的个数mod(1e9+7)。ps:1e9+7是一个质数。参考代码:
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const double eps=1e-10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
const int MAXN=1e6+50;
typedef __int64 LL;
LL f[MAXN],a,b,n;
bool is_excellent(int x)
{
while(x)
{
if(x%10!=a&&x%10!=b)
return false;
x/=10;
}
return true;
}
LL fastmod(LL b,LL c,LL mod)//b^c%mod
{
LL re=1,base=b;
while(c)
{
if(c&1)
re=((re%mod)*(base%mod))%mod;
base=((base%mod)*(base%mod))%mod;
c>>=1;
}
return re%mod;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
f[0]=1;
f[1]=1;
for(int i=2;i<=1e6;i++)
f[i]=(f[i-1]*i)%MOD;
while(scanf("%I64d%I64d%I64d",&a,&b,&n)!=EOF)
{
LL ans=0;
for(int i=0;i<=n;i++)
{
int num=a*i+b*(n-i);
if(is_excellent(num))
{
//DEBUG;
LL t=f[n];
t=(t*fastmod(f[i],MOD-2,MOD))%MOD;
t=(t*fastmod(f[n-i],MOD-2,MOD))%MOD;
ans=(ans+t)%MOD;
}
}
printf("%I64d\n",ans%MOD);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
CodeForces 300C Beautiful Numbers(乘法逆元/费马小定理+组合数公式+快速幂)
原文地址:http://blog.csdn.net/noooooorth/article/details/47735855