Codeforces 300C

来源:互联网 发布:淘宝直通车尺寸 编辑:程序博客网 时间:2024/06/02 15:39

题目链接: 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;
}


0 0
原创粉丝点击