結果
問題 | No.2324 Two Countries within UEC |
ユーザー |
|
提出日時 | 2023-05-28 13:36:44 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 216 ms / 2,000 ms |
コード長 | 1,089 bytes |
コンパイル時間 | 2,842 ms |
コンパイル使用メモリ | 216,552 KB |
最終ジャッジ日時 | 2025-02-13 09:59:17 |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 41 |
ソースコード
#pragma GCC target("avx2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> using namespace std; using ll = long long int; using ull = unsigned long long int; using ld = long double; ll modinv(ll A,ll M){ //A*r+B*n=gcd(A,B) A %= M; if(A == 0 || __gcd(A,M) != 1){ //cout << "Error modinv(" << A << "," << M << ")" << endl; return -1; } ll B = M,U = 1,V = 0; while(B){ ll T = A / B; A -= T * B; swap(A,B); U -= T * V; swap(U,V); } U %= M; if(U < 0) U += M; return U; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); ll N,M,P,Q; cin >> N >> M >> P >> Q; for(ll q = 0;q < Q;q++){ ll x,f; cin >> x >> f; x %= P; if(x == 0){ if(f == 0) cout << M << endl; else cout << 0 << endl; } else{ ll y = (f * modinv(x,P)) % P; ll ans = M / P; if(y != 0 && y <= M % P) ans++; cout << ans << endl; } } }