結果

問題 No.613 Solitude by the window
ユーザー yosupotyosupot
提出日時 2017-12-13 04:18:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,752 ms / 2,000 ms
コード長 2,210 bytes
コンパイル時間 1,514 ms
コンパイル使用メモリ 132,520 KB
実行使用メモリ 393,776 KB
最終ジャッジ日時 2023-08-21 00:41:21
合計ジャッジ時間 11,531 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 108 ms
393,552 KB
testcase_02 AC 1,306 ms
393,480 KB
testcase_03 AC 101 ms
393,756 KB
testcase_04 AC 228 ms
393,708 KB
testcase_05 AC 98 ms
393,736 KB
testcase_06 AC 97 ms
393,736 KB
testcase_07 AC 135 ms
393,608 KB
testcase_08 AC 132 ms
393,672 KB
testcase_09 AC 367 ms
393,656 KB
testcase_10 AC 118 ms
393,648 KB
testcase_11 AC 1,447 ms
393,480 KB
testcase_12 AC 1,752 ms
393,660 KB
testcase_13 AC 1,236 ms
393,692 KB
testcase_14 AC 100 ms
393,608 KB
testcase_15 AC 101 ms
393,748 KB
testcase_16 AC 103 ms
393,692 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 98 ms
393,696 KB
testcase_20 AC 99 ms
393,776 KB
testcase_21 AC 413 ms
393,660 KB
testcase_22 AC 378 ms
393,592 KB
testcase_23 AC 386 ms
393,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("O3")

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <numeric>
#include <random>
#include <vector>
#include <array>
#include <bitset>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n==0) ? 1 : 10*TEN(n-1); }
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;

struct rng {
    struct A {
        int n;
        const bool operator!=(A r) { return n != r.n; }
        A& operator++() { n++; return *this; }
        int operator*() { return n; }
    };
    int l, r;
    rng(int r) : l(0), r(max(0, r)) {}
    rng(int l, int r) : l(l), r(max(l, r)) {}
    A begin() { return A{l}; }
    A end() { return A{r}; }
};

const int MD = TEN(9) + TEN(8) + 10;

uint md; ull imd;
inline ull nx(ull x) {
    ull y = ull(x) * (x) - 2;
    ull d = (__int128(y) * imd) >> 64;
    return y - d * md;
}

ull naive(ll n) {
    ull s = 4;
    for (int i = 0; i < n; i++) {
        s = nx(s);
    }
    return s;
}

ull big(ull n) {
    ull s = 4;
    for (int i = 0; i < 100; i++) s = nx(s);
    ull t = s;
    n -= 100;
    uint c = 0;
    uint d = 0;
    static const uint B = TEN(8) / 2;
    V<ull> buf(B);
    while (c < n) {
        buf[d] = s;
        s = nx(s);
        c++;
        d++;
        if (d == B) d = 0;
        if (__builtin_expect(s == t, 0)) {
            n %= c;
            if (c - n < B) {
                return buf[(d + B - (c - n)) % B];
            }
            c = 0;
            break;
        }
    }
    while (c < n) {
        s = nx(s);
        c++;
    }
    return s;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(20);
    ll n;
    cin >> n >> md;
    if (md == 2) {
        cout << 0 << endl;
        return 0;
    }
    imd = ((__int128(1)<<64) + md-1) / md;
    ull ans;
    if (n < 1000) ans = naive(n);
    else ans = big(n);
    ans = (ans + ull(10) * md - 2) % md;
    cout << ans << endl;
    return 0;
}
0