http://acm.hdu.edu.cn/showproblem.php?pid=4609

题意:给你n个数,问任意取三边能够,构成三角形的概率为多少。

思路:使用FFT对所有长度的个数进行卷积(\(C = \{x + y| x \in A, y \in B \} \)),得到所有两边和情况数,再考虑去掉重复的情况。找边并计数的时候再去掉不可能的情况。具体操作看bin神的博客   

另FFT还可以用来进行多项式和高精度乘法,又难懂又难用的东西=x=

/** @Date    : 2016-12-04-16.31
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version :
*/ #include<bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;
const double PI = acos(-1.0); struct Complex
{
double a, i;//实部,虚部
Complex(double aa = 0, double ii = 0)
{
a = aa, i = ii;
}
Complex operator +(const Complex &y)
{
return Complex(a + y.a, i + y.i);
}
Complex operator -(const Complex &y)
{
return Complex(a - y.a, i - y.i);
}
Complex operator *(const Complex &y)
{
return Complex(a * y.a - i * y.i, a * y.i + i * y.a);
}
}; void change(Complex y[], int len)//len 必须为2的幂;位置i和二进制反转的位置互换
{
int i, j, k;
for(i = 1, j = len / 2; i < len - 1; i++)
{
if(i < j)
swap(y[i], y[j]);
//交换互为小标反转的元素,i<j交换一次
//i正常++ j左反转类型的+1 始终保持二者反转
k = len / 2;
while(j >= k)
{
j -= k;
k /= 2;
}
if(j < k)
j += k;
}
} void fft(Complex y[], int len, int on)//on==1时DFT -1时IDFT len必须为2的幂
{
change(y, len);
for(int h = 2; h <= len; h <<= 1)
{
Complex wn(cos(-on * 2 * PI / h), sin(-on * 2 * PI / h));
for(int j = 0; j < len; j+=h)
{
Complex w(1, 0);
for(int k = j; k < j + h / 2; k++)
{
Complex u = y[k];
Complex t = w * y[k + h / 2];
y[k] = u + t;
y[k + h / 2] = u - t;
w = w * wn;
}
}
}
if(on == -1)
for(int i = 0; i < len; i++)
y[i].a /= len;
} Complex x1[4 * N];
int a[N];
LL num[4 * N];
LL sum[4 * N]; int main()
{
int T;
cin >> T;
while(T--)
{
int n;
scanf("%d", &n);
MMF(num);
for(int i = 0; i < n; i++)
{
scanf("%d", a + i);
num[a[i]]++; }
sort(a, a + n);
////
int len1 = a[n - 1] + 1;
int len = 1;
while(len < 2 * len1)
len <<= 1;
for(int i = 0; i < len1; i++)
x1[i] = Complex(num[i], 0);
for(int i = len1; i < len; i++)
x1[i] = Complex(0 , 0);
//
fft(x1, len, 1);
for(int i = 0; i < len; i++)
x1[i] = x1[i] * x1[i];
fft(x1, len, -1);
//
for(int i = 0; i < len; i++)
num[i] = (LL)(x1[i].a + 0.5);
len = 2 * a[n - 1]; /////
for(int i = 0; i < n; i++)//卷积中同样的数字选取两次
num[a[i] + a[i]]--;
for(int i = 1; i <= len; i++)//卷积中数字选取无顺序关系
num[i] /= 2;
sum[0] = 0;
for(int i = 1; i <= len; i++)//数字出现次数前缀和
{
sum[i] = sum[i - 1] + num[i];
}
LL cnt = 0;
for(int i = 0; i < n; i++)
{
cnt += sum[len] - sum[a[i]];
///
cnt -= (LL)(n - i - 1) * i;
cnt -= (LL)(n - 1) * 1;
cnt -= (LL)(n - i - 1) * (n - i - 2) / 2;
}
printf("%.7lf\n", (double)cnt * 6.0 / n / (n - 1.0) / (n - 2.0));
}
return 0;
}

