[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 我们讲一个悲伤的故事. 从前有一个贫穷的樵夫在河边砍柴. 这时候河里出现了一个水神,夺过了他的斧头,说: “这把斧头,是不是你的?” 樵夫一看 ...
随机推荐
- 【CODEVS】1922 骑士共存问题
[算法]二分图最大匹配(最大流) [题解]按(i+j)奇偶性染色后,发现棋子跳到的地方刚好异色. 然后就是二分图了,对于每个奇点向可以跳到的地方连边,偶点不需连(可逆). 所以题目要求转换为求二分图上 ...
- void指针和NULL指针
Void指针和NULL指针 Void指针: Void指针我们称之为通用指针,就是可以指向任意类型的数据.也就是说,任何类型的指针都可以赋值给Void指针. 举例: #include<stdio. ...
- tar解压与压缩
1.解压 tar -zxvf 压缩文件名 -C 指定的目录 (制定的目录必须存在) 2.压缩 tar -czvf 压缩后的文件名 要压缩的文件夹
- 36 - 网络编程-TCP编程
目录 1 概述 2 TCP/IP协议基础 3 TCP编程 3.1 通信流程 3.2 构建服务端 3.3 构建客户端 3.4 常用方法 3.4.1 makefile方法 3.5 socket交互 3.4 ...
- (转)USB的描述符及各种描述符之间的依赖关系
全文链接:http://justmei.blog.163.com/blog/static/11609985320102421659260/?latestBlog 1 推荐 [原创] USB入门系列之七 ...
- sicily 1259. Sum of Consecutive Primes
Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...
- MYSQL三种安装方式--rpm包安装
1. 首先检查机器里是否已经存在MySQL $ rpm -qa | grep mysql 2. 去官网下载相应的rpm包:https://dev.mysql.com/downloads/mysql/ ...
- 最小的Django应用
创建一个hello.py 内容如下: import sys from django.conf import settings # 设置 settings.configure( DEBUG = Tr ...
- ASP.NET Core 2.0 MVC 发布部署--------- Ubuntun 16.04 X64 具体操作
.Net Core 部署到Ubuntu 16.04 中的步骤 1.安装工具 1.apache 2..Net Core(dotnet-sdk-2.0) 3.Supervisor(进程管理工具,目的是服务 ...
- 2.rabbitmq 工作队列
1. 生产者 #coding:utf8 import pika import json import sys message = ''.join(sys.argv[1:]) or "hell ...