SHUOJ Arithmetic Sequence (FFT)
题意:求一个序列中,有多少三元组(其中元素不重复)在任意的排列下能构成等差数列。
分析:等差数列:\(A_j-A_i=A_k-A_j\),即\(2A_j=A_i+A_k\),枚举\(A_i+A_j\)的所有情况对应的个数,再扫一遍求解。
先统计出每个数对应的出现次数,FFT计算出和的组合情况。但是要减去\(A_i+A_i\)得到的结果以及\(A_i+A_j\)以及\(A_j+A_i\)重复的计算。
现在对于数\(A_j\),假设\(cnt=2*A_j\)的系数,当然cnt中要减去\(A_j\)本身和一个值与\(A_j\)相等的数组合而成的情况。枚举完这个数以后,把这个数从序列中抹去,因为这个数对结果做出的贡献已经计算,之后的统计中该数以及该数对结果的贡献不能重复计算。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 10;
const double PI = acos(-1.0);
struct Complex{
double x, y;
inline Complex operator+(const Complex b) const {
return (Complex){x +b.x,y + b.y};
}
inline Complex operator-(const Complex b) const {
return (Complex){x -b.x,y - b.y};
}
inline Complex operator*(const Complex b) const {
return (Complex){x *b.x -y * b.y,x * b.y + y * b.x};
}
} va[MAXN * 2 + MAXN / 2], vb[MAXN * 2 + MAXN / 2];
int lenth = 1, rev[MAXN * 2 + MAXN / 2];
int N, M; // f 和 g 的数量
//f g和 的系数
// 卷积结果
// 大数乘积
int f[MAXN],g[MAXN];
vector<LL> conv;
vector<LL> multi;
void debug(){for(int i=0;i<conv.size();++i) cout<<conv[i]<<" ";cout<<endl;}
//f g
void init()
{
int tim = 0;
lenth = 1;
conv.clear(), multi.clear();
memset(va, 0, sizeof va);
memset(vb, 0, sizeof vb);
while (lenth <= N + M - 2)
lenth <<= 1, tim++;
for (int i = 0; i < lenth; i++)
rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (tim - 1));
}
void FFT(Complex *A, const int fla)
{
for (int i = 0; i < lenth; i++){
if (i < rev[i]){
swap(A[i], A[rev[i]]);
}
}
for (int i = 1; i < lenth; i <<= 1){
const Complex w = (Complex){cos(PI / i), fla * sin(PI / i)};
for (int j = 0; j < lenth; j += (i << 1)){
Complex K = (Complex){1, 0};
for (int k = 0; k < i; k++, K = K * w){
const Complex x = A[j + k], y = K * A[j + k + i];
A[j + k] = x + y;
A[j + k + i] = x - y;
}
}
}
}
void getConv(){ //求多项式
init();
for (int i = 0; i < N; i++)
va[i].x = f[i];
for (int i = 0; i < M; i++)
vb[i].x = g[i];
FFT(va, 1), FFT(vb, 1);
for (int i = 0; i < lenth; i++)
va[i] = va[i] * vb[i];
FFT(va, -1);
for (int i = 0; i <= N + M - 2; i++)
conv.push_back((LL)(va[i].x / lenth + 0.5));
}
void getMulti() //求A*B
{
getConv();
multi = conv;
reverse(multi.begin(), multi.end());
multi.push_back(0);
int sz = multi.size();
for (int i = 0; i < sz - 1; i++){
multi[i + 1] += multi[i] / 10;
multi[i] %= 10;
}
while (!multi.back() && multi.size() > 1)
multi.pop_back();
reverse(multi.begin(), multi.end());
}
int a[MAXN];
int cnt[MAXN];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T; scanf("%d",&T);
while(T--){
int n; scanf("%d",&n);
int mx = -1;
memset(cnt,0,sizeof(cnt));
for(int i =1;i<=n;++i){
scanf("%d",&a[i]);
mx = max(mx,a[i]);
cnt[a[i]]++;
}
N = M = mx+1;
for(int i=0;i<N;++i){
f[i] = g[i] = cnt[i];
}
getConv();
int sz = conv.size();
for(int i=1;i<=n;++i){
conv[a[i]*2]--;
}
for(int i=0;i<sz;++i){
conv[i]>>=1;
}
LL res=0;
//debug();
//sort(a+1,a+n+1);
for(int i=1;i<=n;++i){
if(2*a[i]>=sz) continue;
LL tmp = conv[2*a[i]];
tmp -= cnt[a[i]]-1; //减去由自己构成的
conv[2*a[i]] -= cnt[a[i]]-1; //将Ai对结果的贡献抹去
cnt[a[i]]--; //将Ai从原序列中抹去
res += tmp;
}
printf("%lld\n",res);
}
return 0;
}
SHUOJ Arithmetic Sequence (FFT)的更多相关文章
- hdu 5400 Arithmetic Sequence
http://acm.hdu.edu.cn/showproblem.php?pid=5400 Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Ot ...
- hdu 5400 Arithmetic Sequence(模拟)
Problem Description A sequence b1,b2,⋯,bn are called (d1,d2)-arithmetic sequence ≤i≤n) such that ≤j& ...
- Arithmetic Sequence(dp)
Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 51 Solved: 19[Submit][Status][We ...
- [Swift]LeetCode1027. 最长等差数列 | Longest Arithmetic Sequence
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall t ...
- (模拟)Arithmetic Sequence -- HDU -- 5400
链接: http://acm.hdu.edu.cn/showproblem.php?pid=5400 Time Limit: 4000/2000 MS (Java/Others) Memory ...
- HZAU 21——Arithmetic Sequence——————【暴力 or dp】
Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1810 Solved: 311[Submit][Status] ...
- 华中农业大学第四届程序设计大赛网络同步赛-1020: Arithmetic Sequence,题挺好的,考思路;
1020: Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MB Submit: ->打开链接<- Descriptio ...
- LeetCode 1027. Longest Arithmetic Sequence
原题链接在这里:https://leetcode.com/problems/longest-arithmetic-sequence/ 题目: Given an array A of integers, ...
- 【leetcode】1027. Longest Arithmetic Sequence
题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Re ...
随机推荐
- VC++ 监视文件(夹)
参考:http://www.cnblogs.com/fangkm/archive/2009/03/31/1426526.html WinFileSystemMonitor.h C++ Code 1 ...
- poj_2823 单调队列
题目大意 给定一行数,共N个.有一个长度为K的窗口从左向右滑动,窗口中始终有K个数字,窗口每次滑动一个数字.求各个时刻窗口中的最大值和最小值. 题目分析 直接搜索,复杂度为O(n^2).考虑使用单调队 ...
- JZOJ.5307【NOIP2017模拟8.18】偷窃
Description
- c#基础 第六讲
烧开水 先询问:“是否要烧开水(Y/N)” 是的话执行--0°--100°(30°---水温了,50°---水热了,80°---水快开了,100°---水已经开了, 结束.) 判断 循环 选择 跳转 ...
- 【BZOJ2500】幸福的道路 树形DP+RMQ+双指针法
[BZOJ2500]幸福的道路 Description 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一同晨练来享受在一起的时光. 他们画出了晨练路线的草图,眼尖的 ...
- tomcat中文配置
tomcat传递中文乱码,修改server.xml文件 <Connector port=" protocol="HTTP/1.1" connectionTimeou ...
- Liunx新手入门必看
安装CentOS(Linux的一个常用发行版本,互联网公司经常使用这个发行版)用到的软件: VMware_workstation_full_12.5.2.exe 虚拟机软件,虚拟机由这个软件安装.管理 ...
- Pycharm取消默认的右击运行unittest方法
Pycharm取消默认的右击运行unittest方法:File-> Settings -> Tools -> Python Integrated Tools -> Defaul ...
- https://zh.cppreference.com 和 https://en.cppreference.com 和 https://cppcon.org/
https://zh.cppreference.comhttps://en.cppreference.com/w/ https://cppcon.org/
- LeetCode_Isomorphic Strings
Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are isom ...