結果

問題 No.206 数の積集合を求めるクエリ
ユーザー Ryuhei MoriRyuhei Mori
提出日時 2017-03-13 06:28:39
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 20 ms / 7,000 ms
コード長 4,019 bytes
コンパイル時間 2,255 ms
コンパイル使用メモリ 36,508 KB
実行使用メモリ 5,096 KB
最終ジャッジ日時 2023-09-12 12:12:38
合計ジャッジ時間 3,676 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
5,096 KB
testcase_01 AC 9 ms
4,976 KB
testcase_02 AC 10 ms
4,976 KB
testcase_03 AC 9 ms
4,956 KB
testcase_04 AC 10 ms
4,956 KB
testcase_05 AC 9 ms
4,956 KB
testcase_06 AC 10 ms
4,976 KB
testcase_07 AC 10 ms
5,012 KB
testcase_08 AC 10 ms
4,956 KB
testcase_09 AC 10 ms
5,016 KB
testcase_10 AC 10 ms
5,040 KB
testcase_11 AC 10 ms
4,920 KB
testcase_12 AC 10 ms
4,960 KB
testcase_13 AC 10 ms
4,964 KB
testcase_14 AC 10 ms
5,092 KB
testcase_15 AC 10 ms
4,980 KB
testcase_16 AC 10 ms
4,972 KB
testcase_17 AC 15 ms
4,976 KB
testcase_18 AC 13 ms
4,952 KB
testcase_19 AC 15 ms
5,044 KB
testcase_20 AC 13 ms
4,976 KB
testcase_21 AC 13 ms
4,992 KB
testcase_22 AC 13 ms
4,984 KB
testcase_23 AC 15 ms
5,000 KB
testcase_24 AC 20 ms
4,996 KB
testcase_25 AC 18 ms
5,016 KB
testcase_26 AC 16 ms
5,020 KB
testcase_27 AC 14 ms
4,964 KB
testcase_28 AC 17 ms
4,980 KB
testcase_29 AC 17 ms
4,976 KB
testcase_30 AC 16 ms
5,060 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘read_uint’ 内:
main.c:17:20: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   17 |   while(c<'0') c = getchar_unlocked();
      |                    ^~~~~~~~~~~~~~~~

ソースコード

diff #

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

#define PI2 6.28318530717958647692

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

typedef double complex cmplx;

cmplx C[1<<17];
cmplx w[1<<16];

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

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

  return x;
}


void write_int(int x){
  int i;
  static char buf[12];
  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);
}

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, q;
  int i, j;
  const double arg = -PI2/m;
  const int B = 100009;

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

  l = read_uint();
  mm = read_uint();
  read_uint();


  for(i=0;i<l;i++){
    int a;
    a = read_uint();
    if(a&1) C[a/2] += B;
    else    C[a/2] += 1;
  }
  for(i=0;i<mm;i++){
    int a;
    a = read_uint();
    if(a&1) C[((1<<18)-a)/2] += B*I;
    else    C[((1<<18)-a)/2] += I;
  }

  q = read_uint();

  convolver(k, C, w);

  int carry=0;
  for(i=0, j=0;4*i<q+2;i++){
    unsigned long long int tmp = creal(C[i])+carry+0.480;
    if(i&&j<q){
      write_int(tmp % B);
      j++;
    }
    tmp /= B;
    if(i&&j<q){ write_int(tmp%B);
      j++;
    }
    carry = tmp/B;
    tmp = cimag(C[i])+carry+0.480;
    if(j<q) write_int(tmp%B);
    j++;
    tmp /= B;
    if(j<q) write_int(tmp%B);
    j++;
    carry = tmp/B;
  }

  return 0;
}
0