hdu 5885 FFT

XM Reserves

Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 99    Accepted Submission(s): 45

Problem Description

As an eligible Ingress Resistance Agent you should know your power source, the Exotic Matter.
We call it XM, which is the driving force behind all of our actions in Ingress.
XM allows us to construct items through hacking portals, to attack enemy portals, make links and create fields.

We try to collect XM from the ground. XM concentration come from location based services, meaning that areas with a lot of foot traffic have higher amounts versus places that don‘t.
You can collect XM by moving through those areas.
The XM will be automatically harvested by your Scanner when it is within your interaction circle/range.

Alice decides to select a location such that she can collect XM as much as possible.
To simplify the problem, we consider the city as a grid map with size `n*m‘ numbered from (0,0) to (n−1,m−1).
The XM concentration inside the block (i,j) is p(i,j).
The radius of your interaction circle is r.
We can assume that XM of the block (i,j) are located in the centre of this block.
The distance between two blocks is the Euclidean distance between their centres.

Alice stands in the centre of one block and collects the XM.
For each block with the distance d smaller than r to Alice, and whose XM concertation is p(i,j), Alice‘s scanner can collects p(i,j)/(1+d) XM from it.

Help Alice to determine the maximum XM which she can collect once he stands in the centre of one block.

Input

There are multiple cases.
For each case, the first line consists two integers n,m (1≤n,m≤500) and one float-point number r (0≤r≤300).
Each of the following n line consists m non-negative float-point numbers corresponding to the XM concentrations inside each blocks.

Output

For each case, output the maximum XM which Alice can collect in one line.
Your answers should be rounded to three decimal places.

Sample Input

3 3 1

1 3 6

7 9 4

2 8 1

3 3 2

1 3 6

7 9 4

2 8 1

5 5 1.

5
4 3 2 9

1
3 4 3 2

8
9 4 3 2

1
2 3 0 1

2
6 3 4 3 1

Sample Output

9.000

24.142

17.956

/*
hdu 5885 FFT卷积

problem:
给出一个n×m的格子,距离它小于等于r的那些格子都会贡献p(i,j)/(d+1), d为两个格子的距离. p[i][j]为格子的价值
选择一个格子, 使得贡献和最大.

solve:
参考:https://async.icpc-camp.org/d/558-2016-icpc
因为小于r的格子都会给当前格子贡献, 所以可以考虑先计算出所有格子对<r的其它格子的贡献.那么最后一次遍历就能找出
出最大值.那么会处理出来一个 (n+r+r,m+r+r)的矩阵.
然后就是如何计算, 格子(i,j)按照(x,y)移动后会到格子(i+x,j+y).坐标相加
如果把所有方向处理成一个一维矩阵,那么当前点与它们相乘后的位置就是当前点能到达的目标位置(卷积,坐标相加)
都转换成一维.
0*M+0 0*M+1 0*M+2
0*M+0 0*M+1 0*M+2 1*M+0 ...   (方向)
---->
0*M+0 0*M+1 0*M+2 1*M+0
      0*M+1 0*M+2 1*M+0
求卷积后坐标之间是存在一定关系的.类似于A,B集合中有多少a+b=c那种题的思想.  (新技能get)
(看代码想了很久才懂 TAT.. )
用a[i*M + j]保存p(i,j), b[i*M+j]保存 1/d.
c = a*b, 那么第i*M + j就是i,j的贡献和.

本来最开始用的m但是发现有问题,因为一行处理后有m+2*r个点,换成M就好了....
hhh-2016-09-21 21:10:30
*/
#pragma comment(linker,"/STACK:124000000,124000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <math.h>
#define lson  i<<1
#define rson  i<<1|1
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define key_val ch[ch[root][1]][0]
using namespace std;
const int maxn = 1e6 + 1000;
const int inf = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const double eps = 1e-7;
template<class T> void read(T&num)
{
    char CH;
    bool F=false;
    for(CH=getchar(); CH<‘0‘||CH>‘9‘; F= CH==‘-‘,CH=getchar());
    for(num=0; CH>=‘0‘&&CH<=‘9‘; num=num*10+CH-‘0‘,CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p)
{
    if(!p)
    {
        puts("0");
        return;
    }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + ‘0‘);
    putchar(‘\n‘);
}

const double PI = acos(-1.0);

struct Complex
{
    double x,y;
    Complex(double _x = 0.0,double _y = 0.0)
    {
        x = _x;
        y = _y;
    }
    Complex operator-(const Complex &b)const
    {
        return Complex(x-b.x,y-b.y);
    }
    Complex operator+(const Complex &b)const
    {
        return Complex(x+b.x,y+b.y);
    }
    Complex operator*(const Complex &b)const
    {
        return Complex(x*b.x-y*b.y,x*b.y+y*b.x);
    }
};

void change(Complex y[],int len)
{
    int i,j,k;
    for(i = 1,j = len/2; i < len-1; i++)
    {
        if(i < j) swap(y[i],y[j]);
        k = len/2;
        while(j >= k)
        {
            j-=k;
            k/=2;
        }
        if(j < k) j+=k;
    }
}

void fft(Complex y[],int len,int on)
{
    change(y,len);
    for(int h = 2; h <= len; h <<= 1)
    {
        Complex wn(cos(-on*2*PI/h),sin(-on*2*PI/h));
        for(int j = 0; j < len; j+=h)
        {
            Complex w(1,0);
            for(int k = j; k < j+h/2; k++)
            {
                Complex u = y[k];
                Complex t = w*y[k+h/2];
                y[k] = u+ t;
                y[k+h/2] = u-t;
                w = w*wn;
            }
        }
    }
    if(on == -1)
    {
        for(int i = 0; i < len; i++)
            y[i].x /= len;
    }
}

double dis(int a,int b)
{
    return sqrt(a*a + b*b);
}

Complex a[2*maxn];
Complex b[2*maxn];

int main()
{
    int k;
    int n,m,limit;
    double x,r;
    while(scanf("%d%d%lf",&n,&m,&r) != EOF)
    {
        limit =0, k = 1;
        while(r >= limit)
        {
            limit ++ ;
        }
        limit -= 1;
        int M = m +limit + limit;
        while(k < (n+r+r)*(m+r+r))
           k <<= 1;

        for(int i = 0;i < k;i++)
        {
            a[i] = b[i] = 0;
        }
        for(int i = 0;i < n;i++)
        {
            for(int j = 0;j < m;j++)
            {
                read(x);
                a[i*M + j] = x;
            }
        }
        fft(a,k,1);
        for(int i = -limit;i <= limit;i++)
        {
            for(int j = -limit;j <= limit;j++)
            {
                double len = dis(i,j);
                if( r > len)
                b[(i+limit) * M + j + limit] = 1.0/(len+1);

            }
        }

        fft(b,k,1);
        for(int i = 0;i < k;i++)
            a[i] = a[i] * b[i];
        fft(a,k,-1);
        double ans = 0;
        for(int i = 0;i < n;i++)
        {
            for(int j = 0;j < m;j++)
            {
                ans = max(ans,a[(i + limit) * M + j + limit].x);
            }
        }
        printf("%.3f\n",ans);
    }
    return 0;
}

  

时间: 2024-10-03 13:45:07

hdu 5885 FFT的相关文章

A * B Problem Plus HDU - 1402 (FFT)

A * B Problem Plus HDU - 1402 第一道FFT... 1 #include <iostream> 2 #include <complex> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <algorithm> 7 using namespace std; 8 9 const int maxn = 200010; 1

hdu 4609 FFT

题意:给出一堆数,问从这些数中取3个能组成三角形的概率? sol:其实就是问从这些数里取3个组成三角形有多少种取法 脑洞大开的解法:用FFT 设一开始的数是1 3 3 4 作一个向量x,其中x[i]=边长为i的边的个数 那么就有x=[0 1 0 2 1 0 0 0 0] 令y=x,对x和y作DFT,得到dx和dy.令dn=dx*dy,再对dn作IDFT得到n 那么就得到n=[0 0 1 0 4 2 4 4 1 0 ] 其中n[i]=在x和y中各选一条边,使得两条边之和为i有几种方案 这时得到的n

HDU 1402 fft 模板题

题目就是求一个大数的乘法 这里数字的位数有50000的长度,按平时的乘法方式计算,每一位相乘是要n^2的复杂度的,这肯定不行 我们可以将每一位分解后作为系数,如153 = 1*x^2 + 5*x^1 + 3*x^0 (在这里x可以理解成10) 那么两个数字相乘就相当于系数相乘后得到新的系数组合 如153 * 4987 = <3,5,1> * <7,8,9,4> 这相当于卷积的计算,最快的方式就是fft,nlgn的复杂度就能求解,求解得到后再把每一位大于10往前赋值就行了 1 #in

hdu 4609 3-idiots 【FFT快(gui)速傅立叶变换】

FFT实现起来挺复杂的,开始用vector,结果发现空间超了,换成数组还是超,删掉了几个后又超时了 sin cos 函数调用次数太多了,改成乘法,还是超时 最后把FFT里的除法运算和模运算优化了一下,终于过了,排的老后面 坑,3843MS,一看时间最少的只有671MS,我都怀疑这是不是同一个算法..为毛差距这么大 #pragma comment(linker, "/STACK:102400000,102400000") #include<iostream> #include

fft模板 HDU 1402

1 // fft模板 HDU 1402 2 3 #include <iostream> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <algorithm> 7 #include <vector> 8 #include <math.h> 9 #include <memory.h> 10 #include <bits/stdc++.h> 11 using

Hdu 1402 (FFT)

题目链接 A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12490    Accepted Submission(s): 2206 Problem Description Calculate A * B. Input Each line will contain two integers A and

HDU 5763 Another Meaning(FFT)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5763 [题目大意] 给出两个串S和T,可以将S串中出现的T替换为*,问S串有几种表达方式. [题解] 我们定义数组f为S串中T出现的最后一个字母所在的位置,那么ans[i]=ans[i-1]+f[i-1]?ans[i-lenT]:0,一遍递推即可,所以关键就在于求出f数组了,f数组可以用kmp求,由于最近练FFT,用FFT求距离卷积匹配为0的位置,就是f数组了. [代码] #include <c

HDU 5730 Shell Necklace(CDQ分治+FFT)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5730 [题目大意] 给出一个数组w,表示不同长度的字段的权值,比如w[3]=5表示如果字段长度为3,则其权值为5,现在有长度为n的字段,求通过不同拆分得到的字段权值乘积和. [题解] 记DP[i]表示长度为i时候的答案,DP[i]=sum_{j=0}^{i-1}DP[j]w[i-j],发现是一个卷积的式子,因此运算过程可以用FFT优化,但是由于在计算过程中DP[j]是未知值,顺次计算复杂度是O(

HDU 4609 3-idiots fft

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4609 题目描述:给出n个数, 问其中选出三个能组成三角形的概率 解题思路:给出kuangbin巨巨的解题思路, 实在是没有比这儿更好的题解, 所以我就不误导别人和自己了 http://www.cnblogs.com/kuangbin/archive/2013/07/24/3210565.html 题目代码: #include <iostream> #include <cstdio> #