light oj 1067 费马小定理求逆元
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1067
Given n different objects, you want to take k of them. How many ways to can do it?
For example, say there are 4 items; you want to take 2 of them. So, you can do it 6 ways.
Take 1, 2
Take 1, 3
Take 1, 4
Take 2, 3
Take 2, 4
Take 3, 4
Input
Input starts with an integer T (≤ 2000), denoting the number of test cases.
Each test case contains two integers n (1 ≤ n ≤ 106), k (0 ≤ k ≤ n).
Output
For each case, output the case number and the desired value. Since the result can be very large, you have to print the result modulo 1000003.
Sample Input |
Output for Sample Input |
3 4 2 5 0 6 4 |
Case 1: 6 Case 2: 1 Case 3: 15 |
分析:
时间只有2秒,T组测试数据加上n的106达到了109递推肯定超时,那么考虑组合公式,C(n,k)=n!/(k!*(n-k)!);先打一个阶乘的表(当然要取模,只有106),然后就是这个除法取模的问题,当然是求逆元,(a/b)%mod=a*(b对mod 的逆元);求逆元可以用扩欧和费马小定理。
费马小定理的使用条件mod必须为素数。
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
#define N 1000009
#define mod 1000003
#define LL long long
LL fact[N];
void init()
{
fact[0] = fact[1] = 1;
for(int i = 2; i < N; i++)
fact[i] = (fact[i-1] * i) % mod;
}
/*LL niyuan(int a, int p)
{
LL ans = 1;
if(p == 0)
return 1;
while(p)
{
if(p & 1)
ans = (ans * a) % mod;
a = (a * a) % mod;
p >>= 1;
}
return ans;
}*/
LL niyuan(int a, int b)///就是一个快速幂。
{
if(b == 0)
return 1;
LL x = niyuan(a, b / 2);
LL ans = x * x % mod;
if(b % 2 == 1)
ans = ans * a % mod;
return ans;
}
LL c(int n, int k)
{
LL fm = (fact[k] * fact[n-k]) % mod;///n! * (n-m)!.
LL ans1 = niyuan(fm, mod - 2);///求n!的逆元。
return (ans1 * fact[n]) % mod;///公式(a / b ) % mod = (a * a ^(mod-2) % mod。
}
int main(void)
{
int T, cas;
int n, k;
init();
scanf("%d", &T);
cas = 0;
while(T--)
{
cas++;
scanf("%d%d", &n, &k);
if(2 * k > n)///组合数性质。
k = n - k;
printf("Case %d: %lld\n", cas, c(n, k));
}
return 0;
}
最新文章
- 帝国CMS列表模板页面内容截取
- 被druid折磨的够呛
- Spring MVC防止数据重复提交
- Mapped Statements collection does not contain value for TaskMapper.selectByPrimaryKey
- Keil 的调试命令、在线汇编与断点设置
- ios策略模式应用
- django rest framework authentication
- 干掉safedog命令
- python3 十六进制字符串进行分割并累加
- QUARTZ系列之零:概述
- JuiceSSH使用教程: 玩转Linux与Windows
- NSOperation讲解
- MT【30】椭圆的第二定义解题
- java 三目运算符
- IntelliJ IDEA 启动tomcat服务器报Error running &#39;Unnamed&#39;: Address localhost:1099 is already in use错误的问题
- Educational Codeforces Round 14 D. Swaps in Permutation(并查集)
- linq在获取部门层级树种的应用
- 【linux】linux的数据流重定向
- windows使用Pandoc将Markdown转换为PDF文件
- C#学习——入门简介
热门文章
- VirtualBox扩充磁盘&;清空安装包
- java 储存机制
- Scrapy信号量
- OA系统、ERP系统、CRM系统的区别和联系有哪些?企业该如何使用?
- MySQL军规升级版(转)
- Creating Custom Helper Methods 创建自定义辅助器方法----辅助器方法 ------ 精通ASP.NET MVC 5
- 基于 HTML5 WebGL 的虚拟现实可视化培训系统
- laravel 工厂模式到容器
- 使用NetBenchmark压测TCP,HTTP和Websocket服务
- Linux(Centos)安装Java JDK及卸载