题意:

有一个\(N \times N\)的方阵,第\(x\)行第\(y\)列有\(C_{x,y}\)个点\((0 \leq C_{x,y} \leq 9)\)。

任选两个不同的点,求两点欧几里德距离的均值(或期望)。

然后按距离从小到大输出该距离的平方\(d_i\)和对应的点对数目\(c_i\)。

分析:

首先要化二维为一维,一般来讲给点\((x,y)\)编号\(x \times N+y(0\leq x, y < N)\)。

这里为了区分行和列从而方便计算距离,按照\(x \times 2N + y\)的方式给点编号。

这样对于两个点\((x_1,y_1)\)和\((x_2, y_2)\),对应编号分别为\(id_1 = x_1 \times 2N + y_1\)和\(id_2 = x_2 \times 2N + y_2(id_1 < id_2)\)。

两点之间的行距\(dx=\left \lceil \frac{id_1 - id_2 + N}{2} \right \rceil\)

两点之间的列距\(dy=\left | id_1 - id_2 -dx\times 2N \right |\)

然后用\(FFT\)计算两个多项式:

\[P(x)=\sum C_{i,j}x^{id_{i,j}}
\]

\[Q(x)=\sum C_{i,j}x^{-id_{i,j}}
\]

的乘积。

距离为\(0\)的点对注意去重或者单独计算。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <complex>
using namespace std; const double PI = acos(-1.0);
typedef long long LL;
typedef complex<double> Complex; void FFT(Complex* P, int n, int op) {
for(int i = 1, j = 0; i < n - 1; i++) {
for(int s = n; j ^= s >>= 1, ~j&s; );
if(i < j) swap(P[i], P[j]);
}
int log = 0;
while((1 << log) < n) log++;
for(int s = 0; s < log; s++) {
int m = 1 << s;
int m2 = m << 1;
Complex wm(cos(PI / m), sin(PI / m) * op);
for(int i = 0; i < n; i += m2) {
Complex w(1, 0);
for(int j = 0; j < m; j++, w *= wm) {
Complex u = P[i + j];
Complex t = P[i + j + m] * w;
P[i + j] = u + t;
P[i + j + m] = u - t;
}
}
}
if(op == -1) for(int i = 0; i < n; i++) P[i].real(P[i].real() / n);
} Complex P[2][1 << 22]; int n; LL cnt[2100000]; double dist(double x, double y) {
return sqrt(x * x + y * y);
} int main()
{
scanf("%d", &n);
int sum = 0;
int offset = (n - 1) * (n * 2 + 1);
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
int x; scanf("%d", &x);
sum += x;
cnt[0] += (x - 1) * x / 2;
int id = i * 2 * n + j;
P[0][id] = x;
P[1][offset-id] = x;
}
} int s = 1;
while(s < offset * 2 + 1) s <<= 1;
FFT(P[0], s, 1); FFT(P[1], s, 1);
for(int i = 0; i < s; i++) P[0][i] *= P[1][i];
FFT(P[0], s, -1); double ans = 0;
for(int i = 1; i <= offset; i++) {
LL t = (LL)(P[0][offset + i].real() + 0.5);
if(!t) continue;
int dx = ((i / n) + 1) >> 1;
int dy = abs(i - dx * n * 2);
ans += dist(dx, dy) * t;
cnt[dx*dx+dy*dy] += t;
} ans /= (double)sum * (sum - 1) / 2;
printf("%.10f\n", ans); int num = 0; int top = (n - 1) * (n - 1) * 2;
for(int i = 0; i <= top && num < 10000; i++) if(cnt[i]) {
printf("%d %lld\n", i, cnt[i]);
num++;
} return 0;
}

