結果

問題 No.206 数の積集合を求めるクエリ
ユーザー ei1333333ei1333333
提出日時 2017-11-17 11:35:34
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 224 ms / 7,000 ms
コード長 2,483 bytes
コンパイル時間 3,218 ms
コンパイル使用メモリ 202,708 KB
実行使用メモリ 6,236 KB
最終ジャッジ日時 2023-08-16 19:06:01
合計ジャッジ時間 6,585 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 5 ms
4,376 KB
testcase_07 AC 5 ms
4,380 KB
testcase_08 AC 6 ms
4,380 KB
testcase_09 AC 6 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 6 ms
4,376 KB
testcase_13 AC 6 ms
4,376 KB
testcase_14 AC 5 ms
4,380 KB
testcase_15 AC 6 ms
4,380 KB
testcase_16 AC 6 ms
4,376 KB
testcase_17 AC 211 ms
5,888 KB
testcase_18 AC 201 ms
6,048 KB
testcase_19 AC 213 ms
6,032 KB
testcase_20 AC 204 ms
5,980 KB
testcase_21 AC 210 ms
5,960 KB
testcase_22 AC 206 ms
6,236 KB
testcase_23 AC 211 ms
6,024 KB
testcase_24 AC 222 ms
5,976 KB
testcase_25 AC 224 ms
6,052 KB
testcase_26 AC 213 ms
5,976 KB
testcase_27 AC 207 ms
6,048 KB
testcase_28 AC 217 ms
6,040 KB
testcase_29 AC 216 ms
5,960 KB
testcase_30 AC 212 ms
5,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

struct NumberTheoreticTransform
{
  int mod;
  int primitiveroot;

  NumberTheoreticTransform(int mod, int root) : mod(mod), primitiveroot(root) {}

  inline int mod_pow(int x, int n)
  {
    int ret = 1;
    while(n > 0) {
      if(n & 1) ret = mul(ret, x);
      x = mul(x, x);
      n >>= 1;
    }
    return ret;
  }

  inline int inverse(int x)
  {
    return (mod_pow(x, mod - 2));
  }

  inline int add(int x, int y)
  {
    x += y;
    if(x >= mod) x -= mod;
    return (x);
  }

  inline int mul(int a, int b)
  {
    unsigned long long x = (long long) a * b;
    unsigned xh = (unsigned) (x >> 32), xl = (unsigned) x, d, m;
    asm("divl %4; \n\t" : "=a" (d), "=d" (m) : "d" (xh), "a" (xl), "r" (mod));
    return (m);
  }

  void DiscreteFourierTransform(vector< int > &F, bool rev)
  {
    const int N = (int) F.size();
    for(int i = 0, j = 1; j + 1 < N; j++) {
      for(int k = N >> 1; k > (i ^= k); k >>= 1);
      if(i > j) swap(F[i], F[j]);
    }
    int w, wn, s, t;
    for(int i = 1; i < N; i <<= 1) {
      w = mod_pow(primitiveroot, (mod - 1) / (i * 2));
      if(rev) w = inverse(w);
      for(int k = 0; k < i; k++) {
        wn = mod_pow(w, k);
        for(int j = 0; j < N; j += i * 2) {
          s = F[j + k], t = mul(F[j + k + i], wn);
          F[j + k] = add(s, t), F[j + k + i] = add(s, mod - t);
        }
      }
    }
    if(rev) {
      int temp = inverse(N);
      for(int i = 0; i < N; i++) F[i] = mul(F[i], temp);
    }
  }

  vector< int > Multiply(const vector< int > &A, const vector< int > &B)
  {
    int sz = 1;
    while(sz < A.size() + B.size() - 1) sz <<= 1;
    vector< int > F(sz), G(sz);
    for(int i = 0; i < A.size(); i++) F[i] = A[i];
    for(int i = 0; i < B.size(); i++) G[i] = B[i];
    DiscreteFourierTransform(F, false);
    DiscreteFourierTransform(G, false);
    for(int i = 0; i < sz; i++) F[i] = mul(F[i], G[i]);
    DiscreteFourierTransform(F, true);
    F.resize(A.size() + B.size() - 1);
    return (F);
  }
};

int main()
{
  int N, M, P;
  scanf("%d %d %d", &N, &M, &P);
  vector< int > A(P), B(P);
  for(int i = 0; i < N; i++) {
    int x;
    scanf("%d", &x);
    A[x - 1] = 1;
  }
  for(int i = 0; i < M; i++) {
    int x;
    scanf("%d", &x);
    B[x - 1] = 1;
  }
  reverse(begin(B), end(B));
  NumberTheoreticTransform ntt(1012924417, 5);
  auto C = ntt.Multiply(A, B);
  int Q;
  scanf("%d", &Q);
  for(int i = 0; i < Q; i++) printf("%d\n", C[P + i - 1]);
}
0