题目描述
阿杰在上班会数学课:“最近有些同学很浮躁……”
早已习惯的你,在想这样一个问题:
共有n种竞赛,对于其中任意i种竞赛(1≤i≤n),有ai个人同时参加,问有多少个人参加了至少一门竞赛?
阿杰还在滔滔不绝,你却陷入了深思。
题解
显然答案是
容斥
但是组合数会爆long long所以不能直接搞。高精写炸了我很烦躁,于是痛定思痛,其实我们会发现这个最终答案小于一开始的那一个值a[1]*n于是我们可以直接用a[i]*n当模数写就是了。
代码
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const LL mod=1000000007;
LL read(){
LL res,f=1; char c;
while(!isdigit(c=getchar())) if(c=='-') f=-1; res=(c^48);
while(isdigit(c=getchar())) res=(res<<3)+(res<<1)+(c^48);
return res*f;
}
LL Pow(LL x,LL k){
LL res=1;
while(k){
if(k&1) res=(res*x)%mod;
k>>=1;
x=(x*x)%mod;
}
return res;
}
LL ans,C,n;
int main(){
n=read();
C=1;
for(LL i=1;i<=n;i++){
C=(C*(n-i+1)%mod*Pow(i,mod-2)%mod)%mod;
ans=(ans+C*(i&1?1:-1)%mod*read()%mod)%mod;
}
printf("%lld\n",(ans+mod)%mod);
return 0;
}