XTU 1250 Super Fast Fourier Transform

Super Fast Fourier Transform

 
Accepted : 73   Submit : 396
Time Limit : 2000 MS   Memory Limit : 65536 KB

Super Fast Fourier Transform

Bobo has two sequences of integers {a1,a2,,an} and {b1,b2,,bm}. He would like to find

i=1nj=1m|aibj|.

Note that x denotes the maximum integer does not exceed x, and |x| denotes the absolute value of x.

Input

The input contains at most 30 sets. For each set:

The first line contains 2 integers n,m (1n,m105).

The second line contains n integers a1,a2,,an.

The thrid line contains m integers b1,b2,,bm.

(ai,bi0,a1+a2++an,b1+b2+,bm106)

Output

For each set, an integer denotes the sum.

Sample Input

1 2
1
2 3
2 3
1 2
3 4 5

Sample Output

2
7

解题思路:从“(ai,bi0,a1+a2++an,b1+b2+,bm106)”这个条件可以看出,数列中肯定有重复的数据,然后统计下,最后暴力解决。。。

还有就是在XTU上提交用long long过不去,要用__int64,郁闷了好久。。。

代码如下:

#include 
#include 
#include 
#include 
#define LL __int64
#define maxn 1001001
using namespace std;
int a1[maxn],a2[maxn],b1[maxn],b2[maxn];
int main()
{
    int n,m;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        int a,b,ta=0,tb=0;
        memset(a2,0,sizeof(a2));
        memset(b2,0,sizeof(b2));
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &a);
            if(a2[a] == 0) a1[ta++] = a;
            a2[a]++;///记录重复的个数
        }
        for(int i = 0; i < m; i++)
        {
            scanf("%d", &b);
            if(b2[b] == 0) b1[tb++] = b;
            b2[b]++;
        }
        LL sum=0;
        for(int i = 0; i < ta; i++)
            for(int j = 0; j < tb; j++)
                sum += (LL)(a2[a1[i]] * (LL)b2[b1[j]] * (LL)sqrt(fabs(a1[i] - b1[j])));
        printf("%I64d\n",sum);
    }
    return 0;
}



你可能感兴趣的:(OJ)