3-idiots

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6343    Accepted Submission(s): 2216

Problem Description
King 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.
 
Input
An 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.
 
Output
Output the probability that their branches can form a triangle, in accuracy of 7 decimal places.
 
Sample Input
2
4
1 3 3 4
4
2 3 3 4
 
Sample Output
0.5000000
1.0000000
 
Source
 
Recommend
liuyiding

代码用时: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)的更多相关文章

  1. loj6570 毛毛虫计数(生成函数FFT)

    link 巨佬olinr的题解 <-- olinr很强 考虑生成函数 考虑直径上点数>=4的毛毛虫的直径,考虑直径中间那些节点以及他上面挂的那些点的EGF \(A(x)=\sum_{i\g ...

  2. bzoj 3513: [MUTC2013]idiots【生成函数+FFT】

    想了好长时间最后发现真是石乐志 第一反应就是两边之和大于第三边,但是这个东西必须要满足三次-- 任意的两边之和合通过生成函数套路+FFT求出来(记得去掉重复选取的),然后这任意两边之和大于任意第三边可 ...

  3. HDU4609 3-idiots(母函数 + FFT)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4609 Description King OMeGa catched three men wh ...

  4. 挑选队友 (生成函数 + FFT + 分治)

    链接:https://www.nowcoder.com/acm/contest/133/D来源:牛客网 题目描述 Applese打开了m个QQ群,向群友们发出了组队的邀请.作为网红选手,Applese ...

  5. 【BZOJ3771】Triple 生成函数 FFT 容斥原理

    题目大意 有\(n\)把斧头,不同斧头的价值都不同且都是\([0,m]\)的整数.你可以选\(1\)~\(3\)把斧头,总价值为这三把斧头的价值之和.请你对于每种可能的总价值,求出有多少种选择方案. ...

  6. 2019.01.02 bzoj3513: [MUTC2013]idiots(fft)

    传送门 fftfftfft经典题. 题意简述:给定nnn个长度分别为aia_iai​的木棒,问随机选择3个木棒能够拼成三角形的概率. 思路:考虑对于木棒构造出生成函数然后可以fftfftfft出两个木 ...

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

  8. BZOJ 3771: Triple(生成函数 FFT)

    Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 911  Solved: 528[Submit][Status][Discuss] Description ...

  9. 【BZOJ3771】Triple 生成函数+FFT

    [BZOJ3771]Triple Description 我们讲一个悲伤的故事. 从前有一个贫穷的樵夫在河边砍柴. 这时候河里出现了一个水神,夺过了他的斧头,说: “这把斧头,是不是你的?” 樵夫一看 ...

随机推荐

  1. [AHOI2012]树屋阶梯 题解(卡特兰数)

    [AHOI2012]树屋阶梯 Description 暑假期间,小龙报名了一个模拟野外生存作战训练班来锻炼体魄,训练的第一个晚上,教官就给他们出了个难题.由于地上露营湿气重,必须选择在高处的树屋露营. ...

  2. 59、有用过with statement吗?它的好处是什么?

    python中的with语句是用来干嘛的?有什么作用? with语句的作用是通过某种方式简化异常处理,它是所谓的上下文管理器的一种 用法举例如下: with open('output.txt', 'w ...

  3. Linux 下解决安装多个node冲突的问题(重新安装node)

    一个系统中不经意安装了多个node版本,结果更新后还是原来的版本,下面思考一下解决办法: 敲黑板: 1. nodejs 用 包管理器安装一般在 /usr/local/bin 2. 查看当前目录下的no ...

  4. 21.Merge Two Sorted Lists---《剑指offer》面试17

    题目链接:https://leetcode.com/problems/merge-two-sorted-lists/description/ 题目大意: 给出两个升序链表,将它们归并成一个链表,若有重 ...

  5. 关于Hazard Pointers的话题

    关于Hazard Pointers的话题, 起源于这个文章: 实现无锁的栈与队列(4) http://www.cnblogs.com/catch/p/3176636.html 其实他的系列文章(3)之 ...

  6. curl 发送请求的时候报错

    AWS HTTP error: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see ...

  7. bash: composer: command not found

    下载composer到本地:curl -sS https://getcomposer.org/installer | php 移动至系统服务:sudo mv composer.phar /usr/bi ...

  8. ntp 校时程序

    //effect:send ntp packet and get the ntp packet ,make the time OK//2014.7.31 is OK//#include <sys ...

  9. CSS背景横向平铺BUG,解决方法

    给定DIV一个背景图片横向平铺,缩小浏览器,拉动横向滚动条,此时触发此BUG:背景图片平铺不完整 解决办法: 1.把背景图片写在BODY上,此办法局限于没有使用iframe的情况下,所以少用 2.设定 ...

  10. 欧拉回路&欧拉通路判断

    欧拉回路:图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在一条回路经过G每条边有且仅有一次, 称这条回路为欧拉回路.具有欧拉回路的图成为欧拉图. 判断欧拉通路是否存在的方法 ...