Codeforces 472G Design Tutorial: Increase the Constraints(分块+FFT)
2016-03-15 20:20
585 查看
#include <iostream> #include <cstdio> #include <cstring> #include <bits/stdc++.h> using namespace std; #define N 550020 #define LL long long #define MOD 1000000007 #define K 3 #define G 3 const int B = 6000; int n, m; char s , t ; int len; int rev ; const double Pi = acos(-1.0); struct cd { double r, i; cd() {} cd(double r, double i):r(r), i(i) {} cd operator + (const cd &b) const { return cd(r + b.r, i + b.i); } cd operator - (const cd &b) const { return cd(r - b.r, i - b.i); } cd operator * (const cd &b) const { return cd(r * b.r - i * b.i, i * b.r + r * b.i); } }x1 , x2 ; void change(cd y[], int len) { for(int i = 1; i < len; ++i) { rev[i] = (rev[i>>1] >> 1) + (i & 1) * (len >> 1); if(i < rev[i]) swap(y[i], y[rev[i]]); } } void FFT(cd y[], int len, int on) { change(y, len); for(int h = 2; h <= len; h <<= 1) { cd wn(cos(on * 2 * Pi / h), sin(on * 2 * Pi / h)); for(int j = 0; j < len; j += h) { cd w(1, 0); for(int k = j; k < j + h / 2; ++k) { cd u = y[k]; cd t = w * y[k+h/2]; y[k] = u + t; y[k+h/2] = u - t; w = w * wn; } } } if(on == -1) { for(int i = 0; i < len; ++i) y[i].r /= len; } } void debug() { cd x1[4] = {cd(-1, 0), cd(-1, 0)}; cd x2[4] = {cd(1, 0), cd(1, 0)}; FFT(x1, 4, 1); FFT(x2, 4, 1); for(int i = 0; i < 4; ++i) x1[i] = x1[i] * x2[i]; FFT(x1, 4, -1); for(int i = 0; i < 4; ++i) printf("%.3lf ", floor(x1[i].r + 0.5)); puts(""); } short int cnt[200020/B] ; int main() { scanf("%s%s", s, t); n = strlen(s); m = strlen(t); len = 1; while(len <= n + m) len <<= 1; for(int i = 0; i + B < n; i += B) { for(int j = 0; j < len; ++j) { if(j < m) { int v = t[j] == '1'? 1: -1; x2[m-j-1] = cd(v, 0); } else x2[j] = cd(0, 0); if(j >= i && j < i + B) { int v = s[j] == '1'? 1: -1; x1[j] = cd(v, 0); } else x1[j] = cd(0, 0); } FFT(x1, len, 1); FFT(x2, len, 1); for(int j = 0; j < len; ++j) x1[j] = x1[j] * x2[j]; FFT(x1, len, -1); for(int j = 0; j < len; ++j) cnt[i/B][j] = (int)floor(x1[j].r + 0.5); } int q; scanf("%d", &q); while(q--) { int a, b, x; scanf("%d%d%d", &a, &b, &x); int l = a, r = a + x - 1; int u = l / B, v = r / B; int ans = 0; if(u == v) { for(int i = 0; i < x; ++i) { if(s[i+a] != t[b+i]) ans++; } } else { for(int i = l % B, j = 0; i < B; ++i, ++j) { if(s[a+j] != t[b+j]) ++ans; } for(int i = 0, j = r % B; i <= r % B; ++i, --j) if(s[r-j] != t[b+x-1-j]) ++ans; for(int k = u + 1; k < v; ++k) { ans += (B - cnt[k][a+m-b-1]) / 2; } } printf("%d\n", ans); } return 0; }
相关文章推荐
- HDU.1023 Train Problem II【大数除法、卡特兰数】--用于求出栈的n种方式(3.15)
- 属性readwrite,readonly,assign,retain,copy,nonatomic 各是什么作用,在那种情况下用
- Error:Execution failed for task ':processDebugResources'.
- HDFS API文件上传报错 Exception in thread "main" .......: Wrong FS: file:///
- SGI STL (7) :: why stl containers have their own iterators?
- sakai作业系统
- HDU 2473 Junk-Mail Filter(并查集删点)
- http://blog.csdn.net/pizi0475/article/details/7768597
- 220. Contains Duplicate III
- PowerDesigner打开设计文件后提示failed to read the fileXXX的解决办法
- Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATI
- B - Pairs Forming LCM——(LightOJ 1236)
- mybaits中的<trim>标签的作用
- sakai创建工具的过程,以roster2为例
- IBM openblockchain学习(一)--obc-peer环境搭建
- 219. Contains Duplicate II
- sakai和jcloud操作流程
- 20160315 Sail Studio 2244 剪格子,2324 N^3,2230 K好数
- 配置Git的user name和email