結果

問題 No.206 数の積集合を求めるクエリ
ユーザー Ryuhei MoriRyuhei Mori
提出日時 2017-03-05 17:49:59
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 24 ms / 7,000 ms
コード長 4,728 bytes
コンパイル時間 930 ms
コンパイル使用メモリ 36,100 KB
実行使用メモリ 8,192 KB
最終ジャッジ日時 2023-09-05 10:12:24
合計ジャッジ時間 3,116 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,128 KB
testcase_01 AC 17 ms
8,156 KB
testcase_02 AC 16 ms
8,140 KB
testcase_03 AC 16 ms
8,064 KB
testcase_04 AC 17 ms
8,060 KB
testcase_05 AC 16 ms
8,128 KB
testcase_06 AC 17 ms
8,100 KB
testcase_07 AC 17 ms
8,124 KB
testcase_08 AC 17 ms
8,100 KB
testcase_09 AC 16 ms
8,112 KB
testcase_10 AC 17 ms
8,180 KB
testcase_11 AC 17 ms
8,152 KB
testcase_12 AC 16 ms
8,116 KB
testcase_13 AC 17 ms
8,096 KB
testcase_14 AC 17 ms
8,128 KB
testcase_15 AC 16 ms
8,152 KB
testcase_16 AC 17 ms
8,028 KB
testcase_17 AC 21 ms
8,060 KB
testcase_18 AC 20 ms
8,056 KB
testcase_19 AC 21 ms
8,128 KB
testcase_20 AC 19 ms
8,152 KB
testcase_21 AC 20 ms
8,136 KB
testcase_22 AC 20 ms
8,036 KB
testcase_23 AC 21 ms
8,128 KB
testcase_24 AC 24 ms
8,128 KB
testcase_25 AC 23 ms
8,152 KB
testcase_26 AC 22 ms
8,060 KB
testcase_27 AC 20 ms
8,032 KB
testcase_28 AC 23 ms
8,116 KB
testcase_29 AC 23 ms
8,116 KB
testcase_30 AC 21 ms
8,192 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘read_uint’ 内:
main.c:33:20: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   33 |   while(c<'0') c = getchar_unlocked();
      |                    ^~~~~~~~~~~~~~~~
main.c: 関数 ‘write_uint’ 内:
main.c:49:5: 警告: 関数 ‘fputs_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   49 |     fputs_unlocked("0\n", stdout);
      |     ^~~~~~~~~~~~~~

ソースコード

diff #

#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 mod10[200] = {
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9
};

const int div10[200] = {
0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,
5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,
10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,13,13,
14,14,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15,15,15,16,16,16,16,16,16,16,16,16,16,17,17,17,17,17,17,17,17,17,17,
18,18,18,18,18,18,18,18,18,18,19,19,19,19,19,19,19,19,19,19
};



void read_uint(int *x){
  char c=0;
  while(c<'0') c = getchar_unlocked();

  *x = c-'0';
  c = getchar_unlocked();

  while(c>='0') {
    *x *= 10;
    *x += c-'0';
    c = getchar_unlocked();
  }
}

void write_uint(int x){
  int i;
  static char buf[13]="           \n";
  if(x==0){
    fputs_unlocked("0\n", stdout);
    return;
  }

  for(i=10; x; i--){
    if(x >= 200){
      buf[i] = '0' + x % 10;
      x /= 10;
    }
    else {
      buf[i] = '0' + mod10[x];
      x = div10[x];
    }
  }
  fputs_unlocked(buf+1+i, stdout);
}


