• codeforces 300 C Beautiful Numbers 逆元的运用 木


    题目链接: www.codeforces.com/problemset/problem/300/C

    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 test(s)
    Input
    1 3 3
    Output
    1
    Input
    2 3 10
    Output
    165


    题目很简单解法也是很容易想到的,不过不要高兴太早,看看数据就知道了。直接算的话,会TLE,或者直接就会崩溃掉0.
    这题主要是解决c(a, b)%p这个值。有数论知识可知:
    定于inv(a) 为a的逆元。
    c(a, b)%p
    = ( a! / (b!*(a-b)!) )%p
    = a!%p * inv( b!*(a-b)! )%p
    接下来考虑计算inv(b!*(a-b)!)%p

    逆元? 什么是逆元?
    逆元定于:http://baike.baidu.com/view/699065.htm
    如有a*b=1,则a,b互为逆元。理由是此时1是乘法单位元。
    那么a*b=1%p,则a的逆元是b%p,b的逆元是a%p
    是不是逆元一定存在呢? 其实是不一定的。
    逆元存在的条件是: gcd(a, p)=1则a的逆元是存在的。这个我不会证明。也只是知道这么做

    逆元是如何计算呢?
    gcd(a, p)=1, t = Eula(p)
    则a的逆元是 inv(a) = a^(t-1)
    Eula(p) 是p的欧拉函数。欧拉函数就是在(1, n)的区间里与p互素的数的个数。

    那么如何求Eula(p)?
    1,p是素数,Eula(p) = p-1,根据定于可知。
    2,p不是素数。Eula(p)=p-p/x (x是p的所有素因子, 对于相同因子只使用1次)

    还是回到原来的问题:题目给的是p=10^9 + 7 这时候你该呵呵了,这是个素数,所以你懂得。。
    inv(b!*(a-b)!)%p
    =( b!*(a-b)! )^(p-2)%p ( Eula(p) = p-1 )
    =(b!)^(p-2)%p*( (a-b)! )^(p-2)%p
    =(b!%p)^(p-2)%p *( (a-b)!%p )^(p-2)%p

    可以预处理阶乘取摸的结果,即等式中的(b!%p) 和 ((a-b)!%p)

    到此可以解决该题了, 哈哈 。。。 我的python代码:
     1 a, b, n = map(int, raw_input().split())
     2 f = [1]*(n+1)
     3 mod = 10**9+7
     4 for i in xrange(1, n+1):  f[i] = f[i-1]*i%mod
     5 
     6 v = [a, b]
     7 def OK(x):
     8     while x > 0:
     9         if x%10 not in v:
    10             return 0
    11         x /= 10
    12     return 1
    13 
    14 def POW(x, y, m):
    15     a = x; b = 1
    16     while y>0:
    17         if y&1:
    18             b = b*a%m
    19         a = a*a%m
    20         y >>= 1
    21     return b
    22 
    23 ans = 0
    24 for i in xrange(n+1):
    25     if OK(a*i+(n-i)*b):
    26         ans = (ans+ POW(f[i]*f[n-i], mod-2, mod) ) 
    27        // 自己写pow函数比调用库函数还慢,开始还以为库函数是很暴力呢。。。
    28         if ans >= mod: ans %= mod
    29 print f[n]*ans%mod
  • 相关阅读:
    Python中的返回函数与闭包
    Python的高阶函数小结
    Python的生成器Generator小结
    Vim插件YCM的安装
    用Vundle管理Vim插件
    声卡(Sound Card)基本概念
    Linux中Source的用法
    js 的执行过程
    mongoose@4.5.2的eachAsync bug
    [mongodb] MMAP 和wiredTiger 的比较
  • 原文地址:https://www.cnblogs.com/TengXunGuanFangBlog/p/codeforcesProlem.html
Copyright © 2020-2023  润新知