[HDU4609]3-idiots(生成函数+FFT)
3-idiots
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6343 Accepted Submission(s): 2216Problem DescriptionKing 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.InputAn integer T(T≤100) will exist in the first line of input, indicating the number of test cases.
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.OutputOutput the probability that their branches can form a triangle, in accuracy of 7 decimal places.Sample Input2
4
1 3 3 4
4
2 3 3 4Sample Output0.5000000
1.0000000SourceRecommendliuyiding
代码用时:3h
比较裸的生成函数应用。理清容斥关系就好。
应为一个非常低级的错误(复数减法运算符重载出错),调了非常长的时间。
以后还是应该尽量自己写程序以免受别人程序干扰,FFT和复数运算模板要熟练。
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define rep(i,l,r) for (int i=l; i<=r; i++)
#define mem(a) memset(a,0,sizeof(a))
typedef long long ll;
using namespace std; const int N=(<<)+;
const double pi=acos(-.);
int T,n,nn,m,q[N],rev[N];
ll s[N]; struct C{
double x,y;
C (double a=,double b=):x(a),y(b){}
}a[N];
C operator +(C a,C b){ return C(a.x+b.x,a.y+b.y); }
C operator -(C a,C b){ return C(a.x-b.x,a.y-b.y); }
C operator *(C a,C b){ return C(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x); } void DFT(C a[],int f){
for (int i=;i<nn;i++)
if (i<rev[i]) swap(a[i],a[rev[i]]);
for (int i=; i<nn; i<<=){
C wn(cos(pi/i),f*sin(pi/i));
for (int p=i<<,j=; j<nn; j+=p){
C w(,);
for (int k=; k<i; k++,w=w*wn){
C x=a[j+k],y=w*a[i+j+k]; a[j+k]=x+y; a[i+j+k]=x-y;
}
}
}
if (f==-) rep(i,,nn-) a[i].x/=nn;
} int main(){
freopen("hdu4609.in","r",stdin);
freopen("hdu4609.out","w",stdout);
scanf("%d",&T);
while (T--){
m=; mem(a); mem(q); mem(s); mem(rev); scanf("%d",&n);
rep(i,,n) scanf("%d",&q[i]),m=max(m,q[i]);
rep(i,,n) a[q[i]].x++;
m<<=; int L=; for (nn=; nn<=m; nn<<=) L++;
rep(i,,nn-) rev[i]=(rev[i>>]>>)|((i&)<<(L-));
DFT(a,); rep(i,,nn-) a[i]=a[i]*a[i]; DFT(a,-);
rep(i,,m) s[i]=(ll)(a[i].x+0.5);
rep(i,,n) s[q[i]<<]--;
rep(i,,m) s[i]=s[i-]+(s[i]>>);
sort(q+,q+n+); ll ans=,tot=1ll*n*(n-)*(n-)/;
rep(i,,n) ans+=1ll*s[m]-s[q[i]]-1ll*(n-i+)*(n-)+1ll*(n-i+)*(n-i)/;
printf("%.7lf\n",(double)ans/tot);
}
return ;
}
[HDU4609]3-idiots(生成函数+FFT)的更多相关文章
- loj6570 毛毛虫计数(生成函数FFT)
link 巨佬olinr的题解 <-- olinr很强 考虑生成函数 考虑直径上点数>=4的毛毛虫的直径,考虑直径中间那些节点以及他上面挂的那些点的EGF \(A(x)=\sum_{i\g ...
- bzoj 3513: [MUTC2013]idiots【生成函数+FFT】
想了好长时间最后发现真是石乐志 第一反应就是两边之和大于第三边,但是这个东西必须要满足三次-- 任意的两边之和合通过生成函数套路+FFT求出来(记得去掉重复选取的),然后这任意两边之和大于任意第三边可 ...
- HDU4609 3-idiots(母函数 + FFT)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4609 Description King OMeGa catched three men wh ...
- 挑选队友 (生成函数 + FFT + 分治)
链接:https://www.nowcoder.com/acm/contest/133/D来源:牛客网 题目描述 Applese打开了m个QQ群,向群友们发出了组队的邀请.作为网红选手,Applese ...
- 【BZOJ3771】Triple 生成函数 FFT 容斥原理
题目大意 有\(n\)把斧头,不同斧头的价值都不同且都是\([0,m]\)的整数.你可以选\(1\)~\(3\)把斧头,总价值为这三把斧头的价值之和.请你对于每种可能的总价值,求出有多少种选择方案. ...
- 2019.01.02 bzoj3513: [MUTC2013]idiots(fft)
传送门 fftfftfft经典题. 题意简述:给定nnn个长度分别为aia_iai的木棒,问随机选择3个木棒能够拼成三角形的概率. 思路:考虑对于木棒构造出生成函数然后可以fftfftfft出两个木 ...
- 2018.12.31 bzoj3771: Triple(生成函数+fft+容斥原理)
传送门 生成函数经典题. 题意简述:给出nnn个数,可以从中选1/2/31/2/31/2/3个,问所有可能的和对应的方案数. 思路: 令A(x),B(x),C(x)A(x),B(x),C(x)A(x) ...
- BZOJ 3771: Triple(生成函数 FFT)
Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 911 Solved: 528[Submit][Status][Discuss] Description ...
- 【BZOJ3771】Triple 生成函数+FFT
[BZOJ3771]Triple Description 我们讲一个悲伤的故事. 从前有一个贫穷的樵夫在河边砍柴. 这时候河里出现了一个水神,夺过了他的斧头,说: “这把斧头,是不是你的?” 樵夫一看 ...
随机推荐
- form表单设置input文本属性只读,不可更改
记住一条好用的,设置readonly属性为true <input readonly=''true"> 更多方法,转载: http://www.jb51.net/web/6 ...
- sg函数&&子状态的讨论
题目链接:https://cn.vjudge.net/contest/269933#problem/H 具体思路:首先,这是一个公平的比赛,并且是两个人参与,两个人都足够聪明,并且可以通过有限步结束比 ...
- 安装 Google BBR 加速VPS网络
Google BBR就是谷歌公司提出的一个开源TCP拥塞控制的算法.详情可以看这儿:https://lwn.net/Articles/701165.https://blog.sometimesnaiv ...
- Linux系统调用、新增系统调用方法【转】
转自:http://blog.chinaunix.net/uid-25374603-id-3401045.html 说明: 系统调用是内核和应用程序间的接口,应用程序要访问硬件设备和其他操作系统资源, ...
- nginx升级至1.12.1版本
nginx升级至1.12.1 编号 名称 说明 1 nginx-1.12.1.tar.gz nginx安装程序 2 nginx_upstream_check_module-master.zip 实现后 ...
- tomcat+java的web程序持续占cpu高问题调试【转】
转自 tomcat+java的web程序持续占cpu问题调试 - 像风一样的自由 - CSDN博客http://blog.csdn.net/five3/article/details/28416771 ...
- PHP 不让标准浏览器(firfox,chrome等)走浏览器的缓存页面
或在HTML页面里加: <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache,no-store, must-reva ...
- 微信开发,调用js-SDK接口
微信开发,调用js-SDK接口<!DOCTYPE html><html><head lang="en"> <meta charset=&q ...
- learnyounode 题解
//第三题 var fs =require('fs')var path=process.argv[2]fs.readFile(path,function(err,data){ var lines=da ...
- NLP基础 成分句法分析和依存句法分析
正则匹配: .除换行符所有的 ?表示0次或者1次 *表示0次或者n次 a(bc)+表示bc至少出现1次 ^x.*g$表示字符串以x开头,g结束 |或者 http://regexr.com/ 依存句法分 ...