转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove

题目:给出n个数,选出三个数,按下标顺序形成等差数列

http://www.codechef.com/problems/COUNTARI

如果只是形成 等差数列并不难,大概就是先求一次卷积,然后再O(n)枚举,判断2 * a[i]的种数,不过按照下标就不会了。

有种很矬的,大概就是O(n)枚举中间的数,然后 对两边分别卷积,O(n * n * lgn)。

如果能想到枚举中间的数的话,应该可以进一步想到分块处理。

如果分为K块

那么分为几种情况 :

三个数都是在当前块中,那么可以枚举后两个数,查找第一个数,复杂度O(N/K * N/K)

两个数在当前块中,那么另外一个数可能在前面,也可能在后面,同理还是枚举两个数,查找,复杂度
O(N/K * N/K)

如果只有一个数在当前块中,那么就要对两边的数进行卷积,然后枚举当前块中的数,查询2 × a[i]。复杂度O(N * lg N)

那么总体就是O(k * (N/K * N/K + N * lg N))。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
//FFT copy from kuangbin
const double pi = acos (-1.0);
// Complex z = a + b * i
struct Complex {
double a, b;
Complex(double _a=0.0,double _b=0.0):a(_a),b(_b){}
Complex operator + (const Complex &c) const {
return Complex(a + c.a , b + c.b);
}
Complex operator - (const Complex &c) const {
return Complex(a - c.a , b - c.b);
}
Complex operator * (const Complex &c) const {
return Complex(a * c.a - b * c.b , a * c.b + b * c.a);
}
};
//len = 2 ^ k
void change (Complex y[] , int len) {
for (int i = 1 , j = len / 2 ; i < len -1 ; i ++) {
if (i < j) swap(y[i] , y[j]);
int k = len / 2;
while (j >= k) {
j -= k;
k /= 2;
}
if(j < k) j += k;
}
}
// FFT
// len = 2 ^ k
// on = 1 DFT on = -1 IDFT
void FFT (Complex y[], int len , int on) {
change (y , len);
for (int h = 2 ; h <= len ; h <<= 1) {
Complex wn(cos (-on * 2 * pi / h), sin (-on * 2 * pi / h));
for (int j = 0 ; j < len ; j += h) {
Complex w(1 , 0);
for (int k = j ; k < j + h / 2 ; k ++) {
Complex u = y[k];
Complex 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].a /= len;
}
}
}
const int N = 100005;
typedef long long LL;
int n , a[N];
int block , size;
LL num[N << 2];
int min_num = 30000 , max_num = 1;
int before[N] = {0}, behind[N] = {0} , in[N] = {0};
Complex x1[N << 2] ,x2[N << 2];
int main () {
#ifndef ONLINE_JUDGE
freopen("input.txt" , "r" , stdin);
#endif
scanf ("%d", &n);
for (int i = 0 ; i < n ; ++ i) {
scanf ("%d", &a[i]);
behind[a[i]] ++;
min_num = min (min_num , a[i]);
max_num = max (max_num , a[i]);
}
LL ret = 0;
block = min(n , 35);
size = (n + block - 1) / block;
for (int t = 0 ; t < block ; t ++) {
int s = t * size , e = (t + 1) * size;
if (e > n) e = n;
for (int i = s ; i < e ; i ++) {
behind[a[i]] --;
}
for (int i = s ; i < e ; i ++) {
for (int j = i + 1 ; j < e ; j ++) {
int m = 2 * a[i] - a[j];
if(m >= 1 && m <= 30000) {
// both of three in the block
ret += in[m];
// one of the number in the pre block
ret += before[m];
}
m = 2 * a[j] - a[i];
if (m >= 1 && m <= 30000) {
// one of the number in the next block
ret += behind[m];
}
}
in[a[i]] ++;
}
// pre block , current block , next block
if (t > 0 && t < block - 1) {
int l = 1;
int len = max_num + 1;
while (l < len * 2) l <<= 1;
for (int i = 0 ; i < len ; i ++) {
x1[i] = Complex (before[i] , 0);
}
for (int i = len ; i < l ; i ++) {
x1[i] = Complex (0 , 0);
}
for (int i = 0 ; i < len ; i ++) {
x2[i] = Complex (behind[i] , 0);
}
for (int i = len ; i < l ; i ++) {
x2[i] = Complex (0 , 0);
}
FFT (x1 , l , 1);
FFT (x2 , l , 1);
for (int i = 0 ; i < l ; i ++) {
x1[i] = x1[i] * x2[i];
}
FFT (x1 , l , -1);
for (int i = 0 ; i < l ; i ++) {
num[i] = (LL)(x1[i].a + 0.5);
}
for (int i = s ; i < e ; i ++) {
ret += num[a[i] << 1];
}
}
for (int i = s ; i < e ; i ++) {
in[a[i]] --;
before[a[i]] ++;
}
}
printf("%lld\n", ret);
return 0;
}

