CodeForces 300C C. Beautiful Numbers (数论 + 逆元 + 详解)

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

Sample test(s)
input
1 3 3
output
1
input
2 3 10
output
165
方法一,通过学习数论乘法逆元可以快速的解决问题,
方法二,在deal(num[1] * i + (n - i) * num[2])将所有满足条件的所需要的a,b的个数先预处理用数组存储起来,然后,通过DFS()不断遍历满足条件的个数,用ans记录数据,
这里讲解一下乘法逆元的相关知识:
乘法逆元的主要运用是将除法变为乘法:
F / A mod C = D
存在一个数K 使得 A * K == 1(mod C)
那么两种乘起来就是F * K mod C = D
如此我们就将除法变为了乘法,现在只需要求解K的值
然后说明 a/x mod p=a*x^(p-2) mod p其中p为质数
的原因,这里要运用到费马小定理,也许读者可能不知道,我无法给出证明,但是大家可以先记住费马小定理就可以了:x^(p - 1) mod p == 1其中p是质数,
所以之前说的a / x mod p = a * x^(p - 2) mod p
通过前面的乘法逆元可以知道,x ^ (p - 1) mod p = 1 - > a / x mod p = a / x * x ^ (p - 1) mod p
-> a * x ^ (p - 2) mod p
接下来的解答,大家可以通过看代码了解

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
const int MAXN = 1e6 + 5;
LL n;
LL num[3];
LL ans,f[MAXN];
LL mod_pow(LL x,LL n,LL mod) {
    LL res = 1;
    while(n > 0) {
        if(n & 1)res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}
bool deal(LL x) {
    while(x > 0) {
        LL tmp = x % 10L;
        if(tmp != (LL)num[1] && tmp != (LL)num[2]) return false;
        x /= 10;
    }
    return true;
}
int main() {
    scanf("%I64d%I64d%I64d", &num[1], &num[2], &n);
    LL ans = 0;
    f[0] = 1;
    for(int i = 1; i <= n; i ++)f[i] = f[i - 1] * i % mod;
    for(int i = 0; i <= n; i ++) {
        if(deal(num[1] * i + (n - i) * num[2])) {
            ans = (ans + mod_pow(f[i] * f[n - i] % mod,mod - 2, mod) % mod) % mod;
        }
    }
    printf("%I64d\n",f[n] * ans % mod);
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值