【HDU 4609】3-idiots
1.题目链接。题目大意:给出n根棍子,现在从中任意选三种,能够组成三角形的概率。
2.其实就是说在这些棍子能够组成多少种三角形。我们预处理a+b,然后枚举第三条边c.预处理a+b采用FFT加速即可。然后统计答案的时候,注意把不合法的减了。
#include<bits/stdc++.h>
using namespace std;
#pragma warning(disable:4996)
const double PI = acos(-1.0);
struct Complex
{double r, i;Complex(double _r = 0, double _i = 0){r = _r; i = _i;}Complex operator +(const Complex &b){return Complex(r + b.r, i + b.i);}Complex operator -(const Complex &b){return Complex(r - b.r, i - b.i);}Complex operator *(const Complex &b){return Complex(r*b.r - i * b.i, r*b.i + i * b.r);}
};
void change(Complex y[], int len)
{int i, j, k;for (i = 1, j = len / 2; i < len - 1; i++){if (i < j)swap(y[i], y[j]);k = len / 2;while (j >= k){j -= k;k /= 2;}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 (on == -1)for (int i = 0; i < len; i++)y[i].r /= len;
}const int MAXN = 400040;
Complex x1[MAXN];
int a[MAXN / 4];
long long num[MAXN];//100000*100000会超int
long long sum[MAXN];int main()
{int T;int n;scanf("%d", &T);while (T--){scanf("%d", &n);memset(num, 0, sizeof(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] = (long long)(x1[i].r + 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];long long cnt = 0;for (int i = 0; i < n; i++){cnt += sum[len] - sum[a[i]];cnt -= (long long)(n - 1 - i)*i;cnt -= (n - 1);cnt -= (long long)(n - 1 - i)*(n - i - 2) / 2;}long long tot = (long long)n*(n - 1)*(n - 2) / 6;printf("%.7lf\n", (double)cnt / tot);}return 0;
}
【HDU 4609】3-idiots相关推荐
- 大数加法【HDU 1002】
大数加法模板 一般的加法只要int类型的两数直接相加即可,大一点的数可以设为long long类型,而超过长整型的数则属于大数问题了,大数加法其实也比较简单,利用数组实现就可以啦: 主要思想如下: ( ...
- 【 HDU - 5093】Battle ships(匈牙利算法,二分图匹配)
题干: Dear contestant, now you are an excellent navy commander, who is responsible of a tough mission ...
- 【HDU - 1455】Sticks (dfs + 剪枝)
题干: George took sticks of the same length and cut them randomly until all parts became at most 50 un ...
- 【HDU - 4006】The kth great number (优先队列,求第k大的数)
题干: Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to wri ...
- 【HDU - 4217 】Data Structure? (线段树求第k小数)
题干: Data structure is one of the basic skills for Computer Science students, which is a particular w ...
- 【HDU - 1754】I Hate It (线段树模板 单点覆盖更新+区间最大值查询)
题干: 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当 ...
- 【HDU 5765】Bonds(进制运算妙用)
[HDU 5765]Bonds(进制运算妙用) Bonds Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- 【HDU 5755】Gambler Bo(高斯消元)
[HDU 5755]Gambler Bo(高斯消元) Gambler Bo Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 131072 ...
- 【HDU 6973】Bookshop 树剖+平衡树
[HDU 6973]Bookshop 树剖+平衡树 [引言] 平衡树的题做得比较少,难得补一次神题,记录一下供以后学习. [题意] 给出一棵 nnn 个结点的树,每个结点有一个价值为 pip_ ...
- 【hdu 1043】Eight
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...
最新文章
- 北斗导航 | GPS卫星导航技术重要人物简介
- Java接口有时有结果 有时没有_《Java程序员面试笔试宝典》之为什么Java中有些接口没有任何方法...
- c语言 gbk字模点阵数组,GBK点阵显示字库的制作和使用
- python如何画出多个独立的图使用turtle_从选项列表一次绘制多个形状(Python-Turtle图形)?...
- #C++初学记录(阶乘#递归)
- OpenCV2:总结篇 imgproc(图像处理模块)
- python脚本打包成linux命令_python怎么打包生成linux命令行可用软件?
- jQuery Event.delegateTarget 属性详解
- 终极版Servlet——我只能提示您路过别错过
- 清华大学 计算机系 研究生导师,清华大学计算机科学与技术系研究生导师简介-胡事民...
- 日期格式化中的大小写区别
- 教你微信怎么加更多好友的绝佳方法
- 微信CRM系统对客户关系管理有什么好处?
- Gensim库生成与导入W2V模型_CodingPark编程公园
- hi3516v300gpio驱动编译遇到的问题
- IntelliJ IDEA类和方法注释模板配置
- dos中ren命令与通配符的使用
- Google Play登录SDK接入
- scn,headroom
- 重庆邮电工商管理类转计算机专业,2021年重庆邮电大学转专业,大一新生转专业和入学考试...