結果

問題 No.97 最大の値を求めるくえり
ユーザー kimiyukikimiyuki
提出日時 2016-12-20 17:29:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 315 ms / 5,000 ms
コード長 1,883 bytes
コンパイル時間 1,405 ms
コンパイル使用メモリ 86,676 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-21 03:18:20
合計ジャッジ時間 7,724 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 257 ms
4,376 KB
testcase_03 AC 260 ms
4,376 KB
testcase_04 AC 165 ms
4,376 KB
testcase_05 AC 163 ms
4,380 KB
testcase_06 AC 166 ms
4,380 KB
testcase_07 AC 171 ms
4,376 KB
testcase_08 AC 183 ms
4,376 KB
testcase_09 AC 202 ms
4,384 KB
testcase_10 AC 239 ms
4,376 KB
testcase_11 AC 278 ms
4,376 KB
testcase_12 AC 315 ms
4,380 KB
testcase_13 AC 219 ms
4,376 KB
testcase_14 AC 212 ms
4,376 KB
testcase_15 AC 204 ms
4,380 KB
testcase_16 AC 201 ms
4,380 KB
testcase_17 AC 200 ms
4,380 KB
testcase_18 AC 199 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <set>
#include <map>
#include <queue>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <functional>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
#define repeat_reverse(i,n) for (int i = (n)-1; (i) >= 0; --(i))
typedef long long ll;
using namespace std;
template <class T> void setmax(T & a, T const & b) { if (a < b) a = b; }

ll powi(ll x, ll y, ll p) { // O(log y)
    assert (y >= 0);
    x = (x % p + p) % p;
    ll z = 1;
    for (ll i = 1; i <= y; i <<= 1) {
        if (y & i) z = z * x % p;
        x = x * x % p;
    }
    return z;
}
ll inv(ll x, ll p) { // p must be a prime, O(log p)
    assert ((x % p + p) % p != 0);
    return powi(x, p-2, p);
}

unsigned xor128_x = 123456789, xor128_y = 362436069, xor128_z = 521288629, xor128_w = 88675123;
unsigned xor128() {
    unsigned t = xor128_x ^ (xor128_x << 11);
    xor128_x = xor128_y; xor128_y = xor128_z; xor128_z = xor128_w;
    return xor128_w = xor128_w ^ (xor128_w >> 19) ^ (t ^ (t >> 8));
}
void generateA(int N, vector<int> & A) {
    for(int i = 0; i < N; ++ i) A[i] = xor128() % 100003;
}
const int mod = 100003;
int main() {
    int n, q; cin >> n >> q;
    vector<int> a(n); generateA(n, a);
    vector<int> in_a(mod); repeat (i,n) in_a[a[i]] = true;
    while (q --) {
        int b; cin >> b;
        int ans = -1;
        if (b == 0) {
            ans = 0;
        } else if (n < 1000) {
            ans = -1;
            repeat (i,n) setmax<int>(ans, a[i] *(ll) b % mod);
        } else {
            int b_inv = inv(b, mod);
            repeat_reverse (c,mod) {
                if (in_a[b_inv *(ll) c % mod]) {
                    ans = c;
                    break;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}
0