设为首页 加入收藏

TOP

BZOJ3509: [CodeChef] COUNTARI(生成函数 分块)(一)
2019-03-14 10:07:58 】 浏览:87
Tags:BZOJ3509: CodeChef COUNTARI 生成函数分块

题意

链接

Sol

这都能分块。。。。

首先移一下项,变为统计多少\(i < j < k\),满足\(2a[j] = a[i] + a[k]\)

发现\(a[i] \leqslant 30000\),那么有一种暴力思路是枚举\(j\),对于之前出现过的数构造一个生成函数,对于之后出现过的数构造一个生成函数,求一下第\(2a[j]\)项的值。复杂度\(O(nVlogV)\)

每次枚举\(j\)暴力卷积显然太zz了,我们可以分一下块,对于每一块之前之后的数分别构造生成函数暴力卷积算,对于块内的直接暴力(这里的暴力不只是统计块内的\((i, j, k)\),还要考虑\(j, k\)在块内\(i\)在块外,以及\(i, j\)在块内,\(k\)在块外的情况,但都是可以暴力的)

如果分成\(B\)块的话,复杂度是\(\frac{N}{B} VlogV + B^2\),假设\(n\)\(V\)同阶的话,\(B\)大概取\(nlogn\)是最优的。此时复杂度为\(O(N \sqrt{Nlogn})\)

下面的代码在原BZOJ上可能会T

#include<bits/stdc++.h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define LL long long 
#define ull unsigned long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e6 + 10, INF = 1e9 + 1;
const double eps = 1e-9, pi = acos(-1);
inline int read() {
    char c =
		    

getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } namespace Poly { int rev[MAXN], GPow[MAXN], A[MAXN], B[MAXN], C[MAXN], lim, INV2; const int G = 3, mod = 1004535809, mod2 = 1004535808; template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;} template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);} template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;} template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;} template <typename A, typename B> inline bool chmax(A &x, B y) {return x < y ? x = y, 1 : 0;} template <typename A, typename B> inline bool chmin(A &x, B y) {return x > y ? x = y, 1 : 0;} int fp(int a, int p, int P = mod) { int base = 1; for(; p > 0; p >>= 1, a = 1ll * a * a % P) if(p & 1) base = 1ll * base * a % P; return base; } int inv(int x) { return fp(x, mod - 2); } int GetLen(int x) { int lim = 1; while(lim < x) lim <<= 1; return lim; } void Init(/*int P,*/ int Lim) { INV2 = fp(2, mod - 2); for(int i = 1; i <= Lim; i++) GPow[i] = fp(G, (mod - 1) / i); } void NTT(int *A, int lim, int opt) { int len = 0; for(int N = 1; N < lim; N <<= 1) ++len; for(int i = 1; i <= lim; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (len - 1)); for(int i = 0; i <= lim; i++) if(i < rev[i]) swap(A[i], A[rev[i]]); for(int mid = 1; mid < lim; mid <<= 1) { int Wn = GPow[mid << 1]; for(int i = 0; i < lim; i += (mid << 1)) { for(int j = 0, w = 1; j < mid; j++, w = mul(w, Wn)) { int x = A[i + j], y = mul(w, A[i + j + mid]); A[i + j] = add(x, y), A[i + j + mid] = add(x, -y); } } } if(opt == -1) { reverse(A + 1, A + lim); int Inv = fp(lim, mod - 2); for(int i = 0; i <= lim; i++) mul2(A[i], Inv); } } void Mul(int *a, int *b, int N, int M) { memset(A, 0, sizeof(A)); memset(B, 0, sizeof(B)); int lim = 1, len = 0; while(lim <= N + M) len++, lim <<= 1; for(int i = 0; i <= N; i++) A[i] = a[i]; for(int i = 0; i <= M; i++) B[i] = b[i]; NTT(A, lim, 1); NTT(B, lim, 1); for(int i = 0; i <= lim; i++) B[i] = mul(B[i], A[i]); NTT(B, lim, -1); for(int i = 0; i <= N + M; i++) b[i] = B[i]; memset(A, 0, sizeof(A)); memset(B, 0, sizeof(B)); } }; using namespace Poly; int N, a[MAXN], mx, block, ll[MAXN], rr[MAXN], bel
编程开发网
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇#leetcode刷题之路21-合并两个有.. 下一篇012章 绪论+向量

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

array(4) { ["type"]=> int(8) ["message"]=> string(24) "Undefined variable: jobs" ["file"]=> string(32) "/mnt/wp/cppentry/do/bencandy.php" ["line"]=> int(217) }