首页 > 其他分享 >HDU 4609

HDU 4609

时间:2023-09-20 09:12:16浏览次数:46  
标签:HDU int 元素 leq 4609 lstn 三角形 pi

题目链接

description

给定一个长度为 \(n\) 的序列 \(A\),元素值域大小为 \(10^5\)。求从中任选三个不同位置的元素,以它们的值为三边能够成三角形的概率。

solution

设有 \(cnt\) 种选三个不同的元素构成三角形的方案,则答案显然为 \(\dfrac{6cnt}{n(n-1)(n-2)}\)。

\(a,b,c(a\leq b\leq c)\) 能作为三角形三边的充要条件是 \(a+b>c\)。但是由于要求 \(a\leq b\leq c\),枚举 \(a+b\),我们不好直接统计出有多少个 \(c\) 满足偏序关系且 \(a+b>c\)。

但是可以反着来,统计有多少对 \((a,b,c)\) 构不成三角形,也就是 \(a+b\leq c\)。这就很好统计了。预处理 \(cc_p\) 表示大于等于 \(p\) 的元素的数量,然后构造多项式统计 \(a+b=p\) 的数量 \(x_p\),枚举 \(p\),把 \(cc_px_p\) 贡献到答案。输出时记得把答案变成合法的概率。

code

#include<bits/stdc++.h>

using namespace std;

const int N=(1<<22)+10;

typedef complex<double> E;
const double pi=acos(-1);

int lstn,rev[N];
void prework(int n){
  if(lstn==n) return ;
  for(int i=1; i<n; i++) rev[i]=(rev[i^(i&-i)]|(1<<(__lg(n)-__lg(i&-i)-1)));
  return lstn=n,void();
}

void fft(E *p,int n,int op){
  prework(n);
  for(int i=1; i<n; i++) if(i<rev[i]) swap(p[i],p[rev[i]]);

  for(int i=2; i<=n; i<<=1){
    int len=i>>1;
    E w=cos(2*pi/i)+1i*sin(2*pi/i);
    if(op==-1) w-=2i*sin(2*pi/i);
    for(int pos=0; pos<n; pos+=i){
      E now=1;
      for(int j=pos; j<pos+len; j++,now=now*w){
        auto u=p[j],v=p[j+len]*now;
        p[j]=u+v,p[j+len]=u-v;
      }
    }
  }

  if(op==-1){
    for(int i=0; i<n; i++) p[i]/=n;
  }
}

E a[N];
long long n,cnt1,s[N],maxa,cnt2,cc[N];

void solve(){
  cin>>n;
  cnt2=0;
  cnt1=n*(n-1)*(n-2)/6;
  maxa=0;
  for(int i=1; i<=n; i++){
    int x;
    scanf("%d",&x);
    s[x]++; cc[x]++;
    a[x]+=1.0;
    maxa=max(maxa,x*1ll);
  }

  for(int i=maxa-1; ~i; i--) s[i]+=s[i+1];

  int t=1;
  for(;t<=maxa*2;t<<=1) ;

  fft(a,t,1);
  for(int i=0; i<t; i++) a[i]=a[i]*a[i];
  fft(a,t,-1);

  for(int i=0; i<t; i++){
    long long x=(long long)(a[i].real()+0.5);
    if(i%2==0){
       x-=(cc[i/2]);
    }
    x/=2;
    cnt2+=s[i]*x;
  }

  printf("%.7lf\n",((double)(cnt1-cnt2))/cnt1);

  memset(s,0,sizeof(long long)*(maxa+10));
  memset(cc,0,sizeof(long long)*(maxa+10));
  memset(a,0,sizeof(E)*(t+1));
}

int main(){

  int T;
  cin>>T;
  while(T--) solve();

  return 0;
}

标签:HDU,int,元素,leq,4609,lstn,三角形,pi
From: https://www.cnblogs.com/FreshOrange/p/17716419.html

相关文章

  • HDU 1054 Strategic Game 树形DP/二分图匹配
    第一次写博文,想了半天就拿一道dp/graph的题作为处作吧此题有两种常见解法(题意比较简单,就不赘述)1.二分图最大匹配       此题等价于问一棵树中最小点覆盖数。树形结构可以把它看做是一个二分图,一个点集为奇数层,另一个点集为偶数层,显然满足二分图定义,可以套用求二分图最小点......
  • hdu 4712 Hamming Distance-----随机
    计算出二进制数中有多少个1:数据范围太大,想到可以随机如果你在第一次调用rand()之前没有调用srand(),那么系统会为你自动调用srand()。------百度rand#include<cstdio>#include<cstring>#include<algorithm>#include<time.h>usingnamespacestd;constintN=1e5+10......
  • hdu 4705 Y
    dfs1.要#pragmacomment(linker,"/STACK:16777216")2.扩栈要vc++编译环境3.不能用%lld,要用%I64d,无语。。#pragmacomment(linker,"/STACK:16777216")//手动扩栈#include<stdio.h>#include<string.h>#include<stdlib.h>#include<vector>using......
  • hdu-3926-Hand in Hand-并查集
    判断两个图是否同构。。用了并查集。。#include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<queue>#include<stack>usingnamespacestd;#definelllonglongintn,m;structnode{ intfu; intsum; intf......
  • HDU 2955 Robberies
    01背包银行总钱数==容量V概率可以算安全的概率p=1-p;#include<stdio.h>#include<string.h>#include<algorithm>usingnamespacestd;doublepp,p[10001],f[10001];intv[10001];intmain(){ intt; scanf("%d",&t); while(t--){ intn,j,i,k,sum......
  • hdu 1372 Knight Moves 骑士的移动 bfs--马走日
    #include<stdio.h>#include<string.h>#include<queue>usingnamespacestd;charss[3],ee[3];intx1,y1,x2,y2;structpos{intx,y,step;}sta,end;intf[10][10];intdir[8][2]={1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-1};boolfan......
  • 二叉树 遍历 hdu-1710-Binary Tree Traversals
    给出二叉树的前序遍历和中序遍历,求后序遍历。。 算法:由前序遍历的第一个元素可确定左、右子树的根节点,参照中序遍历又可进一步确定子树的左、右子树元素。如此递归地参照两个遍历序列,最终构造出二叉树。 由前序和中序结果求后序遍历结果树的遍历: 给你一棵树的先......
  • hdu1400/acwing 291 Mondriaan's Dream
    题意描述:给定一块n*m的区域,用1*2的长方形填充,长方形可以横着或竖着摆,问一共有多少种填充方案具体思路:题意没什么好说的,简单易懂,很经典的一类状态压缩问题(在棋盘中求填充方案)。观察数据,满足n,m都比较小,但是搜索的复杂度大到无法接受,考虑使用状态压缩求解此类问题......
  • [HDU4117] GRE
    RecentlyGeorgeispreparingfortheGraduateRecordExaminations(GREforshort).Obviouslythemostimportantthingisrecitingthewords.NowGeorgeisworkingonawordlistcontaining\(N\)words.Hehassopooramemorythatitistoohardforhim......
  • HDU - 7187-Slipper
    HDU-7187-Slipper(最短路、建图优化)题意:给出n个结点,n-1条无向边,经过每条边的代价为w,以结点1为根节点的树,对于相差k层的结点,可以花费代价p抵达,问结点s到t的最短路径。分析:考虑对于每层的每个点建立到相差k层的点的边,极端情况为$O(n^2)$的复杂度,需要优化。考虑对于每层......