hdu 4609 3-idiots
http://acm.hdu.edu.cn/showproblem.php?pid=4609
FFT 不会 找了个模板
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std; typedef long long ll;
typedef pair<double,double>ppd;
const double PI = acos(-1.);
const int MAXL = (1 << 18)+100;
int a[MAXL],b[MAXL];
ll c[MAXL];
ppd A[MAXL],B[MAXL],C[MAXL],T[MAXL];
int id,ln;
void fill0(int m,int d,int *s,ppd *P)
{
if (m == ln)
P[d] = make_pair(s[id++],0.0);
else
{
fill0(m<<1,d,s,P);
fill0(m<<1,d+m,s,P);
}
} void fill1(int m,int d,ppd *C,ppd *P)
{
if (m == ln) P[d] = C[id++];
else
{
fill1(m<<1,d,C,P);
fill1(m<<1,d+m,C,P);
}
} void fft(double oper,ppd *P)
{
for (int d = 0;(1 << d) < ln;++d)
{
int m = (1 << d);
double p0 = PI / m * oper;
double sinp0 = sin(p0);
double cosp0 = cos(p0);
for (int i = 0;i < ln;i += (m << 1))
{
double sinp = 0;
double cosp = 1;
for (int j = 0;j < m;++j)
{
double ta = cosp * P[i+j+m].first - sinp*P[i+j+m].second;
double tb = cosp * P[i+j+m].second + sinp * P[i+j+m].first;
P[i+j+m].first = P[i+j].first - ta;
P[i+j+m].second = P[i+j].second - tb;
P[i+j].first += ta;
P[i+j].second += tb;
double tsinp = sinp;
sinp = sinp * cosp0 + cosp * sinp0;
cosp = cosp * cosp0 -tsinp * sinp0;
}
}
}
}
void polyMul(int *a,int *b,ll *c,int n)
{
ln=(1<<18);
while((ln>>2)>=n) ln=ln>>1;
id = 0;
fill0(1,0,a,A);
fft(1.0,A);
id = 0;
fill0(1,0,b,B);
fft(1.0,B);
for (int i = 0;i < ln;++i)
{
C[i].first = A[i].first * B[i].first - A[i].second * B[i].second;
C[i].second = A[i].first * B[i].second + A[i].second * B[i].first;
}
id = 0;
fill1(1,0,C,T);
fft(-1.0,T);
for (int i = 0;i < ln;++i)
c[i] = ll(T[i].first / ln + 0.1);
}
int main()
{
//freopen("data.in","r",stdin);
//freopen("1010.in","r",stdin);
//freopen("my.out","w",stdout);
int T;
scanf("%d",&T);
while(T--)
{
int m;
scanf("%d",&m);
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
int n=0;
for(int i=0;i<m;++i)
{
int tmp=0;
scanf("%d",&tmp);
n=max(n,tmp);
++a[tmp];
++b[tmp];
}
++n;
polyMul(a,b,c,n);
for(int i=0;i<n;++i)
if(a[i])
c[i+i]-=a[i];
for(int i=0;i<ln;++i)
c[i]=c[i]>>1; double tmp=0.0;
double t=0;
for(int i=0;i<n;++i)
{
t+=c[i];
tmp+=(a[i]*t);
}
double sum=(1.0*m*(m-1)*(m-2)/2/3);
printf("%.7lf\n",(sum-tmp)/sum);
}
return 0;
}
hdu 4609 3-idiots的更多相关文章
- 解题:HDU 4609 Three Idiots
题面 要求组合的方法显然我们需要对桶卷积,即设$F(x)=\sum\limits_{i=1}^{maxx}x^{cnt[i]}$,然后我们初步的先把$F^2(x)$卷出来,表示选两条边.然后我们发现如 ...
- 快速傅里叶变换应用之二 hdu 4609 3-idiots
快速傅里叶变化有不同的应用场景,hdu4609就比较有意思.题目要求是给n个线段,随机从中选取三个,组成三角形的概率. 初始实在没发现这个怎么和FFT联系起来,后来看了下别人的题解才突然想起来:组合计 ...
- hdu 4609 3-idiots [fft 生成函数 计数]
hdu 4609 3-idiots 题意: 给出\(A_i\),问随机选择一个三元子集,选择的数字构成三角形的三边长的概率. 一开始一直想直接做.... 先生成函数求选两个的方案(注意要减去两次选择同 ...
- hdu 4609 3-idiots <FFT>
链接: http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意: 给定 N 个正整数, 表示 N 条线段的长度, 问任取 3 条, 可以构成三角形的概率为多 ...
- HDU 4609 3-idiots(FFT)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意:给出n个正整数(数组A).每次随机选出三个数.问这三个数能组成三角形的概率为多大? 思路: ...
- HDU 4609 FFT模板
http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意:给你n个数,问任意取三边能够,构成三角形的概率为多少. 思路:使用FFT对所有长度的个数进行卷积(\ ...
- hdu 4609 3-idiots——FFT
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4609 答案就是随便选三条边的方案 - 不合法的方案. 不合法的方案就是算出 x+y = k 的方案数 g[ ...
- hdu 4609 3-idiots —— FFT
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4609 算不合法的比较方便: 枚举最大的边,每种情况算了2次,而全排列算了6次,所以还要乘3: 注意枚举最大 ...
- FFT(快速傅里叶变换):HDU 4609 3-idiots
3-idiots Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- hdu4717The Moving Points(三分)
链接 需要特判一下n=1的时候 精度调太低会超时 #include <iostream> #include<cstdio> #include<cstring> #i ...
- linux之echo命令
linux的echo命令, 在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的, 因此有必要了解下echo的用法 echo命令的功能是在显示器上显示一段文字,一般起到一个提示 ...
- ES6中的const命令
1.const声明一个只读常量,一旦声明,常量的值就不能改变 1 const PI=3.1415; 2 console.log(PI);//3.1415 3 4 PI=3;//Uncaught T ...
- 使用fragment兼容低版本的写法
[1]定义fragment继承V4包中的Fragment [2]定义的activity要继承v4包中的FragmentActivity [3]通过这个方法getSupportFragme ...
- maven实战_01_搭建maven开发环境
一 下载maven 在maven官网上可下载maven:http://maven.apache.org/download.cgi 下载好后,解压.我的解压到了:D:\maven\apache-mave ...
- mybatis动态SQL中的sql片段
在mybatis中通过使用SQL片段可以提高代码的重用性,如下情景: 1.创建动态SQL <sql id="sql_count">select count(*)< ...
- jQuery中其他
hide: 隐藏 $('img').hide(); show:显示 $('img').show(); 单选多选下拉菜单 选中状态checked ($('.radio:checked')); 单选 ( ...
- One Class SVM, SVDD(Support Vector Domain Description)(转)
今天给大家介绍一下one class classification以及用SVDD(support vector domain description)做one class classification ...
- xcode引入第三方静态类库 duplicate symbol _OBJC_XXX 重复编译错误
xcode引入第三方静态类库 duplicate symbol _OBJC_XXX 重复编译错误 一:场景 xcode 同时引入了 libA.a, libB.a 两个静态类库,如果 这两个静态类库之中 ...
- javaWeb1 tomcat
tomcat使用常见问题: 1.闪退: 原因:tomcat 软件是由java语言开发的,当它启动时,会默认到系统 的环境变量中查找 JAVA_HOME 的变量.找它的目的时tomcat 启动 时需要j ...