結果
問題 |
No.2324 Two Countries within UEC
|
ユーザー |
|
提出日時 | 2023-05-28 14:44:14 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 98 ms / 2,000 ms |
コード長 | 1,226 bytes |
コンパイル時間 | 2,022 ms |
コンパイル使用メモリ | 195,076 KB |
最終ジャッジ日時 | 2025-02-13 11:58:17 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 41 |
ソースコード
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, m, p; int q; cin >> n >> m >> p >> q; vector<pair<long long, long long>> query(q); for (int i=0;i<q;++i) cin >> query[i].first >> query[i].second; /** * query[i].first * y (mod p) == query[i].second * 1 <= y <= m */ /** * x * y = cp + f * x * y'= c'p+ f * x * (y-y') = (c-c') * p * * if (x == d * p) => d * (y-y') = (c-c') * else => x * (p *(a-a')) = (c-c') * p */ for (int i=0;i<q;++i) { long long x = query[i].first; long long f = query[i].second; if (x % p == 0) { if (f == 0) cout << m << '\n'; else cout << 0 << '\n'; } else { // 1つめの解 // y = x^(p-2) (mod p) // x * y = 1 (mod p) // x * (f*y) = f (mod p) long long d {x}; long long y {1}; for (long long ct = p-2;ct != 0;ct >>= 1) { if (ct & 1) y *= d; y %= p; d *= d; d %= p; } y *= f; y %= p; //cout << y << ':'; long long ans {}; if (y <= m) { ans = (m - y + p)/p; if (f == 0) --ans; } cout << ans << '\n'; } } }