Submission #1139205
Source Code Expand
#include <stdio.h>
#include <complex.h>
#define PI2 6.28318530717958647692
const int k = 18;
const int m = (1 << 18);
typedef double complex cmplx;
cmplx C[1<<18];
cmplx w[1<<17];
const int mod5[200] = {
0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,
0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,
0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,
0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,
0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4
};
const int div5[200] = {
0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8,9,9,9,9,9,10,10,10,10,10,
11,11,11,11,11,12,12,12,12,12,13,13,13,13,13,14,14,14,14,14,15,15,15,15,15,16,16,16,16,16,17,17,17,17,17,18,18,18,18,18,
19,19,19,19,19,20,20,20,20,20,21,21,21,21,21,22,22,22,22,22,23,23,23,23,23,24,24,24,24,24,25,25,25,25,25,26,26,26,26,26,
27,27,27,27,27,28,28,28,28,28,29,29,29,29,29,30,30,30,30,30,31,31,31,31,31,32,32,32,32,32,33,33,33,33,33,34,34,34,34,34,
35,35,35,35,35,36,36,36,36,36,37,37,37,37,37,38,38,38,38,38,39,39,39,39,39
};
void read_int(int *x){
char c=0;
while(c<'0'||c>'9') c = getchar_unlocked();
*x = c-'0';
c = getchar_unlocked();
while(c>='0'&&c<='9') {
*x *= 10;
*x += c-'0';
c = getchar_unlocked();
}
}
void write_int(int x){
int i;
static char buf[11];
if(x==0){
putchar_unlocked('0');
putchar_unlocked('\n');
return;
}
for(i=10; x; i--){
if(x >= 400){
buf[i] = '0' + x % 10;
x /= 10;
}
else {
buf[i] = '0' + (mod5[x>>1]<<1) + (x&1);
x = div5[x>>1];
}
}
for(i++; i<11; i++)
putchar_unlocked(buf[i]);
putchar_unlocked('\n');
}
unsigned minus(unsigned x) {
int y = 1 << (31-__builtin_clz(x));
return x^(y-1);
}
void fft(cmplx *A, int k){
int m = 1 << k;
int u = 1;
int v = m/2;
for(int i=k;i>0;i--){
for(int jh=0;jh<u;jh++){
cmplx wj = w[jh];
for(int j = jh << i, je = j|v;j<je; j++){
cmplx Ajv = wj * A[j|v];
A[j|v] = A[j] - Ajv;
A[j] += Ajv;
}
}
u <<= 1;
v >>= 1;
}
}
void ifft(cmplx *A, int k){
int m = 1 << k;
int u = m/2;
int v = 1;
for(int i=1;i<=k;i++){
for(int jh=0;jh<u;jh++){
cmplx wj = conj(w[jh]);
for(int j = jh << i, je = j|v;j<je; j++){
cmplx Ajv = A[j] - A[j|v];
A[j] += A[j|v];
A[j|v] = wj * Ajv;
}
}
u >>= 1;
v <<= 1;
}
}
void genw(int i, int b, cmplx z){
if(b == 0){
w[i] = z;
}
else {
genw(i, b>>1, z);
genw(i|b, b>>1, z*w[b]);
}
}
int main(){
int n;
const double arg = -PI2/m;
for(int i=1, j=m/4; j; i<<=1, j>>=1){
w[i] = cexp(I * (arg * j));
}
genw(0, m/4, 1);
read_int(&n);
for(int i=1;i<=n;i++){
int a, b;
read_int(&a);
read_int(&b);
C[i] = a + b*I;
}
// k = 33 - __builtin_clz(n);
// m = 1 << k;
fft(C, k);
C[0] = 4 * creal(C[0]) * cimag(C[0]) * I;
C[1] = 4 * creal(C[1]) * cimag(C[1]) * I;
for(int i = 2; i < m; i+=2){
int j = minus(i);
C[i] = (C[i] + conj(C[j]))*(C[i] - conj(C[j]));
C[j] = -conj(C[i]);
}
for(int i = 0; i < m; i+=2){
C[i/2] = C[i]+C[i^1] + (C[i]-C[i^1])*w[i/2]*I;
}
ifft(C, k-1);
for(int i=1;i<=n;i++){
write_int(-creal(C[i])/(4*m)+0.5);
write_int(cimag(C[i])/(4*m)+0.5);
}
}
Submission Info
Submission Time |
|
Task |
C - 高速フーリエ変換 |
User |
ryuhei |
Language |
C (Clang 3.8.0) |
Score |
100 |
Code Size |
3624 Byte |
Status |
AC |
Exec Time |
28 ms |
Memory |
8192 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
100 / 100 |
Status |
|
|
Set Name |
Test Cases |
Sample |
00_sample_01 |
All |
00_sample_01, 01_00_01, 01_01_19, 01_02_31, 01_03_22, 01_04_31, 01_05_40, 01_06_15, 01_07_39, 01_08_28, 01_09_30, 01_10_23, 01_11_33, 01_12_11, 01_13_28, 01_14_41, 01_15_26, 01_16_49, 01_17_34, 01_18_02, 01_19_33, 01_20_29, 02_00_51254, 02_01_82431, 02_02_17056, 02_03_34866, 02_04_6779, 02_05_65534, 02_06_65535, 02_07_65536, 02_08_65537, 02_09_65538, 02_10_100000 |
Case Name |
Status |
Exec Time |
Memory |
00_sample_01 |
AC |
18 ms |
6272 KB |
01_00_01 |
AC |
18 ms |
6272 KB |
01_01_19 |
AC |
18 ms |
6272 KB |
01_02_31 |
AC |
18 ms |
6272 KB |
01_03_22 |
AC |
18 ms |
6272 KB |
01_04_31 |
AC |
18 ms |
6272 KB |
01_05_40 |
AC |
18 ms |
6272 KB |
01_06_15 |
AC |
18 ms |
6272 KB |
01_07_39 |
AC |
18 ms |
6272 KB |
01_08_28 |
AC |
18 ms |
6400 KB |
01_09_30 |
AC |
18 ms |
6272 KB |
01_10_23 |
AC |
18 ms |
6272 KB |
01_11_33 |
AC |
18 ms |
6272 KB |
01_12_11 |
AC |
18 ms |
6272 KB |
01_13_28 |
AC |
18 ms |
6272 KB |
01_14_41 |
AC |
18 ms |
6272 KB |
01_15_26 |
AC |
18 ms |
6272 KB |
01_16_49 |
AC |
18 ms |
6272 KB |
01_17_34 |
AC |
18 ms |
6272 KB |
01_18_02 |
AC |
18 ms |
6272 KB |
01_19_33 |
AC |
17 ms |
6272 KB |
01_20_29 |
AC |
18 ms |
6272 KB |
02_00_51254 |
AC |
23 ms |
7168 KB |
02_01_82431 |
AC |
27 ms |
7808 KB |
02_02_17056 |
AC |
19 ms |
6656 KB |
02_03_34866 |
AC |
21 ms |
6912 KB |
02_04_6779 |
AC |
18 ms |
6400 KB |
02_05_65534 |
AC |
24 ms |
7552 KB |
02_06_65535 |
AC |
25 ms |
7552 KB |
02_07_65536 |
AC |
25 ms |
7552 KB |
02_08_65537 |
AC |
25 ms |
7552 KB |
02_09_65538 |
AC |
25 ms |
7552 KB |
02_10_100000 |
AC |
28 ms |
8192 KB |