Aizu 2560 Point Distance FFT的更多相关文章

  1. AIZU 2560 [想法题]

    表示敲完多项式乘法&高精度乘法两道FFT模板题后就开始来磕这题了 这题相对而言应该不算模板题 不过神犇们肯定还是一眼看穿 如果原OJ访问速度较慢的话可以考虑戳这里 http://acm.hus ...

  2. CodeChef - PRIMEDST Prime Distance On Tree 树分治 + FFT

    Prime Distance On Tree Problem description. You are given a tree. If we select 2 distinct nodes unif ...

  3. codechef Prime Distance On Tree(树分治+FFT)

    题目链接:http://www.codechef.com/problems/PRIMEDST/ 题意:给出一棵树,边长度都是1.每次任意取出两个点(u,v),他们之间的长度为素数的概率为多大? 树分治 ...

  4. prime distance on a tree(点分治+fft)

    最裸的点分治+fft,调了好久,太菜了.... #include<iostream> #include<cstring> #include<cstdio> #inc ...

  5. [题解] Atcoder ABC 225 H Social Distance 2 生成函数,分治FFT

    题目 首先还没有安排座位的\(m-k\)个人之间是有顺序的,所以先把答案乘上\((m-k)!\),就可以把这些人看作不可区分的. 已经确定的k个人把所有座位分成了k+1段.对于第i段,如果我们能求出这 ...

  6. LA6886 Golf Bot(FFT)

    题目 Source https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page= ...

  7. Codeforces 528D Fuzzy Search(FFT)

    题目 Source http://codeforces.com/problemset/problem/528/D Description Leonid works for a small and pr ...

  8. LA4671 K-neighbor substrings(FFT + 字符串Hash)

    题目 Source http://acm.hust.edu.cn/vjudge/problem/19225 Description The Hamming distance between two s ...

  9. hdu 5885 FFT

    XM Reserves Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)T ...

随机推荐

  1. python_2开发简单爬虫

    2017年12月03日 16:43:01 独行侠的守望 阅读数:204 标签: python爬虫 更多个人分类: Python编辑版权声明:本文为博主原创文章,转载请注明文章链接. https://b ...

  2. git 如何生成 SSH 公钥

    1.打开你的git bash 窗口 2.进入.ssh目录:cd ~/.ssh 3.找到id_rsa.pub文件:ls 4.查看公钥:cat id_rsa.pub    或者vim id_rsa.pub ...

  3. CAS登录认证的简单介绍

    参考博客:  https://www.jianshu.com/p/8daeb20abb84 下面是CAS最基本的协议过程: 下面是CAS Web工作流程图: 名词解释 Ticket Grangting ...

  4. C#Udp组播

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...

  5. rmtree函数

    blog.csdn.net/my2010sam/article/details/8956353

  6. chrome中清除dns缓存

    chrome中清除dns缓存 http://rss.code-mire.com/item/1005.htm web开发经常要做各种host绑定的切换,firefox下有个DNS Flusher插件,但 ...

  7. 一个SAP顾问在美国的这些年

    今天的文章来自我的老乡宋浩,之前作为SAP顾问在美国工作多年.如今即将加入SAP成都研究院S4CRM开发团队.我们都是大邑人. 大邑县隶属于四川省成都市,位于成都平原西部,与邛崃山脉接壤.东与崇州市交 ...

  8. apply()技巧

    apply技巧:   可以将数组的每一项分别作为参数传入函数.   var value = [3,6,34,90]; alert(Math.max.apply(Math,value)); 这个技巧的关 ...

  9. 爬虫爬取代理IP池及代理IP的验证

    最近项目内容需要引入代理IP去爬取内容. 为了项目持续运行,需要不断构造.维护.验证代理IP. 为了绕过服务端对IP 和 频率的限制,为了阻止服务端获取真正的主机IP. 一.服务器如何获取客户端IP ...

  10. 【CF739E】Gosha is hunting(WQS二分套WQS二分)

    点此看题面 大致题意: 你有两种捕捉球(分别为\(A\)个和\(B\)个),要捕捉\(n\)个神奇宝贝,第\(i\)个神奇宝贝被第一种球捕捉的概率是\(s1_i\),被第二种球捕捉的概率是\(s2_i ...