結果

問題 No.2324 Two Countries within UEC
コンテスト
ユーザー D M
提出日時 2026-03-06 10:25:07
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 54 ms / 2,000 ms
コード長 1,029 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,140 ms
コンパイル使用メモリ 333,924 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2026-03-06 10:25:41
合計ジャッジ時間 6,107 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

using int64 = long long;

// a^e mod mod
int64 mod_pow(int64 a, int64 e, int64 mod) {
    int64 r = 1 % mod;
    while (e > 0) {
        if (e & 1) r = (r * a) % mod;
        a = (a * a) % mod;
        e >>= 1;
    }
    return r;
}

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

    int64 N, M, P;
    int Q;
    cin >> N >> M >> P >> Q;

    while (Q--) {
        int64 x, f;
        cin >> x >> f;

        int64 xm = x % P;

        // x ≡ 0 (mod P)
        if (xm == 0) {
            if (f == 0) cout << M << '\n';
            else cout << 0 << '\n';
            continue;
        }

        // y ≡ f * x^{-1} (mod P)
        int64 inv = mod_pow(xm, P - 2, P);   // フェルマーの小定理
        int64 r = (f * inv) % P;

        int64 ans = 0;
        if (r == 0) {
            ans = M / P;
        } else {
            if (r <= M) ans = (M - r) / P + 1;
            else ans = 0;
        }

        cout << ans << '\n';
    }

    return 0;
}
0