表示敲完多项式乘法&高精度乘法两道FFT模板题后就开始来磕这题了

这题相对而言应该不算模板题 不过神犇们肯定还是一眼看穿

如果原OJ访问速度较慢的话可以考虑戳这里

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=153505

-------------------------------------------------------------------------------------------------------------

构造什么的 还是太巧妙了

比如对于这样的样例

2

1 2

3 4

可以构造出这样的矩阵(原矩阵在左上角 额外的矩阵在右下角 且两矩阵不相交且中心对称)

a矩阵               b矩阵

1 2 0 0           0 0 0 0

3 4 0 0           0 0 0 0

0 0 0 0           0 0 4 3

0 0 0 0           0 0 2 1

如果变为多项式的话 就是这样

多项式a  1 2 0 0 3 4 0 0 0 0 0 0 0 0 0 0

多项式b  0 0 0 0 0 0 0 0 0 0 4 3 0 0 2 1

当我们把它们乘起来后 会发现贡献到同一项点对的距离是一样的

当n不为2的整数幂的时候 可以像fft模板题一样 通过补零来进行构造

构造出这两个多项式后 我们就可以直接套fft模板 然后对结果多项式的有效的位数都访问一次即可统计出答案

-------------------------------------------------------------------------------------------------------------

不过A掉后还是有一个问题还没弄懂

设多项式a,b的长度为n 那么按照常规的多项式乘法 我们应该把他们后面都补上n个0

因为结果多项式的长度为n×2 (虽然实际有效位数比n还小)

然而我参考的某AC代码 并没有用n×2的长度 而是只用到n的长度让结果 “自然溢出”了 (不是标准说法)

而且貌似n位之后的部分溢出到前面几位了

如果有神犇知道是为什么的话希望能回复下

#include <bits/stdc++.h>
using namespace std;
const int N=<<,M=<<;
const double pi=acos(-);
typedef complex <double> E;
int mp[M][M];
int n,m,L;
int R[N];
long long cnt[N];
E a[N],b[N];
int sum;
double ans;
void fft(E *A,int f)
{
for(int i=;i<n;++i)
if(i<R[i])
swap(A[i],A[R[i]]);
for(int i=;i<n;i<<=)
{
E wn(cos(pi/i),sin(pi/i)*f);
for(int p=i<<,j=;j<n;j+=p)
{
E w(,);
for(int k=;k<i;++k,w*=wn)
{
E x=A[j+k],y=w*A[j+k+i];
A[j+k]=x+y;
A[j+k+i]=x-y;
}
}
}
}
int getdist(int x,int y)
{
return x*x+y*y;
}
int main()
{
scanf("%d",&m);
for(n=;n<m*;n<<=)
++L;
for(int i=;i<m;++i)
for(int j=;j<m;++j)
{
scanf("%d",&mp[i][j]);
sum+=mp[i][j];
cnt[]-=mp[i][j];
a[i*n+j]=mp[i][j];
b[(n-i-)*n+(n-j-)]=mp[i][j];
}
n=n*n*;
L=L*+;
for(int i=;i<n;++i)
R[i]=(R[i>>]>>)|((i&)<<(L-));
fft(a,);
fft(b,);
for(int i=;i<n;++i)
a[i]*=b[i];
fft(a,-);
int tn=sqrt(n/)/,x,y;
x=tn;
y=tn;
for(int i=*tn*tn-*tn-;i;--i)
{
int tmp1;
if(y>=tn)
tmp1=getdist(x-(tn*-),y-(tn*-));
else
tmp1=getdist(x--(tn*-),tn*--(tn*--y-));
double tmp2=a[x*tn*+y].real()/n;
ans+=sqrt(tmp1)*tmp2;
cnt[tmp1]+=tmp2+0.5;
x+=(y==tn*-);
y=(y==tn*-?:y+);
}
printf("%.10f\n",ans/((long long)sum*(sum-)));
int flag=;
for(int i=;i<m*m*;++i)
if(cnt[i])
{
printf("%d %lld\n",i,cnt[i]/);
if(++flag>=)
break;
}
return ;
}

还是把参考的那个只用了n的长度的多项式的神犇的代码贴一下(如果原作者有意见的话我会删掉的……)

