https://www.codechef.com/status/COUNTARI

题意:

给出n个数,求满足i<j<k且a[j]-a[i]==a[j]-a[k] 的三元组(i,j,k)的个数

n^2 做法:

枚举j和k,当j右移时,令sum[num[右移之前j的值]]++

每次统计sum[num[j]*2-num[k]]即可

如果没有i<j<k,直接上FFT

但是有了这个限制,可以枚举j,再FFT,复杂度为n*n*log(30000)

考虑一次FFT只算1个j有点儿浪费

能不能算好几个j?

分块!

设每一块的大小为S

答案分三种:

一、3个数都在一个块

用平方复杂度的做法,枚举同一块内的j和k,总时间复杂度为O(n/S*S*S)=O(n*S)

二、2个数在两个块

如果在同一块的数是j和k,从第一块开始枚举j和k,记录前面块的sum,累加sum[num[j]*2-num[k]]

如果在同一块的数是i和j,从最后一块开始枚举i和j,记录后面块的sum,累加sum[num[j]*2-num[i]]

总时间复杂度为O(n/S*S*S)=O(n*S)

三、3个数在三个块

枚举中间的的那一块,sumL记录这个块左边所有数,sumR记录这个块右边所有数

用FFT对sumL和sumR做一次卷积,得到sum

枚举中间那一块的每个数j,累加sum[num[j]*2]

FFT的一个小细节:

不能出现次数为0的项,所以所有数向左移一位,所以最后得到的sum向左移了两位,实际累加sum[num[j]*2-2]

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> using namespace std; #define N 100001
#define M 30001
#define S 500 const int K=(<<)+; typedef long long LL; const double pi=acos(-); int n,mx,a[N]; int l[M],r[M]; int len=,rev[K]; struct Complex
{
double x,y;
Complex(double x_=,double y_=):x(x_),y(y_){}
Complex operator + (Complex P)
{
return Complex(x+P.x,y+P.y);
}
Complex operator - (Complex P)
{
return Complex(x-P.x,y-P.y);
}
Complex operator * (Complex P)
{
return Complex(x*P.x-y*P.y,x*P.y+y*P.x);
}
};
typedef Complex E; E A[K],B[K]; LL ans; void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} void fft(E *a,int ty)
{
for(int i=;i<len;++i)
if(i<rev[i]) swap(a[i],a[rev[i]]);
for(int i=;i<len;i<<=)
{
E wn(cos(pi/i),ty*sin(pi/i));
for(int p=i<<,j=;j<len;j+=p)
{
E w(,);
for(int k=;k<i;++k,w=w*wn)
{
E x=a[j+k],y=a[j+k+i]*w;
a[j+k]=x+y; a[j+k+i]=x-y;
}
}
}
if(ty==-)
{
for(int i=;i<len;++i) a[i].x=a[i].x/len+0.5;
}
} void three()
{
int num=mx*-,bit=;
while(len<=num) len<<=,bit++;
for(int i=;i<len;++i) rev[i]=(rev[i>>]>>)|((i&)<<bit-);
for(int i=;i<=n;++i) r[a[i]]++;
int ed;
for(int t=;t<=n;t+=S)
{
ed=min(n,t+S-);
for(int i=t;i<=ed;++i) r[a[i]]--;
for(int i=;i<mx;++i) A[i].x=l[i+],A[i].y=;
for(int i=mx;i<len;++i) A[i].x=A[i].y=;
fft(A,);
for(int i=;i<mx;++i) B[i].x=r[i+],B[i].y=;
for(int i=mx;i<len;++i) B[i].x=B[i].y=;
fft(B,);
for(int i=;i<len;++i) A[i]=A[i]*B[i];
fft(A,-);
for(int i=t;i<=ed;++i) ans+=A[(a[i]<<)-].x;
for(int i=t;i<=ed;++i) l[a[i]]++;
}
memset(l,,sizeof(l));
} void two()
{
int ed;
for(int t=;t<=n;t+=S)
{
ed=min(n,t+S-);
for(int j=t;j<ed;++j)
for(int k=j+;k<=ed;++k)
if(a[j]<<>a[k] && (a[j]<<)-a[k]<=mx) ans+=l[(a[j]<<)-a[k]];
for(int i=t;i<=ed;++i) l[a[i]]++;
}
memset(l,,sizeof(l));
int t=,st;
while(t<n) t+=S;
t-=S;
for(int i=t+;i<=n;++i) r[a[i]]++;
for(;t>;t-=S)
{
st=t-S+;
for(int i=st;i<t;++i)
for(int j=i+;j<=t;++j)
if(a[j]<<>a[i] && (a[j]<<)-a[i]<=mx) ans+=r[(a[j]<<)-a[i]];
for(int i=st;i<=t;++i) r[a[i]]++;
}
} void one()
{
int ed;
for(int t=;t<=n;t+=S)
{
ed=min(t+S-,n);
for(int j=t;j<=ed;++j)
{
for(int k=j+;k<=ed;++k)
if(a[j]<<>a[k] && (a[j]<<)-a[k]<=mx) ans+=l[(a[j]<<)-a[k]];
l[a[j]]++;
}
for(int j=t;j<=ed;++j) l[a[j]]--;
}
} int main()
{
read(n);
for(int i=;i<=n;++i) read(a[i]),mx=max(mx,a[i]);
three();
two();
one();
cout<<ans;
}

