比赛 寒假集训2 评测结果 AAAAAAAAEEEEEEEEEEEE
题目名称 组合数问题 最终得分 40
用户昵称 终焉折枝 运行时间 2.036 s
代码语言 C++ 内存使用 8.60 MiB
提交时间 2026-02-25 11:51:24
显示代码纯文本
#include<iostream>
using namespace std;

#define ll long long
ll n, x, p, m, a[1005], c[1005][1005];

ll qpow(ll base, ll exp){
	ll res = 1;
	base %= p;
	while(exp > 0){
		if(exp & 1) res = res * base % p;
		base = base * base % p;
		exp >>= 1;
	}
	return res;
}

int main(){
    freopen("problem.in", "r", stdin);
    freopen("problem.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> x >> p >> m;
	for(int i = 0;i <= m;i ++) cin >> a[i];
	if(m == 0){
		ll ans = (a[0] % p) * qpow(x + 1, n) % p;
		cout << (ans + p) % p << '\n';
	}
    else{
		for(int i = 0;i <= n && i <= 1000;i ++){
			c[i][0] = 1;
			for(int j = 1;j <= i;j ++){
			    c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % p;
            }
		}
		ll ans = 0, xk = 1;
		for(int k = 0;k <= n;k ++){
			ll fk = 0;
			for(int i = m;i >= 0;i --){
			    fk = (fk * k + a[i]) % p;
            }
			ll cnt = fk * xk % p * c[n][k] % p;
			ans = (ans + cnt) % p;
			xk = xk * (x % p) % p;
		}
		cout << (ans + p) % p << '\n';
	}

	return 0;
}