Codeforces 300C Beautiful Numbers (multiplication inverse/Fermat theorem + combination number formula + fast power)

Source: Internet
Author: User
Tags time limit

C. Beautiful Numbers time limit per test 2 seconds memory limit per test megabytes input standard input output Standar D Output

Vitaly is a very weird man. He ' s got, 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 it 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 or 311 is. Also, number111 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 (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 Test (s) input

1 3 3
Output
1
Input
2 3 10
Output
165


Main topic:Given A and B, if a number of each is a or B, then we call this number good, on the basis of good, if the sum of each of the numbers is also good, then this number is excellent. The number of excellent with a length of n is mod (1e9+7). Ps:1e9+7 is a prime number.

Problem Solving Ideas:
Since n is given in the topic, we can enumerate the number m of a, then the remaining (n-m) bit is B. Again Judge A*m+b* (N-M) is not the number of good, if so, then we add C (m,n) in the answer, the enumeration is finished the final answer.
But the maximum of n is 1e6, when the number of combinations is calculated (C (m,n) =n!/(m!* (n-m))) To calculate the factorial of N, a direct calculation will definitely cause an error.
Here are some mathematical knowledge:


(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.
In short, if 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 the multiplicative inverse element?
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.
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.

Reference Code:

#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;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.