結果

問題 No.2324 Two Countries within UEC
ユーザー KoseTKoseT
提出日時 2023-05-28 14:44:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 97 ms / 2,000 ms
コード長 1,226 bytes
コンパイル時間 2,323 ms
コンパイル使用メモリ 201,276 KB
実行使用メモリ 4,752 KB
最終ジャッジ日時 2023-09-09 06:13:20
合計ジャッジ時間 5,973 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
4,552 KB
testcase_01 AC 95 ms
4,496 KB
testcase_02 AC 18 ms
4,376 KB
testcase_03 AC 71 ms
4,380 KB
testcase_04 AC 81 ms
4,384 KB
testcase_05 AC 19 ms
4,376 KB
testcase_06 AC 33 ms
4,376 KB
testcase_07 AC 16 ms
4,376 KB
testcase_08 AC 66 ms
4,380 KB
testcase_09 AC 70 ms
4,412 KB
testcase_10 AC 96 ms
4,636 KB
testcase_11 AC 89 ms
4,640 KB
testcase_12 AC 29 ms
4,376 KB
testcase_13 AC 29 ms
4,380 KB
testcase_14 AC 18 ms
4,376 KB
testcase_15 AC 16 ms
4,376 KB
testcase_16 AC 65 ms
4,376 KB
testcase_17 AC 42 ms
4,380 KB
testcase_18 AC 20 ms
4,376 KB
testcase_19 AC 95 ms
4,552 KB
testcase_20 AC 13 ms
4,376 KB
testcase_21 AC 38 ms
4,376 KB
testcase_22 AC 50 ms
4,500 KB
testcase_23 AC 17 ms
4,376 KB
testcase_24 AC 41 ms
4,376 KB
testcase_25 AC 97 ms
4,532 KB
testcase_26 AC 29 ms
4,752 KB
testcase_27 AC 31 ms
4,592 KB
testcase_28 AC 28 ms
4,572 KB
testcase_29 AC 28 ms
4,680 KB
testcase_30 AC 28 ms
4,500 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,500 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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';
    }
  }
}
0