結果

問題 No.2748 Strange Clock
ユーザー ponjuiceponjuice
提出日時 2024-04-19 14:20:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,010 ms / 2,000 ms
コード長 1,983 bytes
コンパイル時間 4,541 ms
コンパイル使用メモリ 202,908 KB
実行使用メモリ 78,028 KB
最終ジャッジ日時 2024-04-19 14:20:42
合計ジャッジ時間 11,249 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 3 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 3 ms
6,940 KB
testcase_17 AC 3 ms
6,944 KB
testcase_18 AC 5 ms
6,940 KB
testcase_19 AC 5 ms
6,944 KB
testcase_20 AC 12 ms
6,940 KB
testcase_21 AC 12 ms
6,940 KB
testcase_22 AC 35 ms
6,944 KB
testcase_23 AC 34 ms
6,940 KB
testcase_24 AC 104 ms
11,536 KB
testcase_25 AC 111 ms
11,512 KB
testcase_26 AC 303 ms
28,168 KB
testcase_27 AC 298 ms
28,112 KB
testcase_28 AC 978 ms
77,908 KB
testcase_29 AC 990 ms
78,028 KB
testcase_30 AC 101 ms
11,540 KB
testcase_31 AC 101 ms
11,544 KB
testcase_32 AC 103 ms
11,408 KB
testcase_33 AC 297 ms
28,084 KB
testcase_34 AC 1,004 ms
77,924 KB
testcase_35 AC 1,010 ms
77,972 KB
testcase_36 AC 984 ms
77,908 KB
testcase_37 AC 1 ms
6,940 KB
testcase_38 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

ll ll_pow(ll a, ll n) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res *= a;
        a = a * a;
        n >>= 1;
    }
    return res;
}

array<int, 15> itoar(ll i) {
    array<int, 15>res;
    for(int j = 0; j < 15; j++) {
        res[14 - j] = i % 3;
        i /= 3;
    }
    return res;
}

ll artoi(array<int, 15> a, ll e) {
    ll res = 0;
    for(int j = 0; j < 15; j++) {
        res *= e;
        res += a[j];
    }
    return res;
}

ll extgcd(ll a, ll b, ll &p, ll &q) {  
    if (b == 0) { 
        p = 1;
        q = 0;
        return a;
    }  
    ll d = extgcd(b, a % b, q, p);  
    q -= a / b * p;  
    return d;  
}

ll crt(ll a, ll mod1, ll b, ll mod2, ll p, ll q) {
    ll mod = (mod1 * mod2);
    ll res = (a + mod1 * ((b - a) * p % mod2)) % mod;
    if(res < 0) res += mod;
    return res;
}

int main(){
    ll n, m;
    cin >> n >> m;

    const ll pow3 = ll_pow(3, n);
    const ll pow4 = ll_pow(4, n);
    const ll pow6 = ll_pow(6, n);
    const ll pow12 = ll_pow(12, n);
    ll p, q;
    extgcd(pow3, pow4, p, q);

    vector<pair<ll,ll>> res;
    res.reserve(pow3 / 3);

    for(int i = 0; i < pow3; i += 3) {
        // i
        array<int, 15> ret = itoar(i);
        ll i3 = i;
        ll i4 = artoi(ret, 4);
        ll i6 = artoi(ret, 6);

        ll t = crt(i3, pow3, i4, pow4, p, q);

        i6 = ((i6 - t) % pow6 + pow6) % pow6;

        res.emplace_back(i6, t);
    }

    sort(res.begin(), res.end());
    ll ans = 0;

    ll nw = -1, lt = -1;
    for(int i = 0; i < (int)res.size(); i++) {
        if(nw != res[i].first){
            nw = res[i].first;
            lt = res[i].second + pow12;
        }

        if(i + 1 == res.size() || res[i].first != res[i + 1].first) {
            ans += (lt - (res[i].second + 2) > m) ? 1 : 0;
        }else{
            ans += (res[i+1].second - (res[i].second + 2) > m) ? 1 : 0;
        }
    }

    cout << ans << endl;
}
0