这个题和 https://www.luogu.com.cn/problem/P6189 有啥区别吗?
考虑将连通分量按大小排序,则 为和为 的递增数组个数。
取 , 的数最多只有 个,可以刻画为加入 和全部加 1,设 为 个数和为 的方案数,则 , 暴力完全背包即可,注意指数的模数要减 1。
时间复杂度 。
#include<bits/stdc++.h>
#define rep(i,l,r) for(int i=(l),qwp=(r);i<=qwp;i++)
#define per(i,r,l) for(int i=(r),qwp=(l);i>=qwp;i--)
#define repll(i,l,r) for(ll i=(l),qwp=(r);i<=qwp;i++)
#define perll(i,r,l) for(ll i=(r),qwp=(l);i>=qwp;i--)
#define pb push_back
#define ins insert
#define clr clear
#define rem(S,X) (S).erase((S).find((X)))
#define uset unordered_set
#define umap unordered_map
#define mset multiset
using namespace std;
namespace ax_by_c{
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
constexpr ll mod=1e9-401;
constexpr int N=2e5+5;
constexpr int B=450;
ll ksm(ll a,ll b,ll p){a=a%p;ll r=1;while(b){if(b&1)r=r*a%p;a=a*a%p,b>>=1;}return r%p;}
int n,m,b,f[N],f_[N],_f[N];
void slv(int _csid,int _csi){
scanf("%d %d",&n,&m),b=sqrt(n);
f[0]=1;
rep(i,1,n/b){
rep(j,0,n){
_f[j]=(_f[j]+f[j])%(mod-1);
if(j>=b)f_[j]=f[j-b];
else f_[j]=0;
}
rep(j,i,n)f_[j]=(f_[j]+f_[j-i])%(mod-1);
rep(j,0,n)f[j]=f_[j];
}
rep(j,0,n)_f[j]=(_f[j]+f[j])%(mod-1);
rep(i,1,b-1)rep(j,i,n)_f[j]=(_f[j]+_f[j-i])%(mod-1);
printf("%lld\n",ksm(m,_f[n],mod));
}
void main(){
// ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int T=1,csid=-1;
// scanf("%d",&csid);
// scanf("%d",&T);
// scanf("%d",&csid);
rep(i,1,T)slv(csid,i);
}
}
int main(){
string __name="";
if(__name!=""){
freopen((__name+".in").c_str(),"r",stdin);
freopen((__name+".out").c_str(),"w",stdout);
}
ax_by_c::main();
return 0;
}
/*
g++ -std=c++14 -O2 -Wall -Wextra "-Wl,--stack=200000000" A.cpp -o A.exe
A.exe
*/