AIZU 2560 [想法题]
表示敲完多项式乘法&高精度乘法两道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 [想法题]的更多相关文章
- HDU 4972 Bisharp and Charizard 想法题
Bisharp and Charizard Time Limit: 1 Sec Memory Limit: 256 MB Description Dragon is watching NBA. He ...
- CodeForces 111B - Petya and Divisors 统计..想法题
找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...
- HDU - 5806 NanoApe Loves Sequence Ⅱ 想法题
http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出, ...
- HDU - 5969 最大的位或 想法题
http://acm.hdu.edu.cn/showproblem.php?pid=5969 (合肥)区域赛签到题...orz 题意:给你l,r,求x|y的max,x,y满足l<=x<=y ...
- HDU 4193 Non-negative Partial Sums(想法题,单调队列)
HDU 4193 题意:给n个数字组成的序列(n <= 10^6).求该序列的循环同构序列中,有多少个序列的随意前i项和均大于或等于0. 思路: 这题看到数据规模认为仅仅能用最多O(nlogn) ...
- CodeForces - 156B Suspects 逻辑 线性 想法 题
题意:有1~N,n(1e5)个嫌疑人,有m个人说真话,每个人的陈述都形如X是凶手,或X不是凶手.现在给出n,m及n个陈述(以+x/-X表示)要求输出每个人说的话是true ,false or notd ...
- 2016华中农业大学预赛 E 想法题
Problem E: Balance Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 205 Solved: 64[Submit][Status][We ...
- codeforces 657C - Bear and Contribution [想法题]
题目链接: http://codeforces.com/problemset/problem/657/C ----------------------------------------------- ...
- POJ 1066 Treasure Hunt [想法题]
题目链接: http://poj.org/problem?id=1066 --------------------------------------------------------------- ...
随机推荐
- JS验证数字
//1.验证数字 var reg = new RegExp("^[0-9]*$"); //var reg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+ ...
- 厉害了,Google大神每天写多少行代码?
文章转自开源中国社区,编译自:Quora Quora上有个有趣的问题:Google工程师们每天写多少行代码? Google 的 AdMob 全栈工程师 Raymond Farias 在 Quora 发 ...
- [fw]Nvidia Linux Drive Privilege Escalation
/* Anonymous * * How to use: sudo rm -rf / * * greetz: djrbliss, kad, Ac1dB1tch3z, nVidia! * * Only ...
- Winfrom传值 分类: C# 2015-07-22 15:41 1人阅读 评论(0) 收藏
以前对WinForm窗体显示和窗体间传值了解不是很清楚 最近做了一些WinForm项目,把用到的相关知识整理如下 A.WinForm中窗体显示 显示窗体可以有以下2种方法: Form.Show ...
- 上传.cgi在252板子上跑
1.windows下写好.c程序 2.进入linux,准备交叉编译 arm-hisiv600-linux-gcc -g -Wall -o xxx.cgi xxx.c(交叉编译工具取决于板子) 3.把. ...
- ES6——字符串
1.多了两个方法 1)startsWith 2)endsWith 2.模板字符串(`..`)—— 方便字符串连接 `反单引号 1)可以直接把表达式塞进去 &a ...
- JSONP面试
jQuery 的 JSONP的原理是动态创建一个 script 标签,利用src 发送请求,获取数据 回调函数的键名叫做 callback 跟ajax没有关系 JSONP:主要是利用 script标 ...
- [三下五除二]在Eclipse上的JFinal_Demo
承接上回在IDEA的JFinal的项目的导入,今次同样是同一个文件,但在Eclipse上运行.在Eclipse上运行官网的JFinal的例子是及其快捷. 打开Eclipse,并进入如下的界面. 点击导 ...
- @Profile使用及SpringBoot获取profile值
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/Fmuma/article/details ...
- 基于英伟达Jetson TX1的GPU处理平台
基于英伟达Jetson TX1 GPU的HDMI图像输入的深度学习套件 [309] 本平台基于英伟达的Jetson TX1视觉计算的全功能开发板,配合本公司研发的HDMI输入图像采集板:Jetson ...