結果

問題 No.723 2つの数の和
ユーザー yuppe19 😺yuppe19 😺
提出日時 2018-08-22 20:33:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 469 ms / 2,000 ms
コード長 2,114 bytes
コンパイル時間 967 ms
コンパイル使用メモリ 92,008 KB
実行使用メモリ 14,316 KB
最終ジャッジ日時 2023-08-26 18:29:04
合計ジャッジ時間 14,135 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 456 ms
14,140 KB
testcase_01 AC 455 ms
14,124 KB
testcase_02 AC 455 ms
14,132 KB
testcase_03 AC 464 ms
14,200 KB
testcase_04 AC 465 ms
14,096 KB
testcase_05 AC 463 ms
14,128 KB
testcase_06 AC 465 ms
14,100 KB
testcase_07 AC 460 ms
14,132 KB
testcase_08 AC 460 ms
14,140 KB
testcase_09 AC 466 ms
14,164 KB
testcase_10 AC 459 ms
14,316 KB
testcase_11 AC 455 ms
14,092 KB
testcase_12 AC 461 ms
14,096 KB
testcase_13 AC 469 ms
14,120 KB
testcase_14 AC 458 ms
14,156 KB
testcase_15 AC 460 ms
14,144 KB
testcase_16 AC 462 ms
14,144 KB
testcase_17 AC 463 ms
14,104 KB
testcase_18 AC 464 ms
14,144 KB
testcase_19 AC 469 ms
14,300 KB
testcase_20 AC 454 ms
14,072 KB
testcase_21 AC 453 ms
14,256 KB
testcase_22 AC 454 ms
14,124 KB
testcase_23 AC 465 ms
14,300 KB
testcase_24 AC 457 ms
14,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cmath>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
using i64 = long long;

constexpr i64 O = 3,
              M = (5LL << 37) * 211 + 1, // 48 ビット
              R2 = 65087448474821LL;     // (2**63)**2 % M

inline __uint128_t MR(__uint128_t t) {
  __uint128_t y = ((t * (M-2) & 0x7fffffffffffffffLL) * M + t) >> 63;
  if(y >= M) { y -= M; }
  return y;
}

inline i64 mod_mul(const __uint128_t &x, const __uint128_t &y, const i64 &m) {
  return i64(MR(MR(MR(x * R2) * MR(y * R2))));
}

map<tuple<i64, i64, i64>, i64> cache;

inline i64 mod_pow(i64 a, i64 n, const i64 &m) {
  auto key = make_tuple(a, n, m);
  if(cache.count(key)) { return cache[key]; }
  i64 res = 1;
  for(; n; n>>=1) {
    if(n & 1) { res = mod_mul(res, a, m); }
    a = mod_mul(a, a, m);
  }
  return cache[key] = res;
}

void myfmt(vector<i64> &a, bool inv) {
  int n = int(a.size());
  if(n == 1) { return; }
  int m = n / 2;
  vector<i64> a0(m), a1(m);
  for(int i=0, j=0; i<m; ++i) {
    a0[i] = a[j++];
    a1[i] = a[j++];
  }
  myfmt(a0, inv);
  myfmt(a1, inv);
  i64 z = mod_pow(O, (M-1)/n, M);
  if(inv) { z = mod_pow(z, M-2, M); }
  i64 pz = 1;
  for(int i=0; i<n; ++i) {
    a[i] = a0[i%m] + mod_mul(pz, a1[i%m], M);
    a[i] %= M;
    pz = mod_mul(pz, z, M);
  }
}

void fmt(vector<i64> &a) {
  myfmt(a, false);
}

void ifmt(vector<i64> &a) {
  myfmt(a, true);
  int n = int(a.size());
  i64 inv = mod_pow(n, M-2, M);
  for(int i=0; i<n; ++i) {
    a[i] = mod_mul(a[i], inv, M);
  }
}

vector<i64> convol(vector<i64> a, vector<i64> b) {
  int n = 1;
  while(n < a.size() + b.size()) { n <<= 1; }
  a.resize(n);
  b.resize(n);
  fmt(a);
  fmt(b);
  vector<i64> c(n);
  for(int i=0; i<n; ++i) {
    c[i] = mod_mul(a[i], b[i], M);
  }
  ifmt(c);
  return c;
}

constexpr int N = int(powl(10, 5)) + 10;

int main(void) {
  int n; i64 x; scanf("%d%lld", &n, &x);
  vector<i64> cnt(N);
  for(int i=0; i<n; ++i) {
    int ai; scanf("%d", &ai);
    ++cnt[ai];
  }
  vector<i64> c = convol(cnt, cnt);
  i64 res = 0;
  if(x < c.size()) {
    res = c[x];
  }
  printf("%lld\n", res);
  return 0;
}
0