HDU4609 FFT+组合计数
HDU4609 FFT+组合计数
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4609
题意:
找出n根木棍中取出三根木棍可以组成三角形的概率
题解:
我们统计每种长度的棍子的个数
我们对于长度就有一个多项式
\]
我们考虑两根棍子可以组成所有长度的方案数
所以我们对num数组求一次FFT
两根棍子组成长度的上界是\(len_{max}*2\)
可能存在棍子重复组合的情况,这个时候我们需要去重
去掉两种重复的情况
1.自己和自己组合 即去除a[i]+a[i]的情况
2.A和B组合 B又和A组合的情况 这种时候每个组合/2即可
然后通过组合数计数即可,tips:在过程中可能爆int
代码:
#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1);
LL quick_pow(LL x, LL y) {
LL ans = 1;
while(y) {
if(y & 1) {
ans = ans * x % mod;
} x = x * x % mod;
y >>= 1;
} return ans;
}
struct complex {
double x, y;
complex(double xx = 0, double yy = 0) {
x = xx, y = yy;
}
} x[maxn];
int a[maxn];
complex operator + (complex a, complex b) {
return complex(a.x + b.x, a.y + b.y);
}
complex operator - (complex a, complex b) {
return complex(a.x - b.x, a.y - b.y);
}
complex operator * (complex a, complex b) {
return complex(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
int n, m;
int l, r[maxn];
int limit = 1;
void fft(complex *A, int type) {
for(int i = 0; i < limit; i++) {
if(i < r[i]) swap(A[i], A[r[i]]);
}
for(int mid = 1; mid < limit; mid <<= 1) {
complex Wn(cos(Pi / mid), type * sin(Pi / mid));
for(int R = mid << 1, j = 0; j < limit; j += R) {
complex w(1, 0);
for(int k = 0; k < mid; k++, w = w * Wn) {
complex x = A[j + k], y = w * A[j + mid + k];
A[j + k] = x + y;
A[j + k + mid] = x - y;
}
}
}
}
LL num[maxn];//100000*100000会超int
LL sum[maxn];
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
int T;
scanf("%d", &T);
while(T--) {
int n;
memset(num, 0, sizeof(num));
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
num[a[i]]++;
}
sort(a, a + n);
int len1 = a[n - 1] + 1;
limit = 1;
l = 0;
while(limit < 2 * len1) limit <<= 1, l++;
for(int i = 0; i < len1; i++) {
x[i] = complex(num[i], 0);
}
for(int i = len1; i < limit ; i++) {
x[i] = complex(0, 0);
}
for(int i = 0; i < limit; i++) {
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));
}
fft(x, 1);
for(int i = 0; i < limit; i++) {
x[i] = x[i] * x[i];
}
fft(x, -1);
for(int i = 0; i < limit; i++) {
x[i].x /= limit;
}
for(int i = 0; i < limit; i++) {
num[i] = (LL)(x[i].x + 0.5);
// debug1(num[i]);
}
limit = 2 * a[n - 1];
//去重,去除 a_i,a_i这种情况
for(int i = 0; i < n; i++) {
num[a[i] + a[i]]--;
}
//去重,去除 (a_i,a_j),(a_j,a_i)这种情况
for(int i = 1; i <= limit; i++) {
num[i] /= 2;
}
sum[0] = 0;
for(int i = 1; i <= limit; i++)
sum[i] = sum[i - 1] + num[i];
LL cnt = 0;
for(int i = 0; i < n; i++) {
cnt += sum[limit] - 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("%.7f\n", (double)cnt / tot);
}
return 0;
}
HDU4609 FFT+组合计数的更多相关文章
- BZOJ 4555: [Tjoi2016&Heoi2016]求和 [分治FFT 组合计数 | 多项式求逆]
4555: [Tjoi2016&Heoi2016]求和 题意:求\[ \sum_{i=0}^n \sum_{j=0}^i S(i,j)\cdot 2^j\cdot j! \\ S是第二类斯特林 ...
- BZOJ 4555: [Tjoi2016&Heoi2016]求和 [FFT 组合计数 容斥原理]
4555: [Tjoi2016&Heoi2016]求和 题意:求\[ \sum_{i=0}^n \sum_{j=0}^i S(i,j)\cdot 2^j\cdot j! \\ S是第二类斯特林 ...
- BZOJ 2287. [HZOI 2015]疯狂的机器人 [FFT 组合计数]
2287. [HZOI 2015]疯狂的机器人 题意:从原点出发,走n次,每次上下左右不动,只能在第一象限,最后回到原点方案数 这不煞笔提,组合数写出来发现卷积NTT,然后没考虑第一象限gg 其实就是 ...
- 2019暑期金华集训 Day1 组合计数
自闭集训 Day1 组合计数 T1 \(n\le 10\):直接暴力枚举. \(n\le 32\):meet in the middle,如果左边选了\(x\),右边选了\(y\)(且\(x+y\le ...
- bzoj 2281 [Sdoi2011]黑白棋(博弈+组合计数)
黑白棋(game) [问题描述] 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色 ...
- 【BZOJ5491】[HNOI2019]多边形(模拟,组合计数)
[HNOI2019]多边形(模拟,组合计数) 题面 洛谷 题解 突然特别想骂人,本来我考场现切了的,结果WA了几个点,刚刚拿代码一看有个地方忘记取模了. 首先发现终止态一定是所有点都向\(n\)连边( ...
- [总结]数论和组合计数类数学相关(定理&证明&板子)
0 写在前面 0.0 前言 由于我太菜了,导致一些东西一学就忘,特开此文来记录下最让我头痛的数学相关问题. 一些引用的文字都注释了原文链接,若侵犯了您的权益,敬请告知:若文章中出现错误,也烦请告知. ...
- 【BZOJ5323】[JXOI2018]游戏(组合计数,线性筛)
[BZOJ5323][JXOI2018]游戏(组合计数,线性筛) 题面 BZOJ 洛谷 题解 显然要考虑的位置只有那些在\([l,r]\)中不存在任意一个约数的数. 假设这样的数有\(x\)个,那么剩 ...
- 【BZOJ5305】[HAOI2018]苹果树(组合计数)
[BZOJ5305][HAOI2018]苹果树(组合计数) 题面 BZOJ 洛谷 题解 考虑对于每条边计算贡献.每条边的贡献是\(size*(n-size)\). 对于某个点\(u\),如果它有一棵大 ...
随机推荐
- 2019-9-2-win10-uwp-随着数字变化颜色控件
title author date CreateTime categories win10 uwp 随着数字变化颜色控件 lindexi 2019-09-02 12:57:38 +0800 2018- ...
- oracle使用profile管理用户口令
概述:profile是口令限制.资源限制的命令集合,当建立数据时,oracle会自动建立名称为default的profile,当建立用户没有指定profile选项,那么oracle就会将default ...
- [转] android自定义布局中的平滑移动
无意中搜索到这篇文章,大概扫了一眼,知道是篇好文,先转载记录下来学习! 文章主要讲的是自定义view的写法心得. 转自:http://www.apkbus.com/android-48445-1-1. ...
- 从零学React Native之09可触摸组件
可触摸组件有: TouchableHighlight,TouchableNativeFeedback,TouchableOpacity,TouchableWithoutFeedback 1. Touc ...
- Win10系统使用Docker安装oracle并通过Navicat for oracle进行登录
一.安装Docker Linux系统可以直接采用命令进行Docker安装: Win7系统安装Dokcer实际通过Boot2Docker在Windows下安装一个VirtualBox来实现: Boot2 ...
- 跳出手掌心--如何立即触发UIButton边界事件
http://www.cocoachina.com/ios/20150611/12082.html 最近在使用UIButton的过程中遇到一个问题,我想要获得手指拖动button并离开button边界 ...
- 阿里云亮相2019联通合作伙伴大会,边缘计算等3款云产品助力5G时代产业数字化转型
4月23日,2019中国联通合作伙伴大会在上海正式开幕,本次大会以“合作不设限,共筑新生态”为主题,涉及5G.边缘计算.云计算.物联网.新媒体.人工智能.互联网化等各领域超过600家合作伙伴与3万名各 ...
- JAVA内存溢出解析(转)
JAVA内存溢出解析(转) 核心提示:原因有很多种,比如: 1.数据量过于庞大:死循环 :静态变量和静态方法过多:递归:无法确定是否被引用的对象: 2.虚拟机不回收内存(内存泄漏): 说白了就是程序运 ...
- php开发微信支付获取用户地址
http://mp.weixin.qq.com/s/uNpWE_Z5RZ48PDIWkmGBYQ 使用微信获取地址信息是和微信支付一道申请的,微信支付申请通过,就可以使用该功能. 微信商城中,使用微信 ...
- angularjs 自定义指令弹窗
(function() { 'use strict'; angular.module('frontierApp') .directive('confirmPopup', ['$timeout', Co ...