結果

問題 No.2748 Strange Clock
ユーザー Yu_212Yu_212
提出日時 2024-04-21 15:30:23
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 2,616 bytes
コンパイル時間 3,487 ms
コンパイル使用メモリ 288,216 KB
実行使用メモリ 15,404 KB
最終ジャッジ日時 2024-04-21 15:30:29
合計ジャッジ時間 4,660 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 6 ms
5,376 KB
testcase_19 AC 6 ms
5,376 KB
testcase_20 AC 15 ms
6,648 KB
testcase_21 AC 15 ms
6,652 KB
testcase_22 AC 53 ms
15,404 KB
testcase_23 AC 52 ms
15,400 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 1 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const ll mod = 998244353;
int n;

using ULL = unsigned long long;
struct BarrettReduction {
  ULL MOD;
  __int128 m;

  BarrettReduction(ULL MOD_): MOD(MOD_) {
    m = (__int128(1)<<64) / MOD;
  }

  ULL reduce(ULL x) const {
    ULL q = (x*m)>>64;
    x -= q * MOD;
    return x < MOD? x: x-MOD;
  }
};

ll extgcd(ll a, ll b) {
  ll x0 = 1, x1 = 0, y0 = 0, y1 = 1;
  while (b != 0) {
    ll q = a / b;
    ll r = a % b;
    ll x2 = x0 - q * x1;
    ll y2 = y0 - q * y1;
    a = b;
    b = r;
    x0 = x1;
    x1 = x2;
    y0 = y1;
    y1 = y2;
  }
  return x0;
}

ULL crt(ll r1, ll m1, ll r2, ll dpq1, BarrettReduction br2, BarrettReduction br3) {
  ULL temp = br2.reduce((r2 - r1) * dpq1);
  return br3.reduce(r1 + m1 * temp);
}

ll pw(ll a, ll b) {
  ll x = 1;
  while (b > 0) {
    if ((b & 1) == 1) {
      x = x * a;
    }
    a = a * a;
    b >>= 1;
  }
  return x;
}

int main() {
  cin.tie(0)->sync_with_stdio(false);
  cin >> n;
  ll m;
  cin >> m;
  m += 2;
  if (n == 15) {
    ll ans = 0;
    if (2567836929097728 > m) ans += 1594323;
    if (12839184645488640 > m) ans += 1594323;
    if (15407021574586368 > m) ans += 1594323;
    cout << ans << endl;
    return 0;
  }
  if (n == 14) {
    ll ans = 0;
    if (1283918464548864 > m) ans += 1594323;
    cout << ans << endl;
    return 0;
  }
  if (n == 13) {
    ll ans = 0;
    if (17832200896512 > m) ans += 177147;
    if (89161004482560 > m) ans += 177147;
    if (106993205379072 > m) ans += 177147;
    cout << ans << endl;
    return 0;
  }
  ll p3 = pw(3, n);
  ll p4 = pw(4, n);
  ll p6 = pw(6, n);
  ll p12 = p3 * p4;
  BarrettReduction br4(p4);
  BarrettReduction br6(p6);
  BarrettReduction br12(p12);
  ll dpq1 = extgcd(p3, p4);
  unordered_map<ULL, vector<ULL>> mp;
  for (ll v3 = 0; v3 < p3; v3 += 3) {
    ll v4 = 0;
    ll v6 = 0;
    ll tmp = v3;
    ll pc4 = 4;
    ll pc6 = 6;
    for (int i = 1; i < n; i++) {
      tmp /= 3;
      v4 += tmp % 3 * pc4;
      v6 += tmp % 3 * pc6;
      pc4 *= 4;
      pc6 *= 6;
    }
    ULL v12 = crt(v3, p3, v4, dpq1, br4, br12);
    ULL diff = br6.reduce(v12 - v6 + p6);
    mp[diff].push_back(v12);
  }
  ll ans = 0;
//  map<ULL, ll> freq;
  for (auto &[k, vv]: mp) {
    sort(vv.begin(), vv.end());
    ULL last = vv[vv.size() - 1];
    for (ULL v : vv) {
      if (v + p12 > last + m) {
        ans++;
      }
//      freq[v + p12 - last]++;
      last = v + p12;
    }
  }
//  for (const auto &item: freq) {
//    cout << item.first << ", " << item.second << endl;
//  }
  cout << ans << endl;
}
0