• codeforces_300C_组合数_快速幂


    C. Beautiful Numbers
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    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, number111 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: abn (1 ≤ a < b ≤ 9, 1 ≤ n ≤ 106).

    Output

    Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).

    Examples
    input
    1 3 3
    output
    1
    input
    2 3 10
    output
    165

     思路:遍历数中的a的个数i,b的个数为n-i,若(i*a)+(b*(n-i))为excellent,ans+=C(i,n);

    求C(i,n)是关键,由费马小定理和乘法逆元可以推得组合数公式。

    1)费马小定理
    费马小定理(Fermat Theory)是数论中的一个重要定理,其内容为: 假如p是质数,且Gcd(a,p)=1,那么 a(p-1)(mod p)≡1。即:假如a是整数,p是质数,且a,p互质(即两者只有一个公约数1),那么a的(p-1)次方除以p的余数恒等于1。
    简而言之就是如果a,p互质,同时p是质数,那么a^(p-1) mod p=1。证明略。

    (2)乘法逆元
    若对于a,p存在x,使得a*x mod p=1,那么我们称x为a对p的乘法逆元。证明略。
    那么乘法逆元存在的意义是什么呢?
    假如我们要求(a/b) mod p且无法直接求得a/b的值时,我们可以求出b对p的乘法逆元inv,那么(a/b) mod p=(a*inv) mod p。证明略。。。

    证明如下:
    假如inv是b对于p的乘法逆元,即b*inv=p*t+1(t为整数),移项得inv=(p*t+1)/b
    (a*inv) mod p
    =(a*((p*t+1)/b)) mod p
    =(a*(p*t/b+1/b)) mod p
    =(a/b) mod p+(a*(p*t+1)) mod p
    =(a/b) mod p+(a*p*t/b) mod p
    ∵ (a*p*t/b) mod p=0
    ∴ 原式=(a/b) mod p
    即(a*inv) mod p=(a/b) mod p

    有了这2个概念我们就可以快速地算出组合数了。
    我们可以知道x与x^p-2互为逆元(p是质数)。

    证明:x与x^(p-2)互为逆元(p是质数)

    由费马小定理:x^(p-1) mod p=1
    x*(x^(p-2)) mod p=1
    得x与x^(p-2)互为乘法逆元,证毕。

    由上述结论可知,要计算C(i,n),即计算n!/(i!*(n-i)!) mod p,那么我们只需要计算n!*(i!*(n-i))^(p-2) mod p。(计算是使用快速幂)

    #include<iostream>
    #include<cstdio>
    using namespace std;
    #define UL unsighed long long
    #define LL long long
    
    int a,b,n;
    int  M=1e9+7;
    
    bool excellent(int x)
    {
        while(x>=0)
        {
            if(x%10!=a&&x%10!=b)
                break;
            x/=10;
        }
        if(x>0)
            return 0;
        else
            return 1;
    }
    
    long long powerMod(long long a,long long b,long long c) ///(a^b)%c
    {
        long long ans=1;
        a=a%c;
        while(b>0)
        {
            if(b%2==1)
                ans=(ans*a)%c;
            b=b/2;
            a=(a*a)%c;
        }
        return ans ;
    }
    
    LL mult[1000005];
    void multi()
    {
        LL res=1;
        mult[0]=1;
        for(int i=1;i<=1000000;i++)
            mult[i]=(mult[i-1]*i)%M;
    }
    
    long long combine(int i,int n)
    {
        LL n1=mult[n];
        LL n2=mult[i];
        n2=(n2*mult[n-i])%M;
        n2=powerMod(n2,M-2,M);
        n2=(n2*n1)%M;
        return n2;
    }
    
    int main()
    {
        long long ans=0;
        multi();
        scanf("%d%d%d",&a,&b,&n);  ///n为数字长度
        for(int i=0;i<=n;i++)
        {
            int mul=i*a+(n-i)*b;
            if(excellent(mul))
                ans=(ans+combine(i,n)%M)%M;
        }
        printf("%I64d
    ",ans%M);
        return 0;
    }
  • 相关阅读:
    【NOIP2016提高A组8.12】奇袭
    【NOIP2016提高A组8.12】礼物
    tarjan算法
    【NOIP2016提高A组8.12】总结
    【NOIP2016提高A组8.11】自然数
    【NOIP2016提高A组8.12】通讯
    【NOIP2016提高A组8.11】种树
    【NOIP2016提高A组8.11】钱仓
    【NOIP2016提高组A组7.16】大鱼海棠
    【NOIP2012模拟8.20】Memory
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/5400490.html
Copyright © 2020-2023  润新知