• 转: Beautiful Numbers (费马小定理)


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

    Status

    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, 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 ≤ 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

    Source

    Status

    题目大意:
    给出a和b,如果一个数每一位都是a或b,那么我们称这个数为good,在good的基础上,如果这个数的每一位之和也是good,那么这个数是excellent。求长度为n的excellent数的个数mod(1e9+7)。ps:1e9+7是一个质数。

    解题思路:
    由于题目中给出了n,所以我们可以枚举a的个数m,那么剩下的(n-m)位就是b。再判断a*m+b*(n-m)是不是good数,如果是,那么我们在答案中加上C(m,n)即可,枚举完毕即最终答案。
    但是n最大为1e6,计算组合数时(C(m,n)=n!/(m!*(n-m)!))要计算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。证明略。。。
    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+(p*t+1)) 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 <cmath>
    #include <iostream>
    const int N = 1e6+100;
    typedef long long LL;
    const LL MOD = 1e9+ 7; 
    LL f[N];
    using namespace std;
    int a, b, n;
    LL deal_pow(LL a, LL k)
    {
        LL ans= 1;
        while(k)
        {
            if(k & 1)
                ans=(ans*a) %MOD;
            a=(a*a) %MOD;
            k >>=1;
        }
        return ans;
    }
    bool judge(LL Q)
    {
        while(Q)
        {
            int digit= Q%10;
            if(digit != a && digit != b)
                return 0;
            Q /= 10;
        }
        return 1;
    }
    int main()
    {
        while(scanf("%d%d%d",&a,&b,&n) != EOF)
        {
            f[0]= 1;
            for(int i=1; i<= n; i++)
                f[i]=(f[i-1]*i)% MOD;
            LL ans =0;
            for(int i=0; i<= n; i++)
            {
                LL sum= a*i + b*(n-i);
                if(!judge(sum))
                    continue;
                LL temp= (f[n-i]*f[i])%MOD;
                ans=(ans+(f[n]* deal_pow(temp, MOD-2)) %MOD) %MOD;  //乘法逆元 ;
            }
            printf("%I64d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    数学模型(第五版) 姜启源、谢金星、叶俊 版 课后答案 高等教育出版社 训练题答案 课后习题答案
    网络编程释疑之:同步,异步,阻塞,非阻塞
    Linux ssh黄金参数
    linux下安装java
    C/C++字符串和其他类型转换
    C语言获取当前时间
    C语言字符串复制
    C语言文件读写操作
    C语言实现读取文件所有内容到字符串
    AES加密的C语言实现
  • 原文地址:https://www.cnblogs.com/soTired/p/5436794.html
Copyright © 2020-2023  润新知