Submission #1137844


Source Code Expand

#include <stdio.h>
#include <complex.h>

#define PI2 6.28318530717958647692

const int k = 18;
const int m = (1 << 18);

complex double C[1<<18];
complex double w[1<<17];

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){
  static char buf[12];
  int i;
  if(x==0){
    puts("0");
    return;
  }

  for(i=10; x; i--){
    buf[i] = '0' + x % 10;
    x /= 10;
  }
  puts(buf+1+i);
}

unsigned minus(unsigned x) {
  int y = 1 << (31-__builtin_clz(x));
  return (~x&(y-1))^y;
}


void fft(complex double *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++){
      complex double wj = w[jh];
      for(int j = jh << i, je = j|v;j<je; j++){
        complex double Ajv = wj * A[j|v];
        A[j|v] = A[j] - Ajv;
        A[j] += Ajv;
      }
    }
    u <<= 1;
    v >>= 1;
  }
}

void ifft(complex double *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++){
      complex double wj = conj(w[jh]);
      for(int j = jh << i, je = j|v;j<je; j++){
        complex double 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, complex double 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);

  scanf("%d", &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 2423 Byte
Status AC
Exec Time 29 ms
Memory 8192 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 1
AC × 33
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 24 ms 7040 KB
01_00_01 AC 17 ms 6272 KB
01_01_19 AC 17 ms 6272 KB
01_02_31 AC 17 ms 6272 KB
01_03_22 AC 17 ms 6272 KB
01_04_31 AC 17 ms 6272 KB
01_05_40 AC 17 ms 6272 KB
01_06_15 AC 17 ms 6272 KB
01_07_39 AC 17 ms 6272 KB
01_08_28 AC 17 ms 6272 KB
01_09_30 AC 17 ms 6272 KB
01_10_23 AC 17 ms 6272 KB
01_11_33 AC 17 ms 6272 KB
01_12_11 AC 17 ms 6272 KB
01_13_28 AC 17 ms 6272 KB
01_14_41 AC 17 ms 6272 KB
01_15_26 AC 17 ms 6272 KB
01_16_49 AC 17 ms 6272 KB
01_17_34 AC 17 ms 6272 KB
01_18_02 AC 17 ms 6272 KB
01_19_33 AC 17 ms 6272 KB
01_20_29 AC 17 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 25 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 29 ms 8192 KB