Light OJ 1067 Combinations (乘法逆元)
Description
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
3
4 2
5 0
6 4
Sample Output
Case 1: 6
Case 2: 1
Case 3: 15
知识点:乘法逆元,扩展欧几里得。
题意:求C(n,m)%mod
难点:理解乘法逆元。
扩展:乘法逆元定义:如果a*b=1(mod n),那么b就是a关于模n的乘法逆元,此时,也可称作a与b互为乘法逆元。
思路:1.C(n,m)%mod=n!/m!*(n-m)!%mod除以一个数并取模等价于乘以这个数的逆元然后再去模.可将n!/m!*(n-m)!%mod等价于n!/m!%mod*1/(n-m)!%mod.再等价于n!*inv[m!]%mod(m!的逆元)*inv[(n-m)!]((n-m!)的逆元).
2.也就是说,求出逆元即可求解。c*x=1(mod n)=1-k*n等价于c*x+k*n=1所以可以用扩展欧几里得算法求得x(逆元)的值。为了保证x是正整数,通常需要加上:x=(x%n+n)%n.
#include<cstdlib>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define m 1000003
long long f[];
long long inv[];
long long x,y,gcd;
void extend_gcd(long long a, long long b)
{
if(b == )
{
x = ;
y = ;
gcd = a;
}
else {
extend_gcd(b, a%b);
long long temp = x;
x = y;
y = temp - a/b*y;
}
}
void factorial()
{
f[]=;inv[]=;
for(int i=;i<=;i++)
{
f[i]=f[i-]*i%m;
extend_gcd(f[i],m);
inv[i]=(x%m+m)%m;
}
}
int main()
{
factorial();
int t;
int cnt=;
scanf("%d\n",&t);
int a1,b1;
while(t--)
{
scanf("%d%d",&a1,&b1);
long long ans=f[a1]*inv[b1]%m*inv[a1-b1]%m;
printf("Case %d: %lld\n",++cnt,ans); } return ;
}
//3
//
//4 2
//
//5 0
//
//6 4 //3
//1 1
//2 3
//4 3
Light OJ 1067 Combinations (乘法逆元)的更多相关文章
- light oj 1067 费马小定理求逆元
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1067 1067 - Combinations Given n differen ...
- (light oj 1102) Problem Makes Problem (组合数 + 乘法逆元)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1102 As I am fond of making easier problems, ...
- BZOJ 4517--[Sdoi2016]排列计数(乘法逆元)
4517: [Sdoi2016]排列计数 Time Limit: 60 Sec Memory Limit: 128 MBSubmit: 1727 Solved: 1067 Description ...
- 1067 - Combinations
1067 - Combinations PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Giv ...
- Bzoj2154 Crash的数字表格 乘法逆元+莫比乌斯反演(TLE)
题意:求sigma{lcm(i,j)},1<=i<=n,1<=j<=m 不妨令n<=m 首先把lcm(i,j)转成i*j/gcd(i,j) 正解不会...总之最后化出来的 ...
- 51nod1256(乘法逆元)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1256 题意:中文题诶~ 思路: M, N 互质, 求满足 K ...
- 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数
1.gcd int gcd(int a,int b){ return b?gcd(b,a%b):a; } 2.扩展gcd )extend great common divisor ll exgcd(l ...
- HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)
原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...
- Codeforces 543D Road Improvement(树形DP + 乘法逆元)
题目大概说给一棵树,树的边一开始都是损坏的,要修复一些边,修复完后要满足各个点到根的路径上最多只有一条坏的边,现在以各个点为根分别求出修复边的方案数,其结果模1000000007. 不难联想到这题和H ...
随机推荐
- 关于UIButton中的ContentEdgeInsets的深入研究
UIButton的contentEdgeInsets属性的深入研究 由于用UIButton这个属性做过一些东西,但是对它的规律始终不太了解,虽然苹果官方文档的解释大体上可以理解为,这个属性设置的是内边 ...
- (HYSBZ)BZOJ 1588 营业额统计
营业额统计 Time Limit: 5000MS Memory Limit: 165888KB 64bit IO Format: %lld & %llu Description 营业额 ...
- C#枚举器接口IEnumerator的实现
原文(http://blog.csdn.net/phpxin123/article/details/7897226) 在C#中,如果一个类要使用foreach结构来实现迭代,就必须实现IEnumera ...
- jQuery中的trigger和triggerhandler区别
$("form :input").blur(function(){ // }).keyup(function(){ $(this).triggerHandler("blu ...
- jquery 图片比例不变,全屏居中
<!DOCTYPE html> <html> <head> <meta charset="gb2312"> <title> ...
- js数组去重,并统计最多项算法
从事了一段时间的前端开发,今天写了一个数组去重,并统计最多项的方法,目前只支持数组的项都是数字. 由于本人能力有限,希望能得到网友的指正!如有问题或者更好的实现思路,也欢迎大家和我讨论!代码如下: f ...
- ASP.NET 后台下载文件方法
void DownLoadFile(string fileName) { string filePath = Server.MapPath(fileName);//路径 //以字符流的形式下载文件 F ...
- 一些不熟悉的SQL脚本--约束条件
1.根据表名查询主键的SQL语句 SELECT D.COLUMN_NAME AS COLNAME FROM USER_CONS_COLUMNS D, USER_CONSTRAINTS M WHERE ...
- asp.net缓存(二)
ASP.NET页面局部缓存 有时缓存整个页面是不现实的,因为页的某些部分可能在每次请求时都需要变化.在这些情况下,只能缓存页的一部分.顾名思义,页面部分缓存是将页面部分内容保存在内存中以便响应用户请求 ...
- poj1111 DFS
J - 搜索 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:10000KB 64bit I ...