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 ...
随机推荐
- 04747_Java语言程序设计(一)_第7章_图形、图像与多媒体
例7.1小应用程序用6种字型显示字符串,显示内容说明本身的字型. import java.applet.*; import java.awt.*; public class Example7_1 ex ...
- JavaScript面向对象之类的继承
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 机器设备(dfs)
机器设备 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Alpha 公司设计出一种节能的机器设备.它的内部结构是由 N 个齿轮组成.整个机器设备有 一个驱动齿轮,当 ...
- storm源代码分析---Transactional spouts
Transactionalspouts Trident是以小批量(batch)的形式在处理tuple.而且每一批都会分配一个唯一的transaction id.不同spout的特性不同,一个trans ...
- [Javascript] property function && Enumeration
var vehicle3 = { type: "Submarine", capacity: 8, storedAt: "Underwater Outpost", ...
- jquery于form正在使用submit问题,未解决
$("#login_btn").click(function(){type为submit的button下 if($("#id_password").val(). ...
- 浅谈Android系统进程间通信(IPC)机制Binder中的Server和Client获得Service Manager接口之路
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6627260 在前面一篇文章浅谈Service ...
- phpStudy + JspStudy 2014.10.02 下载
phpStudy + JspStudy 2014.10.02 下载 目标:让天下没有难配的php环境. phpStudy Linux版&Win版同步上线 支持Apache/Nginx/Teng ...
- jquery返回顶部
// 返回顶部 var fixed_totop = $('.back_top').on('click',function(){ $('html, body').animate({scrollTop: ...
- 整合 新浪 腾讯 人人 qq空间 分享地址
function snsShare(snsId, title, content, image, url) { var snsUrl; // 新浪 腾讯 要申请appkey switch (snsId) ...