题解:【五校联考2day2】WYF的盒子
B - [WYF 的盒子](【2025.10.14NOIP】WYF 的盒子 (Standard IO))
题目大意:
求
没了,就这么简单!
思路:
关键在于,这道题,你暴力绝对过不去!
就是恶心你。
然后,就必须要用第二类斯特林数这种更恶心的东西。
首先钦定
显然:
$$ x^n=\sum_{k}{n\brace k}x^{\underline k} $$ $(3)$ 转化为连乘。 $(4)$ 将连乘转化为组合数。将 $j!\times\binom{i}{j}$ 拆开可知。 $(6)$ 因 $\binom{n}{m}=\binom{n-1}{m-1}+\binom{n-1}{m}$,所以 $\sum^{n}_{i=j}\binom{i}{j}$ 即可发现该结论。 $(8)$ 因为 $\prod^{n+1}_{p=n+1-j}p$ 共是连续 $j+1$ 个数的乘积,所以 $\prod^{n+1}_{p=n+1-j}p$ 必定能整除 $j+1$。
公式推理完毕。
现在考虑怎么求
很简单,上述在
由于本题的特殊性质,我们可以用上述方法求得
\mathcal Code:
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){int x = 0; bool f = 1; char ch = getchar();while (ch < '0' || ch > '9') { if (ch == '-') f = 0; ch = getchar(); }while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();return (f ? x : (-x));}
const int N=1e6+5;
__int128 num[2001][2001];
__int128 Pow(__int128 x,__int128 y,__int128 p){
__int128 res=1;
while(y){
if(y&1)res=res*x%p;
x=x*x%p;
y>>=1;
}
return res;
}
void work1(int n,int m,int k,int p){
__int128 ans=0;
for(int i=m;i<=n;i++)ans=(ans+Pow(i,k,p)%p)%p;
printf("%lld\n",(long long)ans);
}
void init(int k,int p){
num[0][0]=1;
for(int i=1;i<=k;i++)for(int j=1;j<=k;j++)num[i][j]=(num[i-1][j-1]+num[i-1][j]*j%p)%p;
}
__int128 work2(int n,int k,int p){
__int128 res=0;
for(int i=0;i<=k;i++){
__int128 ksum1=1;
for(int j=n+1-i;j<=n+1;j++)ksum1=(ksum1*(j%(i+1)==0?j/(i+1):j))%p;
res=(res+num[k][i]*ksum1)%p;
}
return res;
}
signed main(){
int k,n,m,p;
scanf("%lld%lld%lld%lld",&k,&n,&m,&p);
if(k>2000)work1(n,m,k,p);
else{
init(k,p);
printf("%lld\n",(long long)((work2(n,k,p)-work2(m-1,k,p)+p)%p));
}
return 0;
}
------------------ 我也是有底线的 -------------