[ABC323E] Playlist 题解
设
如果存在一首歌时长是
什么条件下才会在
除法可用费马小定理求乘法逆元实现。
放代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=998244353;
int qpow(int a,int b){
int r=1;
while(b){
if(b&1)r=r%mod*a%mod;
a=a%mod*a%mod; b>>=1;
}
return r;
}
int inv(int x){
return qpow(x,mod-2);
} // 费马小定理求逆元
main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n,x,c=0; cin>>n>>x;
vector<int> a(n),f(x+1);
for(auto &i:a)cin>>i;
int y=inv(n); f[0]=1; // 预处理
for(int i=0;i<=x;i++){ // 枚举时刻
for(int j=0;j<n;j++) // 枚举歌曲
if(i+a[j]<=x)(f[i+a[j]]+=f[i]*y%mod)%=mod; // 进行转移
if(i+a[0]>x)(c+=f[i]*y%mod)%=mod; // 累计答案
}
cout<<c<<endl;
return 0;
}