結果

問題 No.97 最大の値を求めるくえり
ユーザー mayoko_mayoko_
提出日時 2015-07-26 09:46:50
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 529 ms / 5,000 ms
コード長 2,232 bytes
コンパイル時間 615 ms
コンパイル使用メモリ 80,988 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-22 23:26:16
合計ジャッジ時間 7,248 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 10 ms
4,376 KB
testcase_02 AC 237 ms
4,376 KB
testcase_03 AC 240 ms
4,380 KB
testcase_04 AC 141 ms
4,376 KB
testcase_05 AC 141 ms
4,380 KB
testcase_06 AC 142 ms
4,380 KB
testcase_07 AC 150 ms
4,380 KB
testcase_08 AC 161 ms
4,380 KB
testcase_09 AC 179 ms
4,376 KB
testcase_10 AC 219 ms
4,376 KB
testcase_11 AC 258 ms
4,380 KB
testcase_12 AC 303 ms
4,376 KB
testcase_13 AC 335 ms
4,380 KB
testcase_14 AC 529 ms
4,380 KB
testcase_15 AC 364 ms
4,384 KB
testcase_16 AC 279 ms
4,380 KB
testcase_17 AC 206 ms
4,380 KB
testcase_18 AC 196 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
#include<complex>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef complex<double> C;

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, int A[]) {
    for(int i = 0; i < N; ++ i)
        A[i] = xor128() % 100003;
}
int A[100007];
const ll MOD = 100003;

// extgcd
ll extgcd(ll a, ll b, ll& x, ll& y) {
    ll d = a;
    if (b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1; y = 0;
    }
    return d;
}
// mod_inverse
ll mod_inverse(ll a, ll m = MOD) {
    ll x, y;
    extgcd(a, m, x, y);
    return (m+x%m) % m;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, Q;
    cin >> N >> Q;
    generateA(N, A);
    if (N < 3000) {
        while (Q--) {
            ll q;
            cin >> q;
            ll ans = 0;
            for (int i = 0; i < N; i++) {
                ans = max(ans, (q*A[i])%MOD);
            }
            cout << ans << endl;
        }
    } else {
        sort(A, A+N);
        while (Q--) {
            ll q;
            cin >> q;
            if (q == 0) {
                cout << 0 << endl;
                continue;
            }
            int ans = MOD-1;
            ll iq = mod_inverse(q);
            while (1) {
                ll p = (iq * ans) % MOD;
                if (binary_search(A, A+N, p)) {
                    cout << ans << endl;
                    break;
                }
                ans--;
            }
        }
    }
    return 0;
}
0