[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 我们讲一个悲伤的故事. 从前有一个贫穷的樵夫在河边砍柴. 这时候河里出现了一个水神,夺过了他的斧头,说: “这把斧头,是不是你的?” 樵夫一看 ...
随机推荐
- gitlab使用 —— 多人协同工作(重要技能)
gitlab使用 —— 多人协同工作(重要技能) 学习链接: http://herry2013git.blog.163.com/blog/static/219568011201341111240751 ...
- 3-Python内置结构-列表
目录 1 Python内置数据结构 1.1 数值型 1.2 math模块 1.3 round圆整 1.4 常用的其他函数 1.5 类型判断 2 列表 2.1 索引访问 2.2 列表和链表的区别 2.3 ...
- win32的回调函数
[转]http://blog.csdn.net/w419675647/article/details/6599070 众所周知,win32的回调函数WndProc()是操作系统调用的函数,win32用 ...
- ldconfig是一个动态链接库管理命令
ldconfig是一个动态链接库管理命令 为了让动态链接库为系统所共享,还需运行动态链接库的管理命令--ldconfig ldconfig 命令的用途,主要是在默认搜寻目录(/lib和/usr/li ...
- selenium滚动到顶部与底部
#coding=utf-8 from selenium import webdriver #滚动到浏览器顶部 js_top = "var q=document.documentElement ...
- javascript sleep方法
function sleep(numberMillis) { var now = new Date(); var exitTime = now.getTime() + numberMi ...
- Django之进阶相关操作
一.QuerySet的特点 1.可切片 使用Python 的切片语法来限制查询集记录的数目 .它等同于SQL 的LIMIT 和OFFSET 子句. 1 >>> Entry.objec ...
- 洛谷 P1469 找筷子 题解
题目传送门 先排序一遍,再一个一个判断是否有偶数个.注意for循环要i+=2. #include<bits/stdc++.h> using namespace std; ]; int ma ...
- 保存最后N个元素
cookbook系列 问题:对要搜索的值的最后几项做个有限的历史记录. 方案: #coding=utf- from collections import deque def search(lines, ...
- 二叉排序树实现(C++封装)
设计思路 设计一个类,根结点只可读取,具备构造二叉树.插入结点.删除结点.查找. 查找最大值.查找最小值.查找指定结点的前驱和后继等功能接口. 二叉排序树概念 它或者是一棵空树:或者是具有下列性质的二 ...