P10613 [PA 2008] Cliquers题解

· · 题解

通过 1000 祭。

形式化题意

定义一个可重集 S 使得内部元素之和恰好等于 n 的方案数为 x,求 m^x \bmod P。

思路

我们设 S 中有值 S_i,目前 S 中数的和为 T。显然的,在 n \le 10^4 时,这就是一个朴素的 O(n^2) 背包 dp 。

但这题的数据范围是 n \le 2 \times 10^5,所以普通的背包肯定是做不了的,我们就考虑能否用某种方式将其拆成我们可以在规定复杂度内求出的子问题再利用组合数学算出总共的答案。

随后我们发现,如果有 S_i \ge \sqrt{n},那这样的数最多只有 \sqrt{n} 个,设 \sqrt{n} = B,所以可以将 S 内填入的数拆成两种:

  1. S_i \le B

不难看出这些数最多只有 B 种,T \le n,所以建立 f_i 为该情况下的背包方案,O(nB) 可以算出。

综上,

x = f_n + \sum_{i=1,\ j=1}^{i=\frac{n}{B},\ j=n} f_j \times g_{n - i \times B}

最后注意 x 可能很大,要取指数模,即对 x \bmod (P-1) 快速幂的时候再 \bmod P。

代码

#include<bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define lique00 0
#define jijijijijijii 1
#define Sunhaoqi1 {
#define mieval }
#define wangyichen_0711 ;
#define CRRC_Defoko (
#define Tobestranged )
#define hypq ,
#define yang123456789 %
#define XCZM_OYX61 =


namespace IO{
    #define CH getchar
    template <typename T>
    inline T read(){
        T f=1,x=0;char c=CH();
        while(c<'0'||c>'9'){if(c=='-')f=-1;c=CH();}
        while(c<='9'&&c>='0'){x=(x<<1)+(x<<3)+(c^'0');c=CH();}
        return x*f;
    }    
    #define PUT putchar
    template <typename T>
    inline void write(T x){
        if(x==0){PUT('0');return;}
        if(x<0){PUT('-');x=-x;}
        int sta[40],top=0;
        while(x)sta[top++]=x%10,x/=10;
        while(top)PUT(sta[--top]+'0');
    }
    template <typename T>
    inline void print(T x){write(x);PUT('\n');}
}

using namespace IO;
using namespace std;
const int N=2e5+5;
const int P=1e9-401;
ll m,n,B;
ll f[N],g[N];
ll ret;
ll qpow(ll a,ll b){
    ll ret=1;
    while(b){
        if(b&1)ret=ret*a%P;
        a=a*a%P;
        b>>=1;
    }
    return ret;
}
int main(){
    n=read<ll>(),m=read<ll>();
    B=sqrt(n);
    g[0]=f[0]=1;
    for(int i=1;i<B;i++)
        for(int j=i;j<=n;j++)f[j]=(f[j]+f[j-i])%(P-1);
    ret=f[n];
    for(int i=1;i<=n/B;i++){
        for(int j=i;j<=n;j++)g[j]=(g[j]+g[j-i])%(P-1);
        for(int j=0;j<=n-i*B;j++)ret=(ret+f[j]*g[n-i*B-j]%(P-1))%(P-1);
    }
    print<ll>(qpow(m,ret));
    return lique00;
}