zoukankan      html  css  js  c++  java
  • BZOJ4332 JSOI2012 分零食 【倍增 + NTT】

    题目链接

    权限题BZOJ4332

    题解

    容易想到(dp)
    (g[i][j])表示前(i)人分到(j)颗糖的所有方案的乘积之和
    (f(x) = Ox^2 + Sx + U)

    [g[i][j] = sumlimits_{k = 1}^{j - 1}g[i - 1][k]f(j - k) ]

    是一个卷积的形式

    [g_n = f^{n} ]

    但我们的答案是

    [F_n = sumlimits_{i = 1}^{n} g_{i,m} ]

    有关系

    [F_n = F_x + F_{n - x}f^{x} ]

    考虑倍增

    [F_n = F_{frac{n}{2}} + F_{frac{n}{2}}f^{frac{n}{2}} ]

    (n)为奇数时,在多算一个(g_{n})(f^{n})即可

    复杂度(O(nlog^2n))

    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<cstdio>
    #include<vector>
    #include<queue>
    #include<cmath>
    #include<map>
    #define LL long long int
    #define REP(i,n) for (int i = 1; i <= (n); i++)
    #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
    #define cls(s,v) memset(s,v,sizeof(s))
    #define mp(a,b) make_pair<int,int>(a,b)
    #define cp pair<int,int>
    using namespace std;
    const int maxn = 40005,maxm = 100005,INF = 0x3f3f3f3f;
    inline int read(){
    	int out = 0,flag = 1; char c = getchar();
    	while (c < 48 || c > 57){if (c == '-') flag = 0; c = getchar();}
    	while (c >= 48 && c <= 57){out = (out << 1) + (out << 3) + c - 48; c = getchar();}
    	return flag ? out : -out;
    }
    const int G = 3,P = 998244353;
    int R[maxn],c[maxn];
    inline int qpow(int a,int b){
    	int re = 1;
    	for (; b; b >>= 1,a = 1ll * a * a % P)
    		if (b & 1) re = 1ll * re * a % P;
    	return re;
    }
    void NTT(int* a,int n,int f){
    	for (int i = 0; i < n; i++) if (i < R[i]) swap(a[i],a[R[i]]);
    	for (int i = 1; i < n; i <<= 1){
    		int gn = qpow(G,(P - 1) / (i << 1));
    		for (int j = 0; j < n; j += (i << 1)){
    			int g = 1,x,y;
    			for (int k = 0; k < i; k++,g = 1ll * g * gn % P){
    				x = a[j + k],y = 1ll * g * a[j + k + i] % P;
    				a[j + k] = (x + y) % P,a[j + k + i] = (x + P - y) % P;
    			}
    		}
    	}
    	if (f == 1) return;
    	int nv = qpow(n,P - 2); reverse(a + 1,a + n);
    	for (int i = 0; i < n; i++) a[i] = 1ll * a[i] * nv % P;
    }
    int md;
    void conv(int* a,int* b,int* t,int deg1,int deg2){
    	int n = 1,L = 0;
    	while (n <= (deg1 + deg2)) n <<= 1,L++;
    	for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
    	for (int i = 0; i <= deg1; i++) t[i] = a[i];
    	for (int i = deg1 + 1; i < n; i++) t[i] = 0;
    	for (int i = 0; i <= deg2; i++) c[i] = b[i];
    	for (int i = deg2 + 1; i < n; i++) c[i] = 0;
    	NTT(t,n,1); NTT(c,n,1);
    	for (int i = 0; i < n; i++) t[i] = 1ll * t[i] * c[i] % P;
    	NTT(t,n,-1);
    	for (int i = 0; i < n; i++) t[i] = t[i] % md;
    }
    int g[maxn],f[maxn],g1[maxn],tmp[maxn];
    int M,N,O,S,U;
    void work(int k){
    	if (k == 1){
    		for (int i = 0; i <= M; i++) f[i] = g[i] = g1[i];
    		return;
    	}
    	work(k >> 1);
    	conv(f,g,tmp,M,M); conv(g,g,g,M,M);
    	for (int i = 0; i <= M; i++)
    		f[i] = (f[i] + tmp[i]) % md;
    	if (k & 1){
    		conv(g,g1,g,M,M);
    		for (int i = 0; i <= M; i++)
    			f[i] = (f[i] + g[i]) % md;
    	}
    }
    int main(){
    	M = read(); md = read(); N = read();
    	O = read(); S = read(); U = read();
    	f[0] = g[0] = 1;
    	for (int i = 1; i <= M; i++)
    		g1[i] = (1ll * i * i * O % md + 1ll * i * S % md + U) % md;
    	work(N);
    	printf("%d
    ",f[M]);
    	return 0;
    }
    
    
  • 相关阅读:
    京东商城Java岗4面面经分享,(3轮技术+HR面已拿offer)
    选中input框禁止键盘弹出
    熟练使用artTemplate.helper()方法
    HTTP标头“Vary:Accept-Encoding”指定方法及其重要性分析
    浏览器缓存系列之三:设置浏览器缓存
    浏览器缓存系列之二:实战
    浏览器缓存系列之一
    使用递归算法实现树菜单
    为什么要用Docker
    高中生也能读懂的Docker入门教程
  • 原文地址:https://www.cnblogs.com/Mychael/p/9313208.html
Copyright © 2011-2022 走看看