题面:
Beautiful Numbers
time limit per test:2 second
memory limit per test:256 megabytes
inputstandard:standard input
outputstandard:standard output
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
Sample Output
1
Sample Input
2 3 10
Sample Output
165
题面描述:
给出两个数字a和b,各个位上完全由这两个数字组成的数字叫做good number(也可以只有a或只有b组成),并且组成的数字的各个位加起来的和也是good number,那么这个由a,b组成的good number就是excellent数,输入三个数a,b,n,分别代表喜欢的数a,b和要组成的数的位数,求最多有多少个符合题意的excellent数。
题目分析:
从正面算由a,b构成的数有多少个,那么每个位上不是a就是b,知道了要组成的数的位数,可以直接枚举a和b分别有多少个,满足good number数的情况下再看是否满足excellent数,比较巧妙的地方就是对枚举出的每种情况如果成立则还要进行C(n,m)的排列组合。
代码:
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include<string>
#include<math.h>
#include<stdlib.h>
#define mol 1000000007
using namespace std;
long long a,b,n,ans=0,fac[1000010]={1,1};
int check(int x){
while(x){
if(x%10!=a&&x%10!=b) return 0;
x/=10;
}
return 1;
}
long long inv(long long a,long long b){
long long ans=1;
while(b){
if(b&1) ans=ans*a%mol;
a=a*a%mol;
b>>=1;
}
return ans;
}
int main(){
cin>>a>>b>>n;
for(long long i=2;i<=n;i++) fac[i]=i*fac[i-1]%mol;
for(long long i=0;i<=n;i++){//因为只有两个数a,b,枚举每个数个有多少个,再用一下C(n,a)组合
long long sum=i*a+(n-i)*b;//看各个位相加满不满足
if(check(sum)){//这里也可以先算C(n,i)的下半部分fac[i]*fac[n-i]%mol在丢到inv里去
ans=ans+fac[n]*inv(fac[i],mol-2)%mol*inv(fac[n-i],mol-2)%mol;
}
}
cout<<ans%mol;
}