题意:

有一个\(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. visual studio2010中C#生成的,ArcGIS二次开发的basetool的dll,注册为COM组件tlb文件,并在arcmap中加载使用

    写了个标题好长啊~~~~ 这两天又认识了一个新玩意,记录一下下,啦啦啦~~~~~ 话说,认识arcgis快十年了,从桌面版到engine的二次开发,其实不过才认识到它的冰山一角, 它总是能带来很多还未 ...

  2. FusionCharts可使用JavaScript渲染iPhone/iPod/iPad图表

    FusionCharts使用JavaScript: FusionCharts允许用户创建建立JavaScript图表(也就是web上的HTML5 /Canvas图表).这个特性允许用户在不支持Flas ...

  3. #include< >和#include“ ”的区别

    < >引用的是编译器的类库路径里面的头文件 " "引用的是你程序目录的相对路径中的头文件 假如你编译器定义的自带头文件引用在C:\Keil\c51\INC\下面 则#i ...

  4. C# 常见的字符串操作

    例1: 遍历字符串中的每一个字符: string src = "aa-b - c-a - d-e- d-e- a- a-b-cc"; foreach(char c in src) ...

  5. php:生成的时间与本地电脑的时间不匹配

    如题,在php中我发现在数据库中的时间跟电脑上的时间没有同步 本地时间: 数据库中,生成的时间: 很明显,时分秒是对应不上去的,那么我们只要在php.ini中找到 这里被注释掉了,那我们就改成这样: ...

  6. IDA逆向:数组的逆向

    阅读<IDA Pro权威指南>第八章,整理的一些笔记,作为逆向的基础,可能有很多认识不足. //全局分配数组 *************************************** ...

  7. pta 编程题7 List Leaves

    其它pta数据结构编程题请参见:pta 这次的编程作业要求从上到下,从左到右输出一棵树的叶子节点,即树的层序遍历,用队列的方式来实现. 注意enqueue和dequeue函数参数为Queue & ...

  8. UVA 11040 Add bricks in the wall(线性组合)

    砖块上的数字最终都可以看作是最后一行的线性组合,独立变元最多9个. 这类题的一般做法,线性组合都可以列出方程然后高斯消元. 对于这道题,只要确定最后一行剩下的4个变量就好了,对于最后一行的j位置,它对 ...

  9. SRM 670 div2 A B C div1 A(贪心,子问题合并)

    A Cdgame brute force... B Drbalance 贪心,每次选最前面的-变成+,相当于后面所有的负值+2. C Treestrat 考虑集中去抓一个Red Token,以这个To ...

  10. python_7_while

    count=0 while True: print('count:',count) count+=1 # count=count+1 if count==500: break#结束整个循环