Light OJ 1067 Combinations(逆元)
来源:互联网 发布:淘宝小号怎么养 编辑:程序博客网 时间:2024/05/18 21:50
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;
分析:这道题得逆元来求,由于mod是质数,我们转化成扩展欧几里德和费马小定理求
1:要求b%mod的逆元,只需exgcd(b,mod,x,y)带入,因为同时在两边模mod得bx=1(%mod)所以x就是
b%mod的逆元。
2:要求b%mod的逆元,直接费马小定理,b'=b^(mod-2)%mod,所以可以直接快速幂来求一下就好了。
代码一:
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int INF=0x3f3f3f3f;typedef long long LL;const int mod=1e6+3;const int maxn=1e6+10;LL fac[maxn],inv[maxn];int n;void exgcd(LL a,LL b,LL &xx,LL &yy){ if(b==0) { xx=1;yy=0; return; } exgcd(b,a%b,xx,yy); LL t=xx;xx=yy;yy=t-a/b*yy; return;}int main(){ LL x,y; fac[0]=1;inv[0]=1; for(LL i=1;i<=1000000;i++) { fac[i]=fac[i-1]*i%mod; exgcd(fac[i],mod,x,y); inv[i]=(x%mod+mod)%mod; } int cas=1;int a,b; scanf("%d",&n); while(n--) { scanf("%d%d",&a,&b); LL ans=fac[a]*inv[b]%mod*inv[a-b]%mod; printf("Case %d: %lld\n",cas++,ans); } return 0;}
代码二:
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int INF=0x3f3f3f3f;typedef long long LL;const int mod=1e6+3;const int maxn=1e6+100;LL fac[maxn],inv[maxn];LL Pow(LL a,LL b){ LL ans=1; while(b) { if(b&1) ans=(ans*a)%mod; a=(a*a)%mod; b>>=1; } return ans;}int main(){ int cas=1; int n,a,b; fac[0]=inv[0]=1; for(int i=1;i<=1000000;i++) { fac[i]=(fac[i-1]*i)%mod; inv[i]=Pow(fac[i],mod-2); } scanf("%d",&n); while(n--) { scanf("%d%d",&a,&b); LL ans=fac[a]*inv[b]%mod*inv[a-b]%mod; printf("Case %d: %lld\n",cas++,ans); } return 0;}
- Light OJ 1067 Combinations(逆元)
- Light OJ 1067 Combinations (乘法逆元)
- LIGHT OJ-1067 Combinations【逆元(欧拉)+快速幂+同余】
- Light OJ 1067 Combinations
- LIght oj 1067 Combinations
- light oj 1067Combinations
- 【Lifht-oj】-1067-Combinations(Lucas定理&逆元)
- Light OJ 1067 Combinations (费小马定理求逆元)
- light oj 1067 Combinations (组合数的lucas定理)
- 【LightOJ】1067 - Combinations(Lucas & 逆元)
- light oj 1102 Problem Makes Problem [组合数+逆元]
- LightOJ - 1067 Combinations(快速幂+逆元)
- lightoj 1067 - Combinations (组合数、乘法逆元)
- Combinations 乘法逆元
- light oj 1067 组合数取模
- light oj(简化复杂度)
- light oj
- light oj
- 二叉查找树基本操作
- 1009. 说反话 (20)
- VLAN与Access、Trunk端口
- Android 添加沉浸式
- 出走三上海篇
- Light OJ 1067 Combinations(逆元)
- 发布到tomcat服务器上webapps里的文件名和workspace中项目名称不一致的问题
- linux 按文本内容查找所在的文件
- 0711 利用周六的时间好好充电
- sprintf函数的用法
- 视频摘要与检索
- POJ 2551 Ones(数论)
- android jni实例讲解
- Java基础