Arithmetic Progressions

Given N integers A1, A2, …. AN, Dexter wants to know how many ways he can choose three numbers such that they are three consecutive terms of an arithmetic progression.

Meaning that, how many triplets (i, j, k) are there such that 1 ≤ i < j < k ≤ Nand Aj - Ai = Ak - Aj.

So the triplets (2, 5, 8), (10, 8, 6), (3, 3, 3) are valid as they are three consecutive terms of an arithmetic
progression. But the triplets (2, 5, 7), (10, 6, 8) are not.

Input

First line of the input contains an integer N (3 ≤ N ≤ 100000). Then the following line contains N space separated integers A1, A2, …, AN and they have values between 1 and 30000 (inclusive).

Output

Output the number of ways to choose a triplet such that they are three consecutive terms of an arithmetic progression.

Example

Input:
10
3 5 3 6 3 4 10 4 5 2 Output:
9

Explanation

The followings are all 9 ways to choose a triplet

1 : (i, j, k) = (1, 3, 5), (Ai, Aj, Ak) = (3, 3, 3)
2 : (i, j, k) = (1, 6, 9), (Ai, Aj, Ak) = (3, 4, 5)
3 : (i, j, k) = (1, 8, 9), (Ai, Aj, Ak) = (3, 4, 5)
4 : (i, j, k) = (3, 6, 9), (Ai, Aj, Ak) = (3, 4, 5)
5 : (i, j, k) = (3, 8, 9), (Ai, Aj, Ak) = (3, 4, 5)
6 : (i, j, k) = (4, 6, 10), (Ai, Aj, Ak) = (6, 4, 2)
7 : (i, j, k) = (4, 8, 10), (Ai, Aj, Ak) = (6, 4, 2)
8 : (i, j, k) = (5, 6, 9), (Ai, Aj, Ak) = (3, 4, 5)
9 : (i, j, k) = (5, 8, 9), (Ai, Aj, Ak) = (3, 4, 5)

题解:

    考虑分块,分成block块

    假设三个点都在同一块,那么我们就在一块内暴力,复杂度block * ( n/block)  * (n/block)

    假设其中两个点在同一块,那么枚举其中一块的两个点算答案,block * n/block * n/block

  ·  假设三个点都不在同一块,枚举中间点属于的那一块 剩下左边和右边进行 FFT, 复杂度block * (n*logn)

    

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 3e5+, M = 1e6+, mod = 1e9+,inf = 2e9; struct Complex {
double r , i ;
Complex () {}
Complex ( double r , double i ) : r ( r ) , i ( i ) {}
Complex operator + ( const Complex& t ) const {
return Complex ( r + t.r , i + t.i ) ;
}
Complex operator - ( const Complex& t ) const {
return Complex ( r - t.r , i - t.i ) ;
}
Complex operator * ( const Complex& t ) const {
return Complex ( r * t.r - i * t.i , r * t.i + i * t.r ) ;
}
} ; void FFT ( Complex y[] , int n , int rev ) {
for ( int i = , j , t , k ; i < n ; ++ i ) {
for ( j = , t = i , k = n >> ; k ; k >>= , t >>= ) j = j << | t & ;
if ( i < j ) swap ( y[i] , y[j] ) ;
}
for ( int s = , ds = ; s <= n ; ds = s , s <<= ) {
Complex wn = Complex ( cos ( rev * * pi / s ) , sin ( rev * * pi / s ) ) , w ( , ) , t ;
for ( int k = ; k < ds ; ++ k , w = w * wn ) {
for ( int i = k ; i < n ; i += s ) {
y[i + ds] = y[i] - ( t = w * y[i + ds] ) ;
y[i] = y[i] + t ;
}
}
}
if ( rev == - ) for ( int i = ; i < n ; ++ i ) y[i].r /= n ;
}
Complex s[N],t[N]; LL cnt[][];
int a[N];
int n,block,pos[N];
LL vis[N];
int main() {
while(scanf("%d",&n)!=EOF) {
block = ;
for(int i = ; i <= n; ++i)
pos[i] = (i-)/block + ;
int mx = -;
for(int i = ; i <= pos[n]; ++i)
for(int j = ; j <= ; ++j) cnt[i][j] = ;
for(int i = ; i <= n; ++i) {
scanf("%d",&a[i]);
mx = max(mx,a[i]);
cnt[pos[i]][a[i]]++;
} for(int i = ; i <= mx; ++i) {
for(int j = ; j <= pos[n]; ++j) {
cnt[j][i] += cnt[j-][i];
}
}
int len = ;
while(len <= *mx) len<<=;
LL ans = ;
for(int k = ; k <= pos[n]; ++k) {
for(int i = (k-)*block + ; i <= min(k*block,n); ++i) {
for(int j = i + ; j <= min(k*block,n); ++j) {
if(*a[i] - a[j] >= && *a[i] - a[j] <= mx)
ans += cnt[k-][*a[i] - a[j]] + vis[*a[i]-a[j]];
if(*a[j] - a[i] >= && *a[j] - a[i] <= mx)
ans += cnt[pos[n]][*a[j] - a[i]] - cnt[k][*a[j] - a[i]];
}
vis[a[i]] += ;
}
for(int i = (k-)*block + ; i <= min(k*block,n); ++i) {
vis[a[i]] = ;
} for(int j = ; j <= mx; ++j)
s[j] = Complex(cnt[k-][j],);
for(int j = mx+; j < len; ++j) s[j] = Complex(,); for(int j = ; j <= mx; ++j)
t[j] = Complex(cnt[pos[n]][j] - cnt[k][j] , );
for(int j = mx+; j < len; ++j) t[j] = Complex(,); FFT(s,len,);FFT(t,len,);
for(int j = ; j < len; ++j) s[j] = s[j] * t[j];
FFT(s,len,-); for(int j = ; j <= mx; ++j) {
LL tmp = (LL)(s[*j].r + 0.5);
ans += tmp*(cnt[k][j] - cnt[k-][j]);
}
}
printf("%lld\n",ans); }
return ;
}

  

