(数论)D - Beautiful Numbers
Posted studyshare777
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(数论)D - Beautiful Numbers相关的知识,希望对你有一定的参考价值。
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 ≤ 1e6).
Output
Print a single integer — the answer to the problem modulo 1000000007 (1e9 + 7).
Examples
Input
1 3 3
Output
1
Input
2 3 10
Output
165
题目描述:
给出a,b两个数,如果一个十进制数只由a,b组成。他是一个good number。如果各个位数上的数字加起来是一个good number。那么就可以说它是一个excellent number。给出n表示数的位数。问一共由多少个excellent number。(结果取模1000000007 (1e9 + 7))
分析:
计算较大的组合数取模,因为C(m,n)=n!/(m! x (n-m)!),所以要用费曼小定理计算。计算(n-m)!的时候,可以看成n!/n ->> n/(n-1) ->>。。。。依次除以(n-m),同样可以用费曼小定理。
注意大数幂运算要用到快速幂运算。
注意:
要预先处理阶乘,不然会T。
代码:
#include <iostream> #include <cstdio> using namespace std; typedef long long ll; const int mod=1000000007; int a,b; bool good(int x) { while(x) { int gewei=x%10; if(gewei==a||gewei==b) { x/=10; } else return false; } return true; } ll mod_pow(ll x,ll n) { ll res=1; while(n) { if(n&1) res=res*x%mod; x=x*x%mod; n>>=1; } return res; } ll cn; ll cm; ll cn_m; ll C() { //组合数 ll ans=cn * mod_pow(cm,mod-2)%mod * mod_pow(cn_m,mod-2)%mod; return ans; } int main() { int n; scanf("%d%d%d",&a,&b,&n); ll ans=0; cn=1; for(int i=2;i<=n;i++) { cn=i*cn%mod; } cn_m=cn; cm=1; for(int i=0;i<=n;i++) { ll num=i*a+(n-i)*b; if(i!=0) cm=cm*i%mod; if(n-i+1!=0&&i!=0) cn_m=cn_m*mod_pow(n-i+1,mod-2)%mod; if(good(num)) { ans=(ans+C())%mod; } } cout<<ans; return 0; }