zoukankan      html  css  js  c++  java
  • CodeForces 300C --数论

    A - A
    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    Description

    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).

    Sample Input

    Input
    1 3 3
    Output
    1
    Input
    2 3 10
    Output
    165


    这道题的想法很简答,枚举a的个数,判断a*i+b*(n-i)是不是一个good数,如果是的话,那么i个a + n-i个b就可以组成excellent数。
    然后求组合数C(i,n)。
    问题是C(i,n)很不好求。因为C(i,n)=(n!)/[(i!*(n-i)!],本身数据量就很大,而mod 1e9后,直接计算会造成精度的损失。
    这里用到两个知识:费马小定理 乘法逆元,在加一个简单的快速幂。


    (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。证明略。。。
    bazinga!!!
    证明如下:
    假如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<stdio.h>
    //#define prim 1000000000+7
    using namespace std;
    int a,b,n;
    const long long prim=1e9+7;
    long long dp[1000005];
    bool exce(long long sum)
    {
        //cout<<sum<<endl;
        while(sum>0)
        {
            if(sum%10!=a&&sum%10!=b)
                return false;
            sum/=10;
        }
        return true;
    }
    long long int comb(long long i)
    {
       // if(i==0||i==1) return 1;
        long long int _i;
        _i=dp[i];
        long long int _ans=1;
        long long int p=prim-2;
        while(p>0)
        {
            if(p&1)
                _ans=_ans*_i%prim;
            _i=_i*_i%prim;
            p=p>>1;
    
        }
        return _ans%prim;
    
    }
    int main()
    {
        cout<<prim-2<<endl;
        dp[0]=1;
    
        //printf("%I64d
    %I64d",dp[1000000],dp[1000000-1]);
        long long int ans=0;
        scanf("%d%d%d",&a,&b,&n);
        for(long long int i=1;i<=n;i++)
        {
            dp[i]=(dp[i-1]*i)%(prim);
        }
        for(long long int i=0;i<=n;i++)
        {
            if(exce(a*i+b*(n-i)))
            {
                long long comi=comb(i);
                long long comni=comb(n-i);
                //cout<<comi<<endl<<comni<<endl;
                ans+=dp[n]*comi%prim*comni%prim;
            }
        }
        printf("%I64d
    ",ans%prim);
        return 0;
    }
    View Code

    这里我要说明一下,你会发现我的代码里面有两个prim,一个是constant,一个是#define,经过实践检验,我发现用常变量是正确的,用宏是错误的。
    虽然我现在还不知道是为什么,但是应该注意到这一点,以后涉及到计算的一律用常变量。
    再用宏我就是脑袋有坑。


  • 相关阅读:
    ubuntu的php7与apache2的环境搭建
    git服务器搭建post-receive 钩子部署服务端代码
    node redis安装与使用
    微信小程序坑集
    微信小程序登录
    mobx react
    react-native-router-flux 下部导航
    java
    java
    java
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5405443.html
Copyright © 2011-2022 走看看