#include<stdio.h>
#include<algorithm>
#include<complex>
#include<vector>
#include<math.h>
#include<map>
using namespace std;
const double PI = 4.0*atan(1.0);
typedef complex<double> Complex;
const Complex I(, );
void fft(int n, double theta, Complex a[]) {
for (int m = n; m >= ; m >>= ) {
int mh = m >> ;
for (int i = ; i < mh; i++) {
Complex w = exp(i*theta*I);
for (int j = i; j < n; j += m) {
int k = j + mh;
Complex x = a[j] - a[k];
a[j] += a[k];
a[k] = w * x;
}
}
theta *= ;
}
int i = ;
for (int j = ; j < n - ; j++) {
for (int k = n >> ; k > (i ^= k); k >>= );
if (j < i) swap(a[i], a[j]);
}
}
int b[][];
Complex p[<<];
Complex q[<<];
int main(){
int a;
scanf("%d",&a);
int cnt=;
for(int i=;i<a;i++){
for(int j=;j<a;j++)scanf("%d",&b[i][j]);
}
int n=;
while(n<a*)n*=;
for(int i=;i<a;i++)for(int j=;j<a;j++){
cnt+=b[i][j];
p[i*n+j]=q[(n--i)*n+(n--j)]=Complex(b[i][j],);
}
fft(n*n,*PI/n/n,p);
fft(n*n,*PI/n/n,q); for(int i=;i<n*n;i++)p[i]=p[i]*q[i];
fft(n*n,-*PI/n/n,p);
for(int i=;i<n*n;i++)p[i]/=n*n;
/*for(int i=0;i<n;i++){
for(int j=0;j<n;j++)printf("%.0f ",p[i*n+j].real());
printf("\n");
}*/
double ret=;
long long sz=;
map<int,long long>m;
for(int i=;i<n*n;i++){
int pi=i/n;
int pj=i%n;
if(pj>=n/)continue;
if(pj==&&pi>=n/)continue;
long long v=(long long)(p[n*n--i].real()+0.5);
if(i==){v-=cnt;v/=;}
if(!v)continue; int I=min(pi,n-pi);
int J=pj;
int t=I*I+J*J;
if(m.count(t)){
m[t]+=v;
}else{
m[t]=v;
}
sz+=v;
ret+=sqrt(t)*v;
}
printf("%.12f\n",ret/sz);
int at=;
for(map<int,long long>::iterator it=m.begin();it!=m.end();it++){
if(at==)break;
at++;
printf("%d %lld\n",(*it).first,(*it).second);
}
}

AIZU 2560 [想法题]的更多相关文章

  1. HDU 4972 Bisharp and Charizard 想法题

    Bisharp and Charizard Time Limit: 1 Sec  Memory Limit: 256 MB Description Dragon is watching NBA. He ...

  2. CodeForces 111B - Petya and Divisors 统计..想法题

    找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...

  3. HDU - 5806 NanoApe Loves Sequence Ⅱ 想法题

    http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出, ...

  4. HDU - 5969 最大的位或 想法题

    http://acm.hdu.edu.cn/showproblem.php?pid=5969 (合肥)区域赛签到题...orz 题意:给你l,r,求x|y的max,x,y满足l<=x<=y ...

  5. HDU 4193 Non-negative Partial Sums(想法题,单调队列)

    HDU 4193 题意:给n个数字组成的序列(n <= 10^6).求该序列的循环同构序列中,有多少个序列的随意前i项和均大于或等于0. 思路: 这题看到数据规模认为仅仅能用最多O(nlogn) ...

  6. CodeForces - 156B Suspects 逻辑 线性 想法 题

    题意:有1~N,n(1e5)个嫌疑人,有m个人说真话,每个人的陈述都形如X是凶手,或X不是凶手.现在给出n,m及n个陈述(以+x/-X表示)要求输出每个人说的话是true ,false or notd ...

  7. 2016华中农业大学预赛 E 想法题

    Problem E: Balance Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 205  Solved: 64[Submit][Status][We ...

  8. codeforces 657C - Bear and Contribution [想法题]

    题目链接: http://codeforces.com/problemset/problem/657/C ----------------------------------------------- ...

  9. POJ 1066 Treasure Hunt [想法题]

    题目链接: http://poj.org/problem?id=1066 --------------------------------------------------------------- ...

随机推荐

  1. mysql时间增加一年

    update siteserver_content_57 set AddDate=DATE_ADD(AddDate,INTERVAL 2 year),LastHitsDate=DATE_ADD(Las ...

  2. java静态方法使用泛型

    用法 import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.ut ...

  3. vue 项目,获取手机验证码和图形验证码(iviewUI框架)

    1.编辑获取验证码模块 <Form ref="phoneFormItem" :model="phoneFormItem" :label-width=&qu ...

  4. vue 移动端列表筛选功能实现

    最近兴趣所致,打算使用vant搭建一个webapp,由于需要使用列表筛选,没有找到合适组件,于是写了一个简单的功能,权当记录. 效果如下:        HTML: <div class=&qu ...

  5. js 程序执行与顺序实现详解

    JavaScript是一种描述型脚本语言,由浏览器进行动态的解析与执行,浏览器对于不同的方式有不同的解析顺序,详细介绍如下,感兴趣的朋友可以参考下哈 函数的声明和调用 JavaScript是一种描述型 ...

  6. c#同时验证手机号和座机号正则

    string strPatern2= @"(^(\d{3,4}-)?\d{6,8}$)"; string strPatern = @"(^1[3-8]\d{9}$|^\d ...

  7. 快速的统计千万级别uv

    菜菜,咱们网站现在有多少PV和UV了? Y总,咱们没有统计pv和uv的系统,预估大约有一千万uv吧 写一个统计uv和pv的系统吧 网上有现成的,直接接入一个不行吗? 别人的不太放心,毕竟自己写的,自己 ...

  8. shell条件判断命令test

  9. https://blog.csdn.net/eguid_1/article/category/6270094

    https://blog.csdn.net/eguid_1/article/category/6270094

  10. Vue列表渲染-变异方法

    Vue 包含一组观察数组的变异方法,变异方法 (mutation method),顾名思义,会改变被这些方法调用的原始数组 所以它们也将会触发视图更新.这些方法如下: push() pop() shi ...