結果

問題 No.2324 Two Countries within UEC
ユーザー InTheBloomInTheBloom
提出日時 2023-05-28 14:52:49
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 223 ms / 2,000 ms
コード長 1,316 bytes
コンパイル時間 4,920 ms
コンパイル使用メモリ 160,264 KB
実行使用メモリ 4,360 KB
最終ジャッジ日時 2023-09-09 06:14:04
合計ジャッジ時間 11,073 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 203 ms
4,352 KB
testcase_01 AC 223 ms
4,356 KB
testcase_02 AC 39 ms
4,356 KB
testcase_03 AC 159 ms
4,352 KB
testcase_04 AC 179 ms
4,356 KB
testcase_05 AC 43 ms
4,352 KB
testcase_06 AC 75 ms
4,356 KB
testcase_07 AC 35 ms
4,356 KB
testcase_08 AC 152 ms
4,356 KB
testcase_09 AC 156 ms
4,352 KB
testcase_10 AC 222 ms
4,352 KB
testcase_11 AC 206 ms
4,352 KB
testcase_12 AC 66 ms
4,356 KB
testcase_13 AC 65 ms
4,352 KB
testcase_14 AC 40 ms
4,352 KB
testcase_15 AC 35 ms
4,360 KB
testcase_16 AC 151 ms
4,356 KB
testcase_17 AC 97 ms
4,352 KB
testcase_18 AC 41 ms
4,356 KB
testcase_19 AC 215 ms
4,356 KB
testcase_20 AC 28 ms
4,352 KB
testcase_21 AC 109 ms
4,352 KB
testcase_22 AC 143 ms
4,352 KB
testcase_23 AC 46 ms
4,352 KB
testcase_24 AC 116 ms
4,352 KB
testcase_25 AC 213 ms
4,352 KB
testcase_26 AC 114 ms
4,352 KB
testcase_27 AC 119 ms
4,352 KB
testcase_28 AC 114 ms
4,356 KB
testcase_29 AC 113 ms
4,356 KB
testcase_30 AC 113 ms
4,356 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 2 ms
4,356 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 2 ms
4,356 KB
testcase_35 AC 2 ms
4,352 KB
testcase_36 AC 1 ms
4,356 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 2 ms
4,352 KB
testcase_39 AC 2 ms
4,352 KB
testcase_40 AC 1 ms
4,356 KB
testcase_41 AC 2 ms
4,356 KB
testcase_42 AC 2 ms
4,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main () {
    long N, M, P, Q;
    {
        auto buf = readln.split.to!(long[]);
        N = buf[0], M = buf[1], P = buf[2], Q = buf[3];
    }
    solve(N, M, P, Q);
}

void solve (long N, long M, long P, long Q) {
    foreach (_; 0..Q) {
        auto buf = readln.split.to!(long[]);
        long x = buf[0], f = buf[1];

        if (x % P != 0) {
        // x^{-1}が存在
            long inv_x = modinv(x, P);
            long modular = (f * inv_x) % P;
            // y == modular(MOD P)

            if (M < modular) {
                writeln(0);
            } else if (modular != 0) {
                writeln(1 + (M - modular)/P);
            } else {
                writeln((M - modular)/P);
            }
        } else {
        // x^{-1}が存在しない(任意のyについて親交度が0である)
            if (f == 0) {
                writeln(M);
            } else {
                writeln(0);
            }
        }
    }
}

long modpow (long x, long a, const long MOD) {
    if (a == 0) {
        return 1;
    }
    if (a == 1) {
        return x;
    }
    if (a % 2 == 1) {
        return (x * modpow(x, a-1, MOD)) % MOD;
    }
    long t = modpow(x, a/2, MOD);
    return (t * t) % MOD;
}

long modinv (long x, const long MOD) {
    return modpow(x, MOD - 2, MOD);
}
0