[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 我们讲一个悲伤的故事. 从前有一个贫穷的樵夫在河边砍柴. 这时候河里出现了一个水神,夺过了他的斧头,说: “这把斧头,是不是你的?” 樵夫一看 ...
随机推荐
- NB二人组(一)----堆排序
堆排序前传--树与二叉树简介 特殊且常用的树--二叉树 两种特殊的二叉树 二叉树的存储方式 二叉树小结 堆排序 堆这个玩意....... 堆排序过程: 构造堆: 堆排序的算法程序(程序需配合着下图理 ...
- GCD HDU - 1695 莫比乌斯反演入门
题目链接:https://cn.vjudge.net/problem/HDU-1695#author=541607120101 感觉讲的很好的一个博客:https://www.cnblogs.com/ ...
- Linux下文件目录权限和对应命令的总结
Linux下的权限有rwx三种,分别对应读,写,执行三种,在对文件和目录时,分别是下列含义: 对应权限的命令为: 文件: r-- cat, more, head, tail w-- echo, vi ...
- hash算法搜索获得api函数地址的实现,"kernel32.dll", "CreateThread"
我们一般要获得一个函数的地址,通常采用的是明文,例如定义一个api函数字符串"MessageBoxA",然后在GetProcAddress函数中一个字节一个字节进行比较.这样弊端很 ...
- SQLite3 使用教学
source: SQL中文站:http://www.sqlite.com.cn/MySqlite/4/378.Html OS X自从10.4后把SQLite这套相当出名的数据库软件,放进了作业系统工具 ...
- 147.Insertion Sort List---链表排序(直接插入)
题目链接 题目大意:对链表进行插入排序. 解法:直接插入排序.代码如下(耗时40ms): public ListNode insertionSortList(ListNode head) { List ...
- hosts,命令行前面的显示
1,/etc/hosts,主机名ip配置文件. # Do not remove the following line, or various programs # that require netwo ...
- HDU 2066 一个人的旅行(dijkstra水题+判重边)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2066 题目大意:输入数据有多组,每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有 ...
- 使用CEPH RGW admin ops API 进行用户user AK/SK管理的秘诀
需求: 云平台面板上需要支持为不同的用户创建不同的RGW 的AK/SK用户秘钥,以完成对象存储的用户隔离,并可以管理bucket和查看bucket容量信息. 分析:查阅CEPH官网文档 S3 API ...
- hdu 5505(数论-gcd的应用)
GT and numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...