結果

問題 No.613 Solitude by the window
ユーザー yosupotyosupot
提出日時 2017-12-13 04:02:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,063 bytes
コンパイル時間 1,673 ms
コンパイル使用メモリ 112,144 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-08-21 00:30:01
合計ジャッジ時間 11,693 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 TLE -
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 192 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 53 ms
4,376 KB
testcase_08 AC 37 ms
4,376 KB
testcase_09 AC 409 ms
4,376 KB
testcase_10 AC 23 ms
4,376 KB
testcase_11 AC 1,346 ms
4,380 KB
testcase_12 AC 1,655 ms
4,380 KB
testcase_13 TLE -
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 381 ms
4,380 KB
testcase_22 AC 379 ms
4,380 KB
testcase_23 AC 285 ms
4,376 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;
    ull c = 0;
    while (true) {
        s = nx(s);
        c++;
        if (__builtin_expect(s == t, 0)) {
            n %= c;
            c = 0;
            break;
        }
    }
    while (c < n) {
        s = nx(s);
        c++;
        if (__builtin_expect(s == t, 0)) {
            n %= c;
            c = 0;
        }
    }
    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 + md - 2) % md;
    cout << ans << endl;
    return 0;
}
0