A-aTime
limit:2000MS
Memory Limit:262144KB
64bit IO Format:%i64d &%i6 4u SubmitStatusPracticecodeforces 300C
Description
Vitaly is a very weird man. He's got and favorite Digits a and b . Vitaly calls a positive integer good, if the decimal representation of this integer o Nly 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 is 1 and 3, then number is ' t good and numbers 1 3 or 311 is. Also, number 111 is excellent and number one is ' t.
Now Vitaly was wondering, how many excellent numbers of length exactly n was there. As this number can is rather large, he asks you to count the remainder after dividing it by 1000000007(9 + 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(9 + 7).
Sample Input
Input
1 3 3
Output
1
Input
2 3 10
Output
165
The idea of this problem is very simple answer, enumerate a number, Judge a*i+b* (N-i) is not a good number, if so, then i A + n-i B can form excellent number.
Then the combination number C (i,n) is obtained.
The problem is that C (i,n) is very difficult to find. Because C (i,n) = (n!) /[(i!* (n-i)!], the amount of data itself is very large, and mod 1e9, the direct calculation will result in the loss of precision.
Here are two things to know: Fermat theorem multiplication Inverse, in addition to a simple fast power.
(1) Fermat theorem
The Fermat theorem (Fermat theory) is an important theorem in number theory, which includes: if P is prime, and gcd (a,p) = 1, then A (p-1) (mod p) ≡1. That is: If A is an integer, p is a prime number, and A,p coprime (that is, there is only one convention of 1), then the remainder of a (p-1) divided by P is constant equal to 1. (I love Niang (╯‵-′) ╯︵┻━┻).
In short, it isif A,p coprime, and P is prime, then a^ (p-1) mod p=1. Proof slightly.
(2) multiplication inverse element
if x is present for a,p, which makes A*x mod p=1, then we call x A-p multiplication inverse. Proof slightly.
So what is the meaning of the existence of multiplicative inverses?
If we ask for a (A/b) mod p and cannot directly obtain a A/b value, we can find the multiplicative inverse of B to P inv, then (A/b) mod p= (A*INV) mod p. Prove a little ...
bazinga!!!
The proof is as follows:
If INV is b for P multiplication inverse, namely b*inv=p*t+1 (T is an integer), move the item to inv= (p*t+1)/b
(A*INV) mod p
= (A * ((p*t+1)/b)) mod p
= (* (p*t/b+1/b)) mod p
= (A/b) mod p+ (* (p*t+1)) mod p
= (A/b) mod p+ (a*p*t/b) mod p
∵ (a*p*t/b) mod p=0
∴ Primitive = (A/b) mod p
IE (A*INV) mod p= (A/b) mod p
With these 2 concepts we can quickly figure out the number of combinations.
We can know that X and x^p-2 are mutually inverse (p is prime).
/*
Proof: X and x^ (p-2) are mutually inverse (p is prime)
By Fermat theorem: x^ (p-1) mod p=1
x* (x^ (p-2)) mod p=1
The X and x^ (p-2) are mutually multiplicative inverses, and the proof is completed.
*/
From the above conclusion, to calculate C (i,n), that is, calculate n!/(i!* (n-i)!) MoD p, then we only need to calculate n!* (i!*) ^ (n-i) mod p.
#include <iostream>#include<stdio.h>//#define PRIM 1000000000+7using namespacestd;intA,b,n;Const Long Longprim=1e9+7;Long Longdp[1000005];BOOLExce (Long Longsum) { //cout<<sum<<endl; while(sum>0) { if(sum%Ten!=a&&sum%Ten!=b)return false; Sum/=Ten; } return true;}Long Long intComb (Long Longi) { //if (i==0| | I==1) return 1; Long Long int_i; _i=Dp[i]; Long Long int_ans=1; Long Long intp=prim-2; while(p>0) { if(p&1) _ans=_ans*_i%Prim; _i=_i*_i%Prim; P=p>>1; } return_ans%Prim;}intMain () {cout<<prim-2<<Endl; dp[0]=1; //printf ("%i64d\n%i64d", Dp[1000000],dp[1000000-1]); Long Long intans=0; scanf ("%d%d%d",&a,&b,&N); for(Long Long intI=1; i<=n;i++) {Dp[i]= (dp[i-1]*i)%(Prim); } for(Long Long intI=0; i<=n;i++) { if(Exce (a*i+b* (ni )) {Long LongComi=comb (i); Long LongComni=comb (ni); //cout<<comi<<endl<<comni<<endl;ans+=dp[n]*comi%prim*comni%Prim; }} printf ("%i64d\n", ans%Prim); return 0;}
View Code
Here I want to explain, you will find my code inside there are two prim, one is constant, one is # define, after the practice test, I found that using the constant variable is correct, with the macro is wrong.
Although I do not know why, but should be aware of this point, later involved in the calculation of the constant variable.
I just have a hole in my head.
Codeforces 300C--Number theory