[MUTC2013]idiots
嘟嘟嘟
首先\(O(n ^ 2)\)大家都会,枚举最长边,然后找两条短边满足两边之大于第三边即可。
然后估计就没法优化了。
正难则反,如果枚举的两条短边小于等于第三边会怎么样呢?发现\(a_i \leqslant 10 ^ 5\),那就可以FFT求出凑出每一条边的方案了,记为\(f(i)\)。不过还要减去自己配自己的情况,以及每一种方案实际上被算了两遍。
那么我们可以枚举最长边,然后所有不合法的方案就是\(cnt(i) * \sum _{j = 1} ^ {i} f(j)\)了,\(cnt(i)\)表示长度为\(i\)的边的个数。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cctype>
#include<map>
#include<queue>
#include<vector>
#include<assert.h>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 3e5 + 5;
const db PI = acos(-1);
In ll read()
{
ll ans = 0;
char ch = getchar(), las = ' ';
while(!isdigit(ch)) las = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(las == '-') ans = -ans;
return ans;
}
In void write(ll x)
{
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
freopen("ha.in", "r", stdin);
freopen("ha.out", "w", stdout);
#endif
}
int n, Max = 0, a[maxn], cnt[maxn];
ll ans[maxn];
int rev[maxn];
struct Comp
{
db x, y;
In Comp operator + (const Comp& oth)const
{
return (Comp){x + oth.x, y + oth.y};
}
In Comp operator - (const Comp& oth)const
{
return (Comp){x - oth.x, y - oth.y};
}
In Comp operator * (const Comp& oth)const
{
return (Comp){x * oth.x - y * oth.y, x * oth.y + y * oth.x};
}
friend In void swap(Comp& a, Comp& b)
{
swap(a.x, b.x); swap(a.y, b.y);
}
}b[maxn], c[maxn], ret[maxn];
In void fft(Comp* a, int len, int flg)
{
for(int i = 0; i < len; ++i) if(i < rev[i]) swap(a[i], a[rev[i]]);
for(int i = 1; i < len; i <<= 1)
{
Comp omg = (Comp){cos(PI / i), sin(PI / i) * flg};
for(int j = 0; j < len; j += (i << 1))
{
Comp o = (Comp){1, 0};
for(int k = 0; k < i; ++k, o = o * omg)
{
Comp tp1 = a[k + j], tp2 = o * a[k + j + i];
a[k + j] = tp1 + tp2, a[k + j + i] = tp1 - tp2;
}
}
}
}
int main()
{
// MYFILE();
int T = read();
while(T--)
{
n = read(); Mem(cnt, 0); Max = 0;
for(int i = 0; i < maxn; ++i) b[i] = c[i] = (Comp){0, 0};
for(int i = 1; i <= n; ++i)
{
++cnt[a[i] = read()];
Max = max(Max, a[i]);
}
for(int i = 1; i <= Max; ++i) b[i] = c[i] = (Comp){1.0 * cnt[i], 0};
int len = 1, lim = 0;
while(len <= Max + Max) len <<= 1, ++lim;
for(int i = 0; i < len; ++i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (lim - 1));
fft(b, len, 1), fft(c, len, 1);
for(int i = 0; i < len; ++i) ret[i] = b[i] * c[i];
fft(ret, len, -1);
for(int i = 1; i <= Max; ++i)
{
ans[i] = (ll)(ret[i].x / len + 0.5);
if(!(i & 1)) ans[i] -= cnt[i >> 1];
ans[i] >>= 1;
}
ll ans1 = 0, sum = 0, ans2 = 1LL * n * (n - 1) * (n - 2) / 6;
for(int i = 1; i <= Max; ++i) sum += ans[i], ans1 += cnt[i] * sum;
printf("%.7lf\n", 1.0 * (ans2 - ans1) / ans2);
}
return 0;
}
[MUTC2013]idiots的更多相关文章
- bzoj 3513: [MUTC2013]idiots FFT
bzoj 3513: [MUTC2013]idiots FFT 链接 bzoj 思路 参考了学姐TRTTG的题解 统计合法方案,最后除以总方案. 合法方案要不好统计,统计不合法方案. \(a+b< ...
- bzoj千题计划168:bzoj3513: [MUTC2013]idiots
http://www.lydsy.com/JudgeOnline/problem.php?id=3513 组成三角形的条件:a+b>c 其中,a<c,b<c 若已知 两条线段之和=i ...
- bzoj 3513 [MUTC2013]idiots FFT 生成函数
[MUTC2013]idiots Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 806 Solved: 265[Submit][Status][Di ...
- BZOJ 3513: [MUTC2013]idiots
3513: [MUTC2013]idiots Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 476 Solved: 162[Submit][Stat ...
- BZOJ3513: [MUTC2013]idiots
Description 给定n个长度分别为a_i的木棒,问随机选择3个木棒能够拼成三角形的概率. Input 第一行T(T<=100),表示数据组数.接下来若干行描述T组数据,每组数据第一行是n ...
- BZOJ3513[MUTC2013]idiots——FFT+生成函数
题目描述 给定n个长度分别为a_i的木棒,问随机选择3个木棒能够拼成三角形的概率. 输入 第一行T(T<=100),表示数据组数. 接下来若干行描述T组数据,每组数据第一行是n,接下来一行有n个 ...
- 2019.01.02 bzoj3513: [MUTC2013]idiots(fft)
传送门 fftfftfft经典题. 题意简述:给定nnn个长度分别为aia_iai的木棒,问随机选择3个木棒能够拼成三角形的概率. 思路:考虑对于木棒构造出生成函数然后可以fftfftfft出两个木 ...
- 【刷题】BZOJ 3513 [MUTC2013]idiots
Description 给定n个长度分别为a_i的木棒,问随机选择3个木棒能够拼成三角形的概率. Input 第一行T(T<=100),表示数据组数. 接下来若干行描述T组数据,每组数据第一行是 ...
- 【bzoj3513】[MUTC2013]idiots FFT
题目描述 给定n个长度分别为a_i的木棒,问随机选择3个木棒能够拼成三角形的概率. 输入 第一行T(T<=100),表示数据组数. 接下来若干行描述T组数据,每组数据第一行是n,接下来一行有n个 ...
- bzoj 3513: [MUTC2013]idiots【生成函数+FFT】
想了好长时间最后发现真是石乐志 第一反应就是两边之和大于第三边,但是这个东西必须要满足三次-- 任意的两边之和合通过生成函数套路+FFT求出来(记得去掉重复选取的),然后这任意两边之和大于任意第三边可 ...
随机推荐
- js new到底做了什么?如何重写new?(转)
转自:https://blog.csdn.net/lyt_angularjs/article/details/86623988
- Mac 下编译 Hadoop
Mac 下编译 Hadoop-2.9.2 系统环境 系统: Mac OS_10.14.4 maven: Apache Maven 3.6.0 jdk: jdk_1.8.0_201 ProtocolBu ...
- 前端之:传统的DOM是如何渲染的?
a.纯后端渲染:页面发送请求,后端服务器中将数据拼成完整DOM树,并转换成一个字节流作为HTTP Response的body返回给浏览器.优点在于 返回的HTTP Response是包含着全部页面内容 ...
- 监听iframe加载完成
用 @load="loading" 在Vue里面写了一个界面,有一个iframe标签, iframe加载其他网站, <iframe @load="loading&q ...
- js时间格式化和相互转换
1. Thu Mar 07 2019 12:00:00 GMT+0800 (中国标准时间) 转换为 2019-03-07 12:00:00 const d = new Date(Thu Mar 07 ...
- 解决mysql登录警告问题
一.前言 我们在登录mysql的时候经常会看到一句警告: Warning: Using a password on the command line interface can be insecure ...
- Python3 实例
一直以来,总想写些什么,但不知从何处落笔. 今儿个仓促,也不知道怎么写,就把手里练习过的例子,整理了一下. 希望对初学者有用,都是非常基础的例子,很适合初练. 好了,Follow me. 一.Pyth ...
- Hive调优笔记
Hive调优 先记录了这么多,日后如果有遇到,再补充. fetch模式 <property> <name>hive.fetch.task.conversion</name ...
- Django 使用form组件对文件上传
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 详解Linux磁盘管理与文件系统
磁盘基础 硬盘结构 物理结构 盘片:硬盘有多个盘片,每盘片 2 面. 磁头:每面一个磁头. 数据结构 扇区:磁盘上的每个磁道被等分为若干个弧段,这些弧段便是硬盘的扇区. 硬盘的第一个扇区,叫做引导扇区 ...