void fft(int k, cmplx *A, const cmplx *w){
  const int m = 1 << k;
  int u = 1;
  int v = m/4;
  int i, j;
  if(k&1){
    for(j=0; j<m/2; j++){
      cmplx Ajv = A[j|(m/2)];
      A[j|(m/2)] = A[j] - Ajv;
      A[j] += Ajv;
    }
    u <<= 1;
    v >>= 1;
  }
  for(i=k&~1;i>0;i-=2){
    int jh;
    for(jh=0;jh<u;jh++){
      cmplx wj = w[jh<<1];
      cmplx wj2 = w[jh];
      cmplx wj3 = wj2 * wj;
      int je;
      for(j = jh << i, je = j+v;j<je; j++){
        cmplx tmp0 = A[j];
        cmplx tmp1 = wj * A[j+v];
        cmplx tmp2 = wj2 * A[j+2*v];
        cmplx tmp3 = wj3 * A[j+3*v];

        cmplx ttmp0 = tmp0 + tmp2;
        cmplx ttmp2 = tmp0 - tmp2;
        cmplx ttmp1 = tmp1 + tmp3;
        cmplx ttmp3 = -I * (tmp1 - tmp3);

        A[j] = ttmp0 + ttmp1;
        A[j+v] = ttmp0 - ttmp1;
        A[j+2*v] = ttmp2 + ttmp3;
        A[j+3*v] = ttmp2 - ttmp3;
      }
    }
    u <<= 2;
    v >>= 2;
  }
}


void ifft(int k, cmplx *A, const cmplx *w){
  const int m = 1 << k;
  int u = m/4;
  int v = 1;
  int i, j;
  for(i=2;i<=k;i+=2){
    int jh;
    for(jh=0;jh<u;jh++){
      cmplx wj = conj(w[jh<<1]);
      cmplx wj2 = conj(w[jh]);
      cmplx wj3 = wj2 * wj;
      int je;
      for(j = jh << i, je = j+v;j<je; j++){
        cmplx tmp0 = A[j];
        cmplx tmp1 = A[j|v];
        cmplx tmp2 = A[j|(v<<1)];
        cmplx tmp3 = A[j|(v<<1)|v];

        cmplx ttmp0 = tmp0 + tmp1;
        cmplx ttmp1 = tmp0 - tmp1;
        cmplx ttmp2 = tmp2 + tmp3;
        cmplx ttmp3 = I * (tmp2 - tmp3);

        A[j] = ttmp0 + ttmp2;
        A[j|v] = wj * (ttmp1 + ttmp3);
        A[j|(v<<1)] = wj2 * (ttmp0 - ttmp2);
        A[j|(v<<1)|v] = wj3 * (ttmp1 - ttmp3);
      }
    }
    u >>= 2;
    v <<= 2;
  }
  if(k&1){
    for(j = 0;j<m/2; j++){
      cmplx Ajv = A[j|(m/2)];
      A[j|(m/2)] = A[j] - Ajv;
      A[j] += Ajv;
    }
  }
}

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]);
  }
}

void convolver(int k, cmplx *A, const cmplx *w){
  int i;
  const int m = 1 << k;

  fft(k, A, w);
  A[0] = 4 * creal(A[0]) * cimag(A[0]) * I;
  A[1] = 4 * creal(A[1]) * cimag(A[1]) * I;
  for(i = 2; i < m; i+=2){
    int y = 1 << (31-__builtin_clz(i));
    int j = i^(y-1);
    A[i] = (A[i] + conj(A[j]))*(A[i] - conj(A[j]));
    A[j] = -conj(A[i]);
  }

  for(i = 0; i < m; i+=2){
    A[i/2] = (A[i]+A[i^1] - (A[i]-A[i^1])*w[i/2]*I)/(4*m);
  }

  ifft(k-1, A, w);
}


int main(){
  int l, mm, n, q;
  int i, j;
  const double arg = -PI2/m;

  for(i=1, j=m/4; j; i<<=1, j>>=1){
    w[i] = cexp(I * (arg * j));
  }
  genw(0, m/4, 1);

  read_uint(&l);
  read_uint(&mm);
  read_uint(&n);


  for(i=0;i<l;i++){
    int a;
    read_uint(&a);
    C[a] = 1;
  }
  for(i=0;i<mm;i++){
    int a;
    read_uint(&a);
    C[(1<<18)-a] += I;
  }
  read_uint(&q);

  convolver(k, C, w);

  write_uint(cimag(C[0])+0.5);
  for(i=1;2*i<q;i++){
    write_uint(creal(C[i])+0.5);
    write_uint(cimag(C[i])+0.5);
  }
  if(!(q&1)) write_uint(creal(C[q/2])+0.5);

  return 0;
}
0