結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 458 ms
14,180 KB
testcase_01 AC 457 ms
14,132 KB
testcase_02 AC 456 ms
14,204 KB
testcase_03 AC 470 ms
14,136 KB
testcase_04 AC 474 ms
14,256 KB
testcase_05 AC 474 ms
14,268 KB
testcase_06 AC 475 ms
14,220 KB
testcase_07 AC 469 ms
14,256 KB
testcase_08 AC 471 ms
14,176 KB
testcase_09 AC 476 ms
14,212 KB
testcase_10 AC 469 ms
14,256 KB
testcase_11 AC 466 ms
14,128 KB
testcase_12 AC 470 ms
14,076 KB
testcase_13 AC 477 ms
14,100 KB
testcase_14 AC 467 ms
14,256 KB
testcase_15 AC 472 ms
14,136 KB
testcase_16 AC 471 ms
14,204 KB
testcase_17 AC 473 ms
14,256 KB
testcase_18 AC 460 ms
14,140 KB
testcase_19 AC 466 ms
14,096 KB
testcase_20 AC 455 ms
14,268 KB
testcase_21 AC 461 ms
14,116 KB
testcase_22 AC 460 ms
14,136 KB
testcase_23 AC 472 ms
14,132 KB
testcase_24 AC 468 ms
14,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

constexpr i64 O = 5,
              M = (1LL << 54) * 49 + 1,  // 60ビット
              R2 = 236393025338712675LL; // (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