CC Arithmetic Progressions (FFT + 分块处理)的更多相关文章

  1. CodeChef COUNTARI Arithmetic Progressions(分块 + FFT)

    题目 Source http://vjudge.net/problem/142058 Description Given N integers A1, A2, …. AN, Dexter wants ...

  2. CodeChef - COUNTARI Arithmetic Progressions (FFT)

    题意:求一个序列中,有多少三元组$(i,j,k)i<j<k $ 满足\(A_i + A_k = 2*A_i\) 构成等差数列. https://www.cnblogs.com/xiuwen ...

  3. CodeChef Arithmetic Progressions

    https://www.codechef.com/status/COUNTARI 题意: 给出n个数,求满足i<j<k且a[j]-a[i]==a[j]-a[k] 的三元组(i,j,k)的个 ...

  4. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  5. Dirichlet's Theorem on Arithmetic Progressions 分类: POJ 2015-06-12 21:07 7人阅读 评论(0) 收藏

    Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  6. 洛谷P1214 [USACO1.4]等差数列 Arithmetic Progressions

    P1214 [USACO1.4]等差数列 Arithmetic Progressions• o 156通过o 463提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题 ...

  7. POJ 3006 Dirichlet's Theorem on Arithmetic Progressions (素数)

    Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  8. poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】

    题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...

  9. (素数求解)I - Dirichlet&#39;s Theorem on Arithmetic Progressions(1.5.5)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit cid=1006#sta ...

随机推荐

  1. Resharper 8.0 的最实用的功能

    在 8.0 里面.resharper 加入了 插件nuget方式 以前引用resharper插件.都是从新安装,复制进去,或者其他方式.比如resharper xunit  测试 我用xunit 插件 ...

  2. JavaScript注意事项

    var m = "false"; Boolean(m); // true而非 false ajax不能设置setRequestHeaders("Cookie", ...

  3. Linux2.6内核--内存管理(1)--分页机制

          在内核里分配内存可不像在其他地方分配内存那么容易.造成这种局面的因素很多.从根本上讲,是因为内核本身不能像用户空间那样奢侈的使用内存.内核与用户空间不同,它不具备这种能力,它不支持简单便捷 ...

  4. Hadoop FileInputFormat实现原理及源码分析

    FileInputFormat(org.apache.hadoop.mapreduce.lib.input.FileInputFormat)是专门针对文件类型的数据源而设计的,也是一个抽象类,它提供两 ...

  5. CentOS 6.4 安装 Fcitx4.0

    一.首先安装中文支持: su root yum install "@Chinese Support" exit yum remove ibus 注销再登陆 二.安装fcitx 下载 ...

  6. HDOJ(HDU) 2093 考试排名(Arrays.sort排序、类的应用)

    Problem Description C++编程考试使用的实时提交系统,具有即时获得成绩排名的特点.它的功能是怎么实现的呢? 我们做好了题目的解答,提交之后,要么"AC",要么错 ...

  7. MySQL数据库的安装

    官方下载地址: http://downloads.mysql.com/archives/community/ msi为安装版,zip为免安装版,最新版本的MySQL没有64位windows的msi版囧 ...

  8. java笔记11之二维数组

    格式1: 二维数组:就是元素为一维数组的一个数组 数据类型[][] 数组名 = new 数组类型[m][n] 其中m为行 n为列 注意: A:以下格式也可以表示二维数组            a:数据 ...

  9. UIAlertController 的使用(NS_CLASS_AVAILABLE_IOS(8_0)iOS8以后有效)

    iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController 在实现视图控制器间的过渡动画效果和自适应设备尺 ...

  10. Gmail邮件功能那么强大,GMail被封,在国内怎么用gmail收邮件?

    IT圈子里最热门的话题一定是:gmail被封,该怎么办?gmail由于强大的邮件功能,ITer一定是人手一个or多个,之前想要收发gmail使用imap或SMTP方式是可以在国内正常使用的,目前ima ...