CodeChef - COUNTARI FTT+分块的更多相关文章

  1. [BZOJ 3509] [CodeChef] COUNTARI (FFT+分块)

    [BZOJ 3509] [CodeChef] COUNTARI (FFT+分块) 题面 给出一个长度为n的数组,问有多少三元组\((i,j,k)\)满足\(i<j<k,a_j-a_i=a_ ...

  2. BZOJ3509 [CodeChef] COUNTARI 【分块 + fft】

    题目链接 BZOJ3509 题解 化一下式子,就是 \[2A[j] = A[i] + A[k]\] 所以我们对一个位置两边的数构成的生成函数相乘即可 但是由于这样做是\(O(n^2logn)\)的,我 ...

  3. bzoj 3509: [CodeChef] COUNTARI] [分块 生成函数]

    3509: [CodeChef] COUNTARI 题意:统计满足\(i<j<k, 2*a[j] = a[i] + a[k]\)的个数 \(2*a[j]\)不太好处理,暴力fft不如直接暴 ...

  4. BZOJ 3509: [CodeChef] COUNTARI

    3509: [CodeChef] COUNTARI Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 883  Solved: 250[Submit][S ...

  5. BZOJ3509: [CodeChef] COUNTARI

    3509: [CodeChef] COUNTARI Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 339  Solved: 85[Submit][St ...

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

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

  7. BZOJ 3509 [CodeChef] COUNTARI ——分块 FFT

    分块大法好. 块内暴力,块外FFT. 弃疗了,抄SX队长$silvernebula$的代码 #include <map> #include <cmath> #include & ...

  8. CodeChef FNCS (分块+树状数组)

    题目:https://www.codechef.com/problems/FNCS 题解: 我们知道要求区间和的时候,我们用前缀和去优化.这里也是一样,我们要求第 l 个函数到第 r 个函数 [l, ...

  9. CodeChef - COUNTARI Arithmetic Progressions (FFT)

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

随机推荐

  1. The BLOB and TEXT Types

    官网参考:https://dev.mysql.com/doc/refman/5.7/en/blob.html 字符串类型对应的存储需求 Data Type Storage Required CHAR( ...

  2. ASP.NET项目使用MYSQL数据库部署到IIS服务器找不到请求的.Net Framework Data Provider解决方案

    使用MySQL开发过程中在自己的机器上跑项目是没有问题的,但在实际部署到服务器上的时候就发生“找不到请求的.Net Framework Data Provider解决方案”错误,在排除项目本身原因之后 ...

  3. log日志,crontab

    定期备份mysql的log日志文件,保留一个月 将文件压缩为gzip格式,节省空间,备份到/home/shell/myqsl_back/目录下,保留一个月mysql_backup备份的脚本 #!/bi ...

  4. BZOJ 2286 [Sdoi2011]消耗战 ——虚树

    虚树第一题. 大概就是建一颗只与询问有关的更小的新树,然后在虚树上DP #include <map> #include <ctime> #include <cmath&g ...

  5. 将一个list均分成n个list

    /** * 将一个list均分成n个list,主要通过偏移量来实现的 * @param source * @return */ public <T> List<List<T&g ...

  6. 作业调度方案(codevs 1156)

    题目描述 Description 我们现在要利用m台机器加工n个工件,每个工件都有m道工序,每道工序都在不同的指定的机器上完成.每个工件的每道工序都有指定的加工时间. 每个工件的每个工序称为一个操作, ...

  7. mongodb的入门CURD

    mongodb的入门CURD #查看所有数据库show dbs;show databases; #有些版本可能不行 #使用数据库use 数据库名 #查看集合(集合即mysql的表)show table ...

  8. CKeditor如何实现图片上传功能

    http://makaiyuan.blog.51cto.com/5819595/1049521 如何在数据库中导入excel文件内的数据:http://jingyan.baidu.com/album/ ...

  9. LeetCode第一题以及时间复杂度的计算

    问题描述:给定一组指定整数数组,找出数组中加和等于特定数的两个数. 函数(方法)twoSum返回这两个数的索引,index1必须小于index2. 另外:你可以假设一个数组只有一组解. 一个栗子: I ...

  10. android应用开发之View的大小计量单位(px、dpi、dp、dip、sp)

    http://blog.csdn.net/ljianhui/article/details/43601495?ref=myread 一.像素(px)与屏幕分辨率 1)px(Pixels ,像素):对应 ...