题意:给定 n 条边,问随机选出 3 条边,能组成三角形的概率是多少。

析:答案很明显就是  能组成三角形的种数 / (C(n, 3))。现在的问题是怎么求能组成三角形的种数。

这个博客说的非常清楚了。。。

https://www.cnblogs.com/kuangbin/archive/2013/07/24/3210565.html

总体来说就是把边长转换成下标,然后再根据组合数,就可以知道选出两条边,长度为 i 有多少种情况,然后再减去重复的,最后再枚举斜边,就可以解决这个问题了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define be begin()
#define ed end()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,n,x) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 400000 + 100;
const int maxm = 1e6 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
inline int readInt(){ int x; scanf("%d", &x); return x; } struct Complex{
double x, y;
Complex(double x_ = 0., double y_ = 0.) : x(x_), y(y_) {}
Complex operator - (const Complex &c) const{
return Complex(x - c.x, y - c.y);
}
Complex operator + (const Complex &c) const{
return Complex(x + c.x, y + c.y);
}
Complex operator * (const Complex &c) const{
return Complex(x * c.x - y * c.y, x * c.y + c.x * y);
}
}; void change(Complex *y, int len){
for(int i = 1, j = (len>>1); i < len-1; ++i){
if(i < j) swap(y[i], y[j]);
int k = len>>1;
while(j >= k){
j -= k;
k >>= 1;
}
if(j < k) j += k;
}
} void fft(Complex *y, int len, int on){
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(-1 == on) for(int i = 0; i < len; ++i)
y[i].x /= len;
} int a[maxn>>2];
Complex x[maxn];
LL sum[maxn], num[maxn]; int main(){
int T; cin >> T;
while(T--){
scanf("%d", &n); ms(num, 0);
for(int i = 0; i < n; ++i) ++num[a[i]=readInt()];
sort(a, a + n);
int mmax = a[n-1] + 1;
int len = 1;
while(len < (mmax<<1)) len <<= 1;
for(int i = 0; i < mmax; ++i)
x[i] = Complex(num[i], 0);
for(int i = mmax; i < len; ++i)
x[i] = Complex();
fft(x, len, 1);
for(int i = 0; i < len; ++i)
x[i] = x[i] * x[i];
fft(x, len, -1);
for(int i = 0; i < len; ++i)
num[i] = (LL)(x[i].x + 0.5);
for(int i = 0; i < n; ++i)
--num[a[i]<<1];
for(int i = 1; i <= len; ++i)
sum[i] = sum[i-1] + num[i] / 2;
LL ans = 0;
for(int i = 0; i < n; ++i){
ans += sum[len] - sum[a[i]];
ans -= (LL)(n-i-1) * (n-i-2) / 2; // both are greater than a[i]
ans -= (LL)(n-i-1) * i; // the one is greater than a[i] but the other is less;
ans -= n - 1; // the one of the branches is a[i]
}
LL de = (LL)n * (n-1) * (n-2) / 6;
printf("%.7f\n", ans * 1. / de);
}
return 0;
}

  

HDU 4609 3-idiots (组合数学 + FFT)的更多相关文章

  1. hdu 4609 3-idiots(快速傅里叶FFT)

    比较裸的FFT(快速傅里叶变换),也是为了这道题而去学的,厚的白书上有简单提到,不过还是推荐看算法导论,讲的很详细. 代码的话是照着别人敲的,推荐:http://www.cnblogs.com/kua ...

  2. 解题:HDU 4609 Three Idiots

    题面 要求组合的方法显然我们需要对桶卷积,即设$F(x)=\sum\limits_{i=1}^{maxx}x^{cnt[i]}$,然后我们初步的先把$F^2(x)$卷出来,表示选两条边.然后我们发现如 ...

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

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

  4. 快速傅里叶变换应用之二 hdu 4609 3-idiots

    快速傅里叶变化有不同的应用场景,hdu4609就比较有意思.题目要求是给n个线段,随机从中选取三个,组成三角形的概率. 初始实在没发现这个怎么和FFT联系起来,后来看了下别人的题解才突然想起来:组合计 ...

  5. HDU 4609 FFT+组合数学

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

  6. hdu 4609 3-idiots <FFT>

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意: 给定 N 个正整数, 表示 N 条线段的长度, 问任取 3 条, 可以构成三角形的概率为多 ...

  7. HDU 4609 3-idiots(FFT)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意:给出n个正整数(数组A).每次随机选出三个数.问这三个数能组成三角形的概率为多大? 思路: ...

  8. HDU 4609 FFT模板

    http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意:给你n个数,问任意取三边能够,构成三角形的概率为多少. 思路:使用FFT对所有长度的个数进行卷积(\ ...

  9. hdu 4609 3-idiots——FFT

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4609 答案就是随便选三条边的方案 - 不合法的方案. 不合法的方案就是算出 x+y = k 的方案数 g[ ...

随机推荐

  1. .net WCF简单练习

    之前一直没接触过WCF这个东西,由于是初学WCF没有深入研究其原理,只是写了一个demo WCF服务用于两个不同项目中的调用,在这里我举例项目A调用WCF服务实现查询数据功能. 第一步:创建数据库,有 ...

  2. C++ 数组和字符串

    数组和字符串的基本知识 目录 一.数组的声明 二.字符串 一.数组的声明 1.1.存储在每个元素中的值得类型: 1.2.数组名: 1.3.数组中的元素数. ];//short 数组元素值的类型,a数组 ...

  3. hbuilder 打包 vueAPP

    1:设置状态栏颜色 在manifest.json 找到 plus 下添加 "statusbar": { "immersed": true/*沉浸式状态栏*/ 设 ...

  4. 多功能网页刷新工具,刷pv工具

    多功能网页刷新工具,刷pv工具,在线刷流量,刷PV,刷UV小牛刷新助手功能介绍:1.设置多个刷新网页地址.2.设置刷新时间3.开始工作4.其他操作:老板键:打开时自动刷新:置系统托盘5.可手动输入地址 ...

  5. jmeter入门简介(一)

    简介 Apache JMeter是100%纯JAVA桌面应用程序,被设计为用于测试CS/BS的软件.它可以用来测试静态和动态资源的性能,可用于模拟大量负载来测试一台服务器,网络或者对象的健壮性或者分析 ...

  6. Android 网络编程的陷阱

    陷阱一,不要在主线程或者UI线程中建立网络连接 Androd4.0以后,不允许在主线程中建立网络连接,不然会出现莫名其妙的程序退出情况.正确的做法是在主线程中,创建新的线程来运行网络连接程序. // ...

  7. React中this.props的主要属性

    this.props主要包含:history属性.location属性.match属性 ①history属性又包含 ②location属性又包含 ③match属性又包含

  8. Servle第四篇(会话技术之cookie)

    会话技术 什么是会话技术 基本概念: 指用户开一个浏览器,访问一个网站,只要不关闭该浏览器,不管该用户点击多少个超链接,访问多少资源,直到用户关闭浏览器,整个这个过程我们称为一次会话. 为什么我们要使 ...

  9. Distance on the tree

    Distance on the tree https://nanti.jisuanke.com/t/38229 DSM(Data Structure Master) once learned abou ...

  10. Scrapy中集成selenium

    面对众多动态网站比如说淘宝等,一般情况下用selenium最好 那么如何集成selenium到scrapy中呢? 因为每一次request的请求都要经过中间件,所以写在中间件中最为合适 from se ...