HDU 4609 FFT模板的更多相关文章

  1. hdu 4609 FFT

    题意:给出一堆数,问从这些数中取3个能组成三角形的概率? sol:其实就是问从这些数里取3个组成三角形有多少种取法 脑洞大开的解法:用FFT 设一开始的数是1 3 3 4 作一个向量x,其中x[i]= ...

  2. HDU 1402 fft 模板题

    题目就是求一个大数的乘法 这里数字的位数有50000的长度,按平时的乘法方式计算,每一位相乘是要n^2的复杂度的,这肯定不行 我们可以将每一位分解后作为系数,如153 = 1*x^2 + 5*x^1 ...

  3. HDU 4609 FFT+组合数学

    3-idiots Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. HDU 4609 FFT+各种分类讨论

    思路: http://www.cnblogs.com/kuangbin/archive/2013/07/24/3210565.html 其实我是懒得写了.... 一定要define int long ...

  5. hdu 4609 3-idiots [fft 生成函数 计数]

    hdu 4609 3-idiots 题意: 给出\(A_i\),问随机选择一个三元子集,选择的数字构成三角形的三边长的概率. 一开始一直想直接做.... 先生成函数求选两个的方案(注意要减去两次选择同 ...

  6. fft模板 HDU 1402

    // fft模板 HDU 1402 #include <iostream> #include <cstdio> #include <cstdlib> #includ ...

  7. HDU 4609 3-idiots FFT+容斥

    一点吐槽:我看网上很多分析,都是在分析这个题的时候,讲了半天的FFT,其实我感觉更多的把FFT当工具用就好了 分析:这个题如果数据小,统计两个相加为 x 的个数这一步骤(这个步骤其实就是求卷积啊),完 ...

  8. HDU 1402 A * B Problem Plus (FFT模板题)

    FFT模板题,求A*B. 用次FFT模板需要注意的是,N应为2的幂次,不然二进制平摊反转置换会出现死循环. 取出结果值时注意精度,要加上eps才能A. #include <cstdio> ...

  9. HDU 4609 3-idiots ——(FFT)

    这是我接触的第一个关于FFT的题目,留个模板. 这题的题解见:http://www.cnblogs.com/kuangbin/archive/2013/07/24/3210565.html. FFT的 ...

随机推荐

  1. [C++] Class (part 2)

    Members that are const or reference must be initialized. Similary, members that are of a class type ...

  2. springboot 整合apache shiro

    这几天因为项目需要,学习了下shiro,由此留下一些记录,也希望对初学shiro的朋友有帮助. springboot 是这两年新兴起来的一个项目,它的出现是为了减少springmvc开发过程中需要引入 ...

  3. 20172332 实验一《Java开发环境的熟悉》实验报告

    20172332 2017-2018-2 <程序设计与数据结构>实验一报告 课程:<程序设计与数据结构> 班级: 1723 姓名: 于欣月 学号:20172332 实验教师:王 ...

  4. 搭建独立域名博客 -- 独立域名博客上线了 www.hanshuliang.com

    博客是安装在阿里云的服务器上. 小结 : -- 进入数据库命令 :mysql -uroot -p123456 ; -- 检查nginx配置语法 :.../nginx/sbin/nginx -t; -- ...

  5. WIN8/8.1/10进入BIOS方法图解

    1.首先点击桌面左下角的"开始". 2.然后点击电源. 3.然后按住shift,同时点击"重启".于是进入这个画面: 4.然后点击"疑难解答" ...

  6. 父类与子类的转换as,is

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. OBJ文件

    OBJ文件是Alias|Wavefront公司为它的一套基于工作站的3D建模和动画软件"Advanced Visualizer"开发的一种标准3D模型文件格式,很适合用于3D软件模 ...

  8. 【bzoj5064】B-number 数位dp

    题目描述 B数的定义:能被13整除且本身包含字符串"13"的数. 例如:130和2613是B数,但是143和2639不是B数. 你的任务是计算1到n之间有多少个数是B数. 输入 输 ...

  9. main方法为什么是静态的

    main函数其实也是所在类的一个方法,就比如一个类是test,那么该类的main其实就是test.main(String[] args),众所周知如果一个方法不是静态的,则要先实例化该类,比如要这样  ...

  10. 个人vim配置

    YouCompletMe支持golang cd .vim/bundle/YouCompleteMe ./install.sh --clang-completer --go-completer clan ...