結果
問題 | No.117 組み合わせの数 |
ユーザー | LayCurse |
提出日時 | 2015-01-03 22:28:39 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 61 ms / 5,000 ms |
コード長 | 2,029 bytes |
コンパイル時間 | 1,354 ms |
コンパイル使用メモリ | 161,392 KB |
実行使用メモリ | 65,980 KB |
最終ジャッジ日時 | 2024-06-13 02:14:28 |
合計ジャッジ時間 | 1,850 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:15:44: warning: ‘res’ may be used uninitialized in this function [-Wmaybe-uninitialized] 15 | template<class T> void writerLn(T x){writer(x,'\n');} | ~~~~~~^~~~~~~~ main.cpp:32:16: note: ‘res’ was declared here 32 | int T, N, K, res; | ^~~
ソースコード
#include<bits/stdc++.h>using namespace std;#define REP(i,a,b) for(i=a;i<b;i++)#define rep(i,n) REP(i,0,n)#define mygc(c) (c)=getchar_unlocked()#define mypc(c) putchar_unlocked(c)#define ll long longvoid reader(int *x){int k,m=0;*x=0;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){*x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);}template <class T, class S> void reader(T *x, S *y){reader(x);reader(y);}void writer(int x, char c){int s=0,m=0;char f[10];if(x<0)m=1,x=-x;while(x)f[s++]=x%10,x/=10;if(!s)f[s++]=0;if(m)mypc('-');while(s--)mypc(f[s]+'0');mypc(c);}template<class T> void writerLn(T x){writer(x,'\n');}#define MD 1000000007void extended_euclid(ll x,ll y,ll *c,ll *a,ll *b){ll d=1,e=0,f,g=0,h=1,i,j=x,k=y,l,q;while(k>0)q=j/k,l=j%k,f=d-q*e,i=g-q*h,j=k,k=l,d=e,e=f,g=h,h=i;*c=j;*a=d;*b=g;}ll get_inv(ll n, ll p){ll a,b,c;extended_euclid(n,p,&c,&a,&b);if(a<p)a+=p;return a%p;}struct combination{ll *f,*v,**d; int m,s;void* init(void *mem, int md, int lim=1000100, int small=0){int i,j;m=md;s=small;f=(ll*)mem;v=f+lim;d=(ll**)(v+lim);d[0]=(ll*)(d+s);REP(i,1,s)d[i]=d[i-1]+s;mem=(void*)(d[0]+s*s);f[0]=1;REP(i,1,lim)f[i]=f[i-1]*i%m;v[lim-1]=get_inv(f[lim-1],m);for(i=lim-1;i;i--)v[i-1]=v[i]*i%m;REP(i,1,s)d[0][i]=0;rep(i,s)d[i][0]=1;REP(i,1,s)REP(j,1,s){d[i][j]=d[i-1][j-1]+d[i-1][j];if(d[i][j]>=m)d[i][j]-=m;}return mem;}ll C(int a, int b){if(b<0||b>a)return 0;if(a<s)return d[a][b];return f[a]*v[b]%m*v[a-b]%m;}ll P(int a, int b){if(b<0||b>a)return 0;return f[a]*v[a-b]%m;}ll H(int a, int b){if(a==0&&b==0)return 1;if(a<=0||b<0)return 0;return C(a+b-1,b);}};int main(){int i, j, k;int T, N, K, res;char c;combination comb;void *mem = malloc(100000000);mem = comb.init(mem, MD, 2000100, 2000);reader(&T);while(T--){do{ mygc(c); }while(c <= ' ');reader(&N,&K);if(c=='C') res = comb.C(N,K);if(c=='P') res = comb.P(N,K);if(c=='H') res = comb.H(N,K);writerLn(res);}return 0;}