FFT(快速傅里叶变换):HDU 4609 3-idiots
3-idiots
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3560 Accepted Submission(s): 1241
OMeGa catched three men who had been streaking in the street. Looking
as idiots though, the three men insisted that it was a kind of
performance art, and begged the king to free them. Out of hatred to the
real idiots, the king wanted to check if they were lying. The three men
were sent to the king's forest, and each of them was asked to pick a
branch one after another. If the three branches they bring back can form
a triangle, their math ability would save them. Otherwise, they would
be sent into jail.
However, the three men were exactly idiots, and
what they would do is only to pick the branches randomly. Certainly,
they couldn't pick the same branch - but the one with the same length as
another is available. Given the lengths of all branches in the forest,
determine the probability that they would be saved.
Each test case begins with the number of branches N(3≤N≤105).
The following line contains N integers a_i (1≤a_i≤105), which denotes the length of each branch, respectively.
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn=;
const long double PI=acos(-1.0);
struct complex{
long double r,i;
complex(long double r_=0.0,long double i_=0.0){
r=r_;i=i_;
}
complex operator +(complex &a){
return complex(a.r+r,a.i+i);
}
complex operator -(complex &a){
return complex(r-a.r,i-a.i);
}
complex operator *(complex a){
return complex(r*a.r-i*a.i,i*a.r+a.i*r);
}
}A[maxn]; void Rader(complex *a,int len){
for(int i=,j=len>>;i<len-;i++){
if(i<j)swap(a[i],a[j]);
int k=len>>;
while(j>=k){
j-=k;
k>>=;
}
j+=k;
}
} void FFT(complex *a,int len,int on){
Rader(a,len);
for(int h=;h<=len;h<<=){
complex wn(cos(-on*PI*/h),sin(-on*PI*/h));
for(int j=;j<len;j+=h){
complex w(1.0,);
for(int k=j;k<j+(h>>);k++){
complex x=a[k];
complex y=a[k+(h>>)]*w;
a[k]=x+y;
a[k+(h>>)]=x-y;
w=w*wn;
}
}
}
if(on==-)
for(int i=;i<len;i++)
a[i].r/=len;
}
int a[maxn];
long long num[maxn];
int main(){
#ifndef ONLINE_JUDGE
//freopen("","r",stdin);
//freopen("","w",stdout);
#endif
int T,n,len=;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
memset(A,,sizeof(A));
memset(num,,sizeof(num));
while(len<=)len<<=;
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
sort(a+,a+n+);len=;
while(len<=a[n]*)len<<=;
for(int i=;i<=n;i++)
A[a[i]].r++;
FFT(A,len,);
for(int i=;i<len;i++)
A[i]=A[i]*A[i];
FFT(A,len,-);
for(int i=;i<len;i++)
num[i]=(long long)(A[i].r+0.5);
for(int i=;i<=n;i++)
num[a[i]<<]--;
for(int i=;i<len;i++)
num[i]>>=;
for(int i=;i<len;i++)
num[i]+=num[i-];
long long cnt=;
for(int i=;i<=n;i++){
cnt+=num[len-]-num[a[i]];
cnt-=(long long)(n-i)*(i-);
cnt-=n-;
cnt-=(long long)(n-i)*(n-i-)/;
}
long long tot=((long long)n*(n-)*(n-))/;
printf("%.7lf\n",1.0*cnt/tot);
}
return ;
}
FFT(快速傅里叶变换):HDU 4609 3-idiots的更多相关文章
- FFT 快速傅里叶变换 学习笔记
FFT 快速傅里叶变换 前言 lmc,ikka,attack等众多大佬都没教会的我终于要自己填坑了. 又是机房里最后一个学fft的人 早背过圆周率50位填坑了 用处 多项式乘法 卷积 \(g(x)=a ...
- CQOI2018 九连环 打表找规律 fft快速傅里叶变换
题面: CQOI2018九连环 分析: 个人认为这道题没有什么价值,纯粹是为了考算法而考算法. 对于小数据我们可以直接爆搜打表,打表出来我们可以观察规律. f[1~10]: 1 2 5 10 21 4 ...
- 「学习笔记」FFT 快速傅里叶变换
目录 「学习笔记」FFT 快速傅里叶变换 啥是 FFT 呀?它可以干什么? 必备芝士 点值表示 复数 傅立叶正变换 傅里叶逆变换 FFT 的代码实现 还会有的 NTT 和三模数 NTT... 「学习笔 ...
- FFT —— 快速傅里叶变换
问题: 已知A[], B[], 求C[],使: 定义C是A,B的卷积,例如多项式乘法等. 朴素做法是按照定义枚举i和j,但这样时间复杂度是O(n2). 能不能使时间复杂度降下来呢? 点值表示法: 我们 ...
- 浅谈FFT(快速傅里叶变换)
本文主要简单写写自己在算法竞赛中学习FFT的经历以及一些自己的理解和想法. FFT的介绍以及入门就不赘述了,网上有许多相关的资料,入门的话推荐这篇博客:FFT(最详细最通俗的入门手册),里面介绍得很详 ...
- [C++] 频谱图中 FFT快速傅里叶变换C++实现
在项目中,需要画波形频谱图,因此进行查找,不是很懂相关知识,下列代码主要是针对这篇文章. http://blog.csdn.net/xcgspring/article/details/4749075 ...
- matlab中fft快速傅里叶变换
视频来源:https://www.bilibili.com/video/av51932171?t=628. 博文来源:https://ww2.mathworks.cn/help/matlab/ref/ ...
- FFT快速傅里叶变换算法
1.FFT算法概要: FFT(Fast Fourier Transformation)是离散傅氏变换(DFT)的快速算法.即为快速傅氏变换.它是根据离散傅氏变换的奇.偶.虚.实等特性,对离散傅立叶变换 ...
- FFT快速傅里叶变换
FFT太玄幻了,不过我要先膜拜HQM,实在太强了 1.多项式 1)多项式的定义 在数学中,由若干个单项式相加组成的代数式叫做多项式.多项式中的每个单项式叫做多项式的项,这些单项式中的最高项次数,就是这 ...
- [学习笔记]FFT——快速傅里叶变换
大力推荐博客: 傅里叶变换(FFT)学习笔记 一.多项式乘法: 我们要明白的是: FFT利用分治,处理多项式乘法,达到O(nlogn)的复杂度.(虽然常数大) FFT=DFT+IDFT DFT: 本质 ...
随机推荐
- href与src的区别
src是source的缩写,指向外部资源的位置,指向的内容将会嵌入到文档中当前标签所在位置:在请求src资源时会将其指向的资源下载并应用到文档内,例如js脚本,img图片和frame等元素. href ...
- Conversion Between DataTable and List in C#
1.List to DataTable public static DataTable ToDataTable<TSource>(this IList<TSource> dat ...
- web 页面传值方法
一. 使用QueryString变量 QueryString是一种非常简单也是使用比较多的一种传值方式,但是它将传递的值显示在浏览器的地址栏中,如果是传递一个或多个安全性要求不高或是结构简单的数 ...
- window.showModalDialog 子窗口和父窗口不兼容最新的谷歌
最新版的谷歌不支持window.showModalDialog的写法,会出现,找不到方法的问题,同时返回值的方法window.dialogArguments;也用不了. 这里就只能用最原版的windo ...
- Win7设置承载网络 分类: 网络 2014-10-30 09:08 105人阅读 评论(0) 收藏
Win7设置承载网络 (1)最重要的第一步,要知道自己的网卡是否支持承载网络,如果不支持就悲剧地一票否决了,支持的话才能开始以后各步骤的设置. netsh wlan show drivers (2)设 ...
- Index Full Scan vs Index Fast Full Scan-1103
[Oracle] Index Full Scan vs Index Fast Full Scan作者:汪海 (Wanghai) 日期:14-Aug-2005 出处:http://spaces.msn. ...
- 解决UITableViewCell左侧分割线有空白的问题
ios7中,UITableViewCell左侧会有默认15像素的空白.设置setSeparatorInset:UIEdgeInsetsZero 能将空白去掉. ios8中,setSeparatorIn ...
- 使用Qt创建第一个OpenCV的Gui应用
写在前面 学习OpenCV有一些小日子了,发现群里还有很多初学OpenCV的人像我当初一样跌跌撞撞到处找资料,所以在这里把学习笔记分享给大家,希望有志学习OpenCV进行计算机视觉活动的小伙伴们能少走 ...
- Java学习----到底调用哪一个方法(多态)
public class Father { public void print() { System.out.println("Father:print()"); } } publ ...
- php 购物车完整实现代码
1.商品展示页面 代码如下: <table width="255" border="0" cellspacing="0" cellpa ...