結果

問題 No.1255 ハイレーツ・オブ・ボリビアン
ユーザー finefine
提出日時 2020-10-10 01:52:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,007 ms / 2,000 ms
コード長 3,475 bytes
コンパイル時間 1,824 ms
コンパイル使用メモリ 179,972 KB
実行使用メモリ 5,900 KB
最終ジャッジ日時 2023-09-30 14:46:49
合計ジャッジ時間 6,367 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 5 ms
4,376 KB
testcase_06 AC 5 ms
4,376 KB
testcase_07 AC 5 ms
4,376 KB
testcase_08 AC 462 ms
5,696 KB
testcase_09 AC 490 ms
5,664 KB
testcase_10 AC 497 ms
5,632 KB
testcase_11 AC 484 ms
5,732 KB
testcase_12 AC 487 ms
5,684 KB
testcase_13 AC 42 ms
5,900 KB
testcase_14 AC 1,007 ms
5,788 KB
testcase_15 AC 461 ms
5,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';

vector<int> pi_func(int n) {
    vector<int> res(2 * n);
    for (int i = 0; i < 2 * n; i++) {
        if (i % 2 == 0) res[i] = i / 2;
        else res[i] = n + i / 2;
    }
    return res;
}

struct UnionFind {
    //各要素が属する集合の代表(根)を管理する
    //もし、要素xが根であればdata[x]は負の値を取り、-data[x]はxが属する集合の大きさに等しい
    vector<int> data;

    UnionFind(int sz) : data(sz, -1) {}

    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        bool is_union = (x != y);
        if (is_union) {
            if (data[x] > data[y]) swap(x, y);
            data[x] += data[y];
            data[y] = x;
        }
        return is_union;
    }

    int find(int x) {
        if (data[x] < 0) { //要素xが根である
            return x;
        } else {
            data[x] = find(data[x]); //data[x]がxの属する集合の根でない場合、根になるよう更新される
            return data[x];
        }
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    int size(int x) {
        return -data[find(x)];
    }
};

const ll MOD = 1000000007;

ll modpow(ll x, ll n, ll mod = MOD) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

// y * y >= x を満たす最小の整数yを求める
ll calcSquare(ll x) {
    ll ng = 0, ok = x;
    while (ok - ng > 1) {
        ll mid = (ng + ok) / 2;
        (mid * mid >= x ? ok : ng) = mid;
    }
    return ok;
}

// a * x + b * y == gcd(a, b) なるx, yを計算
// 返り値はgcd(a, b)
template<typename T>
T extgcd(T a, T b, T& x, T& y) {
    T d = a;
    if (b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1; y = 0;
    }
    return d;
}

// g^x == y (mod p) の解xを求める(なければ-1を返す)
ll modlog(ll g, ll y, ll p = MOD) {
    g %= p;
    y %= p;

    if (g == 1 && y != 1) return -1;
    if (y == 1) return 0;

    ll sq = calcSquare(p);

    unordered_map<ll, ll> baby_table;
    for (ll i = 0, b = 1; i < sq; i++) {
        baby_table[b] = i;
        (b *= g) %= p;
    }

    ll inv_gsq = modpow(y, sq, p);

    for (ll i = 0; i < sq; i++) {
        if (baby_table.find(y) != baby_table.end()) {
            return i * sq + baby_table[y];
        }
        (y *= inv_gsq) %= p;
    }
    return -1;
}

template<typename T>
vector<T> divisor(T n) {
    vector<T> res;
    for (T i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            res.emplace_back(i);
            if (i != n / i) res.emplace_back(n / i);
        }
    }
    return res;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        ll n;
        cin >> n;
        ll foo, bar;
        extgcd(2LL, 2 * n - 1, foo, bar);
        if (foo < 0) foo += 2 * n - 1;
        foo %= 2 * n - 1;
        ll ans = (n == 1 ? 1 : modlog(2, foo, 2 * n - 1) + 1);
        vector<ll> d = divisor(ans);
        sort(d.begin(), d.end());
        ans = -1;
        for (ll x : d) {
            if (modpow(2, x, 2 * n - 1) == 1) {
                ans = x;
                break;
            }
        }
        if (n == 1) ans = 1;
        cout << ans << newl;
    }
    return 0;
}
0