CodeChef Arithmetic Progressions的更多相关文章

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

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

  2. 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 ...

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

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

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

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

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

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

  6. (素数求解)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 ...

  7. USACO 1.4 Arithmetic Progressions

    Arithmetic Progressions An arithmetic progression is a sequence of the form a, a+b, a+2b, ..., a+nb ...

  8. Educational Codeforces Round 16 D. Two Arithmetic Progressions (不互质中国剩余定理)

    Two Arithmetic Progressions 题目链接: http://codeforces.com/contest/710/problem/D Description You are gi ...

  9. 等差数列Arithmetic Progressions题解(USACO1.4)

    Arithmetic Progressions USACO1.4 An arithmetic progression is a sequence of the form a, a+b, a+2b, . ...

随机推荐

  1. 键盘事件(keyup、keydown、keypress)

    1.onkeyup 和onkeydown时,keyCode是不区分大小写的,会将小写字母自动转化为大写字母. 2 onkeypress时,区分大小写. 3兼容event.keyCode||event. ...

  2. ntpdate[31915]: the NTP socket is in use, exiting

    [root@master local]# ntpdate cn.pool.ntp.org 10 Oct 13:24:36 ntpdate[31915]: the NTP socket is in us ...

  3. msql 复杂练习

    https://blog.csdn.net/xiao__oaix/article/details/78122294 customer表branch 表account 表 depositor 表loan ...

  4. 机器学习中的降维算法:ISOMAP & MDS

    参见:https://blog.csdn.net/Dark_Scope/article/details/53229427

  5. awk、sed、grep三大shell文本处理工具之sed的应用

    sed 流编辑器 对文本中的行,逐行处理 非交互式的编辑器 是一个编辑器 1.工作流程 1)将文件的第一行读入到自己的缓存空间(模式空间--pattern space),删除掉换行符 2)匹配,看一下 ...

  6. Struts2 Intercepter 笔记

    以前一直对Struts2的自定义拦截器(intercepter)不是很清楚,今天仔细研究了下,终于搞懂了,现在记录对拦截器的总结如下: 1:自定义拦截器的作用一般就是用来实现系统权限控制比较多: 2: ...

  7. 学习笔记之form表单

    form表单提交的数据 是字典类型 这样 方便在create时候 直接解压

  8. jenkins--svn+添加钩子去触发jenkins的job工作

    找到svn钩子脚本 post-commit: 添加一个接口: /usr/bin/curl http://admin:admin@x.x.x.x:8080/job/svn/buildWithParame ...

  9. python系列-1 字符串操作

    1.去除空格   str.strip():删除字符串两边的指定字符,括号的写入指定字符,默认为空格 >>> a=' hello ' >>> b=a.strip() ...

  10. mysql 性能指标

    qps 每秒处理的查询数tps 每秒处理的事务数IOPS 每秒磁盘进行的I/O操作次数 一.TPS:Transactions Per Second(每秒传输的事物处理个数),即服务器每秒处理的事务数. ...