Topic Connection: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=26784
Topic:
Time Limit: 2000MS Memory Limit:32768KB 64bit IO Format:%lld &%llu
Description:
Given n different objects, you want to take K of them. How is many ways to can do it?
For example, say there is 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 the 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 and you have to print the result modulo 1000003.
Test instructions: To you n and K, from n different objects, choose K different, is to find the combination of C (n,k)%1000003;
Analysis:
Time is only 2 seconds, T Group test data plus N of 106 reached 109 recursive positive timeout, then consider the combination formula, C (n,k) =n!/(k!* (n-k)!); First hit a factorial table (of course, to take the mold, only 106), and then the division to take the model of the problem, of course, is to find the inverse, (A/b)%mod=a* (b to mod inverse), the inverse can be used to expand the European and Fermat theorem (can Ah, Baidu);
Code:
#include <cstdio>#include<iostream>#include<algorithm>using namespacestd;Const intmaxn=1e6+5, mod=1000003; typedefLong LongLL; LL FACT[MAXN];voidf () {fact[1]=fact[0]=1; for(intI=2; i<maxn;i++) Fact[i]= (LL) (i*fact[i-1])%MoD;} ll Niyuan (ll A,ll p)//It 's a quick power .{ if(p==0)return 1; LL x=niyuan (a,p/2); LL ans=x*x%MoD; if(p%2==1) ans=ans*a%MoD; returnans;} LL C (intNintk) {LL FM= (Fact[k]*fact[n-k])%MoD; LL ans1=niyuan (fm,mod-2);//Fermat theorem, it is good to ask for a power; return(Ans1*fact[n])%MoD;}intMain () {intT,n,k,kase=0; f ();//Play Tablescanf"%d",&t); while(t--) {scanf ("%d%d",&n,&k); if(k*2>N) k=n-k;//symmetry of combinatorial number, reduction of calculation;printf"Case %d:%lld\n",++kase,c (n,k)); } return 0;}View Code
Light OJ 1067 combined number modulus