結果

問題 No.2324 Two Countries within UEC
コンテスト
ユーザー 緑野
提出日時 2026-04-18 00:34:36
言語 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  
実行時間 178 ms / 2,000 ms
コード長 963 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,101 ms
コンパイル使用メモリ 330,048 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-04-18 00:34:50
合計ジャッジ時間 11,153 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
const int INF = int(1e+9) + 1;
const ll INFL = ll(4e+18) + 1;
void chmax(ll &a, ll b) { a = max(a, b); }
void chmin(ll &a, ll b) { a = min(a, b); }

// a^b mod m を求める(繰り返し二乗法でO(log_{b}))
ll modpow(ll a, ll b, ll m) {
    ll p = a, ans = 1;
    for (int i = 0; i < 60; i++) {
        if ((b>>i) & 1L) {ans = (ans*p)%m;}
        p = (p*p)%m;
    }
    return ans;
}

int main() {
    ll N, M, P, Q; cin >> N >> M >> P >> Q;
    rep(_, Q) {
        ll x, f; cin >> x >> f;
        if (x%P == 0) {
            cout << (f==0 ? M : 0) << '\n';
            continue;
        }
        ll x_inv = modpow(x, P-2, P);
        ll y = (f * x_inv)%P;
        ll ans;
        if (y==0) {ans = M/P;}
        else {
            if (M-y>=0) {ans = (M-y)/P+1;}
            else {ans = 0;}
        }
        cout << ans << '\n';
    }
}
0