您的位置:首页 > 其它

Codeforces 300C

2014-01-07 19:24 323 查看
题目链接: http://codeforces.com/contest/300/problem/C
本来是道不难的题目,还是自己的数学功底不扎实。

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

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

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

附AC代码:

/*************************************************************************

> File Name: 300C.c

> Author: Stomach_ache

> Mail: 1179998621@qq.com

> Created Time: 2014年01月07日 星期二 17时32分26秒

************************************************************************/

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#define mod 1000000007

typedef long long LL;

int a, b, n, c[1000005], rfact[1000005]; // c[] -> c(n, k).............rfack[] -> k在n下的逆

// x is good ?

int ok(int x) {

while (x > 0) {

int tmp = x % 10;

if (tmp != a && tmp != b)

return 0;

x /= 10;

}

return 1;

}

// quick mod

LL pow_mod(LL a, LL b, LL c) {

LL res = 1;

while (b) {

if (b & 1)

res = (res * a) % c;

a = (a * a) % c;

b >>= 1;

}

return res % c;

}

int main(void) {

while (~scanf("%d %d %d", &a, &b, &n)) {

int i, cur = a * n, d = b - a;

LL cnt = ok(cur);

c[0] = 1;

for (i = 1; i <= n; i++) {

rfact[i] = pow_mod(i, mod-2, mod);

c[i] = (LL)c[i - 1] * (n - i + 1) % mod;

c[i] = (LL)c[i] * rfact[i] % mod;

}

for (cur += d, i = 1; i < n; i++, cur += d) {

if (ok(cur)) {

cnt += c[i];

cnt %= mod;

}

}

cnt = (cnt + ok(b * n)) % mod;

printf("%lld\n", cnt);

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: