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;
}
本来是道不难的题目,还是自己的数学功底不扎实。
从该题又一次巩固了关于乘法逆的概念,在剩余系中,如果要做除法,如 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;
}
相关文章推荐
- Mahout 中文分类 (1)
- 百度地图API的事件处理:覆盖物的如何阻止冒泡
- redhat 安装图形界面
- struts标签+jstl标签之国际化实例
- 查找CSS错误的完整步骤
- Android开发之切换activity动画overridePendingTransition
- STDIN_FILENO的作用及与stdin 的区别
- UVA 10396 - Vampire Numbers(暴力打表)
- 主设备号、次设备号
- Linux命令的组成规则及其全拼单词
- Source Insight 有用设置配置
- Nginx介绍及视频教程
- Azkaban 的作业类型插件
- Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
- 命令行程序选项解析函数—getopt
- linux双系统安装时可能会遇见的一些问题
- 浅谈foreach遍历的使用
- getopt_long 与 optind
- 加密算法
- SQLite 入门教程(四)增删改查,有讲究