Description
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, number 111 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.
Input
The first line contains three integers: a, b, n(1 ≤ a < b ≤ 9, 1 ≤ n ≤ 106).
Output
Print a single integer — the answer to the problem modulo 1000000007(109 + 7).
Sample Input
1 3 3
1
2 3 10
165
题意:
somebody喜欢两个数字a和b,他把各个位上完全由这两个数字组成的数字叫做good number(也可以只有a或只有b组成),要是组成的数字的各位加起来的和也是good number,那么这个由a,b组成的good number就是excellent数,输入三个数a,b,n,分别代表喜欢的数a,b和要组成的数的位数,求最多有多少个符合题意的excellent数?
解法:乘法逆元+二分幂
<span style="font-size:18px;">#include<stdio.h>
long long int mod=1000000007;
long long int vis[1000010]={1};
long long int charge(long long int m,long long int n)//快速二分幂求值
{
if(n==0)
return 1;
long long int t=1;
while(n)
{
if(n%2==1)
t=t*m%mod;
m=m*m%mod;
n/=2;
}
return t;
}
int main()
{
long long int a,b,n,i,j,sum,t1,t2,ans;
while(scanf("%lld%lld%lld",&a,&b,&n)!=EOF)
{
ans=0;
for(i=1;i<=n;i++)
vis[i]=vis[i-1]*i%mod;//求1到n的阶乘
for(i=0;i<=n;i++)
{
j=n-i;
sum=a*i+b*j;
while(sum)
{
long long int t=sum%10;
if(t!=a&&t!=b)
break;
sum=sum/10;
}
if(sum==0)//其实这里还包含了一个乘法逆元的求法
{
t1=charge(vis[i],mod-2);
t2=charge(vis[n-i],mod-2);
ans+=vis[n]*t1%mod*t2%mod;
}
}
printf("%lld\n",ans%mod);
}
return 0;
}</span>