結果

問題 No.2324 Two Countries within UEC
ユーザー tnakao0123
提出日時 2023-06-24 02:16:24
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 1,576 bytes
コンパイル時間 286 ms
コンパイル使用メモリ 41,928 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 05:52:11
合計ジャッジ時間 4,869 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2324.cc:  No.2324 Two Countries within UEC - yukicoder
 */

#include<cstdio>
#include<algorithm>
 
using namespace std;

/* constant */

/* typedef */

struct MI {
  static int MOD;
  int v;
  MI(): v() {}
  MI(int _v): v(_v % MOD) {}
  MI(long long _v): v(_v % MOD) {}

  MI operator+(const MI m) const { return MI(v + m.v); }
  MI operator-(const MI m) const { return MI(v + MOD - m.v); }
  MI operator*(const MI m) const { return MI((long long)v * m.v); }

  MI &operator+=(const MI m) { return (*this = *this + m); }
  MI &operator-=(const MI m) { return (*this = *this - m); }
  MI &operator*=(const MI m) { return (*this = *this * m); }

  bool operator==(const MI m) const { return v == m.v; }
  bool operator!=(const MI m) const { return v != m.v; }

  MI pow(int n) const {  // a^n % MOD
    MI pm = 1, a = *this;
    while (n > 0) {
      if (n & 1) pm *= a;
      a *= a;
      n >>= 1;
    }
    return pm;
  }

  MI inv() const { return pow(MOD - 2); }
  MI operator/(const MI m) const { return *this * m.inv(); }
  MI &operator/=(const MI m) { return (*this = *this / m); }
};

int MI::MOD;

/* global variables */

/* subroutines */

/* main */

int main() {
  int n, m, p, qn;
  scanf("%d%d%d%d", &n, &m, &p, &qn);
  MI::MOD = p;

  while (qn--) {
    int xi, fi;
    scanf("%d%d", &xi, &fi);

    MI x(xi);
    int c;
    if (x.v == 0)
      c = (fi == 0) ? m : 0;
    else {
      MI y = MI(fi) / x;
      int q = m / p, r = m % p;
      c = q + ((y.v > 0 && y.v <= r) ? 1 : 0);
    }

    printf("%d\n", c);
  }
  return 0;
}
0