Codeforces 300C

题目链接: http://codeforces.com/contest/300/problem/C

本来是道不难的题目,还是自己的数学功底不扎实。

从该题又一次巩固了关于乘法逆的概念,在剩余系中,如果要做除法,如 a / b%n ,  此时应该计算 a * (b在n下的逆), 而不是直接计算 a / b

另外一点值得注意的是,如果 n 为一素数,那么 b 的逆就是 pow_mod(a, n-2, n). 模拟叫做费马小定理。

乘法逆真的很重要。务必牢记!!!


附AC代码:

 1 /*************************************************************************
 2     > File Name: 300C.c
 3     > Author: Stomach_ache
 4     > Mail: 1179998621@qq.com 
 5     > Created Time: 2014年01月07日 星期二 17时32分26秒
 6  ************************************************************************/
 7 
 8 #include<stdio.h>
 9 #include<string.h>
10 #include<stdlib.h>
11 
12 #define mod 1000000007
13 typedef long long LL;
14 int a, b, n, c[1000005], rfact[1000005]; // c[] -> c(n, k).............rfack[] -> k在n下的逆
15 
16 // x is good ?
17 int ok(int x) {
18         while (x > 0) {
19                 int tmp = x % 10;
20                 if (tmp != a && tmp != b)
21                     return 0;
22                 x /= 10;
23         }
24 
25         return 1;
26 }
27 // quick mod
28 LL pow_mod(LL a, LL b, LL c) {
29         LL res = 1;
30         while (b) {
31                 if (b & 1)
32                     res = (res * a) % c;
33                 a = (a * a) % c;
34                 b >>= 1;
35         }
36 
37         return res % c;
38 }
39 
40 int main(void) {
41         while (~scanf("%d %d %d", &a, &b, &n)) {
42                 int i, cur = a * n, d = b - a;
43                 LL cnt = ok(cur);
44                 c[0] = 1;
45                 for (i = 1; i <= n; i++) {
46                         rfact[i] = pow_mod(i, mod-2, mod);
47                         c[i] = (LL)c[i - 1] * (n - i + 1) % mod;
48                         c[i] = (LL)c[i] * rfact[i] % mod;
49                 }
50                 for (cur += d, i = 1; i < n; i++, cur += d) {
51                         if (ok(cur)) {
52                                 cnt += c[i];
53                                 cnt %= mod;
54                         }
55                 }
56                 cnt = (cnt + ok(b * n)) % mod;
57                 printf("%lld\n", cnt);
58         }
59 
60         return 0;
61 }

 

 

转载于:https://www.cnblogs.com/Stomach-ache/p/3703174.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值