比赛 2022级数学专题练习赛8 评测结果 AAAAAEEEEEEEEEEEEEEE
题目名称 组合数问题 最终得分 25
用户昵称 op_组撒头屯 运行时间 3.225 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2023-02-06 19:31:21
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int,int>
#define vi vector<int>
#define si set<int>
#define unsi unordered_set<int>
#define qi queue<int>
#define sti stack<int>
#define pqi priority_queue<int>
#define mii map<int,int>
#define unmii unordered_map<int,int>
#define fi first
#define se second
#define pb push_back
#define clr(f,n) memset(f,0,sizeof(int)*(n))
#define cpy(f,g,n) memcpy(f,g,sizeof(int)*(n))
const int N=1000+5;
const int M=100000+5;
ll n,s,mod,m;
ll a[N],c[N][N];
ll f(ll x){
    ll ans=0,p=1;
    for (int i=0;i<=m;i++){
        ans=(ans+a[i]*p%mod)%mod;
        p=p*x%mod;
    }
    return ans;
}
void solve1(){
    c[0][0]=1;
    for (int i=1;i<=n;i++){
        c[i][0]=1;
        for (int j=1;j<=i;j++)c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
    }ll ans=0,p=1;
    for (int i=0;i<=n;i++){
        ans=(ans+f(i)*p%mod*c[n][i]%mod)%mod;
        p=p*s%mod;
    }
    printf("%lld\n",ans);
    return ;
}
ll fac[M],inv[M];
ll fst(ll x,ll y){
    ll ans=1;
    while(y){
        if (y&1)ans=ans*x%mod;
        x=x*x%mod;y>>=1;
    }
    return ans;
}
ll C(int x,int y){
    return fac[x]*inv[y]%mod*inv[x-y]%mod;
}
ll lucas(ll x,ll y){
    if (x<y)return 0;
    if (!x)return 1;
    ll ans=0;
    if (x%mod>=y%mod)ans=C(x%mod,y%mod);
    return lucas(x/mod,y/mod)*ans%mod;
}
void solve2(){
    fac[0]=1;for (int i=1;i<mod;i++)fac[i]=fac[i-1]*i%mod;
    inv[mod-1]=fst(fac[mod-1],mod-2);
    for (int i=mod-2;i>=0;i--)inv[i]=inv[i+1]*(i+1)%mod;
    ll ans=0,p=1;
    for (int i=0;i<=n;i++){
        ans=(ans+a[0]*p%mod*lucas(n,i)%mod)%mod;
        p=p*s%mod;
    }
    printf("%lld\n",ans);
    return ;
}
int main(){
	freopen ("haoi2020_problem.in","r",stdin);
	freopen ("haoi2020_problem.out","w",stdout);
	scanf("%lld%lld%lld%lld",&n,&s,&mod,&m);
	for (int i=0;i<=m;i++)scanf("%lld",&a[i]);
	if (n<=1000&&m<=1000)solve1();
	else solve2();
}