【Nowcoder】暑期多校day6 Generation I （组合数学）

解题思路

ans=i=1min(n,m)Ci1n1Ci1mi!=i=1min(n,m)m!(mi)!(n1)!(ni)!inv(i!) a n s = ∑ i = 1 m i n ( n , m ) C n − 1 i − 1 ∗ C m i − 1 ∗ i ! = ∑ i = 1 m i n ( n , m ) m ! ( m − i ) ! ∗ ( n − 1 ) ! ( n − i ) ! ∗ i n v ( i ! )

代码

#include <bits/stdc++.h>
using namespace std;

register int val=0, sign=1; char ch;
while(~(ch=getchar()) && (ch<'0' || ch>'9') && ch!='-'); ch=='-'?sign=-1:val=ch-'0';
while(~(ch=getchar()) && (ch>='0' && ch<='9')) val=(val<<1)+(val<<3)+ch-'0';
return val*sign;
}

const int maxn=int(1e6)+5, moder=998244353;
inline int mul(const int &a,const int &b) {return 1ll*a*b%moder;}
inline int add(const int &a,const int &b) {return (a+b<moder)?a+b:a+b-moder;}

long long n,m;
int fac[maxn], inv[maxn];

int fpow(int a,int k) {
int res=1;
for(;k;k>>=1,a=mul(a,a)) if(k&1) res=mul(res,a);
return res;
}
void init() {
register int i; fac[0]=1;
for(i=1;i<maxn;++i) fac[i]=mul(fac[i-1],i);
for(i=0;i<maxn;++i) inv[i]=fpow(fac[i],moder-2);
return;
}

void work() {
cin>>n>>m;
register int i;
int ans=m%moder, tmp1=1, tmp2=m%moder;
for(i=2;i<=min(n,m);++i) {
tmp2=mul(tmp2,(m-i+1)%moder), tmp1=mul(tmp1,(n-i+1)%moder);
}
cout<<ans;
return;
}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
double t1=clock();
#endif
init();
for(int i=1;i<=cas;++i) {
printf("Case #%d: ",i);
work();
putchar('\n');
}

printf("%.3lfsec\n",(clock()-t1)/CLOCKS_PER_SEC);
return 0;
}

07-18 611
07-13 659
08-01 306