結果

問題 No.2748 Strange Clock
ユーザー Series_205Series_205
提出日時 2024-04-20 16:34:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 924 ms / 2,000 ms
コード長 3,664 bytes
コンパイル時間 2,287 ms
コンパイル使用メモリ 213,880 KB
実行使用メモリ 144,640 KB
最終ジャッジ日時 2024-04-20 16:34:53
合計ジャッジ時間 9,111 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 4 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 9 ms
5,376 KB
testcase_21 AC 8 ms
5,376 KB
testcase_22 AC 24 ms
8,576 KB
testcase_23 AC 23 ms
8,704 KB
testcase_24 AC 79 ms
19,200 KB
testcase_25 AC 78 ms
19,072 KB
testcase_26 AC 303 ms
61,440 KB
testcase_27 AC 263 ms
61,696 KB
testcase_28 AC 913 ms
144,512 KB
testcase_29 AC 876 ms
144,640 KB
testcase_30 AC 72 ms
19,200 KB
testcase_31 AC 75 ms
19,328 KB
testcase_32 AC 72 ms
19,328 KB
testcase_33 AC 284 ms
61,568 KB
testcase_34 AC 909 ms
144,512 KB
testcase_35 AC 924 ms
144,640 KB
testcase_36 AC 918 ms
144,512 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define FOR(i, l, r) for(ll i = l; i < r; i++)
#define rep(i, n) FOR(i, 0, n)
#define FORR(i, l, r) for(ll i = r - 1; i >= l; i--)
#define ALL(ar) begin(ar), end(ar)

template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
    return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
    return a > b && (a = b, true);
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
    is >> p.first >> p.second;
    return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
    os << p.first << ' ' << p.second;
    return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
    for(T &x : v) is >> x;
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    for(size_t i = 0; i < v.size(); i++) os << (i ? " " : "") << v[i];
    return os;
}
//-------------------------------------------------
// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
long long extGCD(long long a, long long b, long long &x, long long &y) {
    if(b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

ll po(ll x, ll n) {
    ll res = 1;
    rep(i, n) res *= x;
    return res;
}

ll p2[16], p3[16], p4[16], p6[16];

void solve() {
    rep(i, 16) {
        p2[i] = po(2, i);
        p3[i] = po(3, i);
        p4[i] = po(4, i);
        p6[i] = po(6, i);
    }

    ll n, m;
    cin >> n >> m;
    m += 3;
    if(n == 1) {
        if(m <= 12) {
            cout << 1 << endl;
        } else {
            cout << 0 << endl;
        }
        return;
    }
    if(n == 2) {
        if(m <= 144) {
            cout << 3 << endl;
        } else {
            cout << 0 << endl;
        }
        return;
    }
    vector<int> d(n);
    map<pair<ll, ll>, vector<pair<ll, ll>>> mp;
    rep(i, po(3, n - 2)) {
        ll d1 = 0, d2 = 0;
        ll sum3 = 0, sum4 = 0;
        rep(j, n) {
            d1 += (p6[j] - p3[j]) * d[j];
            d2 += (p6[j] - p4[j]) * d[j];
            sum3 += p3[j] * d[j];
            sum4 += p4[j] * d[j];
        }
        d1 %= p3[n];
        d2 %= p2[n];
        mp[{d1, d2}].emplace_back(sum3, sum4);

        d[2]++;
        rep(j, n) if(d[j] == 3) {
            d[j] = 0;
            if(j + 1 < n) d[j + 1]++;
        }
    }

    int ans = 0;
    const ll p12 = po(12, n);
    ll x, y;
    extGCD(p3[n], p4[n], x, y);
    x %= p4[n];
    y %= p3[n];
    while(y > 0) {
        y -= p3[n];
        x += p4[n];
    }
    // cout << x << " " << y << endl;
    for(const auto &[key, vec] : mp) {
        if(vec.size() == 1) {
            if(m <= p12) ans++;
        } else {
            vector<ll> t;
            for(auto [l, r] : vec) {
                ll tmp = l - vec[0].first;
                if(r - l >= vec[0].second - vec[0].first) {
                    tmp += x * ((r - l) - (vec[0].second - vec[0].first)) %
                           p4[n] * p3[n];
                } else {
                    tmp += (x - p4[n]) *
                           ((r - l) - (vec[0].second - vec[0].first)) % p4[n] *
                           p3[n];
                }
                t.push_back(tmp);
            }
            t.push_back(p12);
            sort(ALL(t));
            rep(i, t.size() - 1) if(m <= t[i + 1] - t[i]) ans++;
        }
    }

    cout << ans * 3 << endl;
}

int main() {
    int t = 1;
    // cin >> t;
    while(t--) solve();
}
0