結果

問題 No.3179 3 time mod
ユーザー 👑 hos.lyric
提出日時 2025-06-13 21:29:25
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,440 bytes
コンパイル時間 954 ms
コンパイル使用メモリ 110,872 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-06-14 01:38:46
合計ジャッジ時間 2,018 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:118:38: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  118 |     for (int i = 0; i < 3; ++i) scanf("%lld", &M[i]);
      |                                 ~~~~~^~~~~~~~~~~~~~~
main.cpp:119:38: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  119 |     for (int i = 0; i < 3; ++i) scanf("%lld", &B[i]);
      |                                 ~~~~~^~~~~~~~~~~~~~~

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")

// a x + b y = (+/-) gcd(a, b)
//   (a, 0): g = a, x = 1, y = 0
//   (0, b), (b, b), (-b, b) (b != 0): g = b, x = 0, y = 1
//   otherwise: 2 |x| <= |b|, 2 |y| <= |a|
// S: signed integer
template <class S> S gojo(S a, S b, S &x, S &y) {
  if (b != 0) {
    const S g = gojo(b, a % b, y, x);
    y -= (a / b) * x;
    return g;
  } else {
    x = 1;
    y = 0;
    return a;
  }
}

// x == b0  (mod m0),  x == b1  (mod m1)
// S: signed integer
template <class S>
pair<S, S> modSystem(S m0, S b0, S m1, S b1) {
  assert(m0 >= 1);
  assert(m1 >= 1);
  if ((b0 %= m0) < 0) b0 += m0;
  if ((b1 %= m1) < 0) b1 += m1;
  if (m0 < m1) {
    swap(m0, m1);
    swap(b0, b1);
  }
  // to avoid overflow
  if (m0 % m1 == 0) {
    if (b0 % m1 != b1) return make_pair(0, 0);
    return make_pair(m0, b0);
  }
  S z0, z1;
  const S g = gojo(m0, m1, z0, z1);
  b1 -= b0;
  if (b1 % g != 0) return make_pair(0, 0);
  (b1 /= g) %= m1;
  m1 /= g;
  b0 += m0 * ((z0 * b1) % m1);
  m0 *= m1;
  if (b0 < 0) b0 += m0;
  return make_pair(m0, b0);
}
template <class S>
pair<S, S> modSystem(const pair<S, S> &mb0, const pair<S, S> &mb1) {
  return modSystem(mb0.first, mb0.second, mb1.first, mb1.second);
}

// x == bs[i]  (mod ms[i])
// S: signed integer
template <class S>
pair<S, S> modSystem(const vector<S> &ms, const vector<S> &bs) {
  assert(ms.size() == bs.size());
  const int len = ms.size();
  pair<S, S> mb0(1, 0);
  for (int i = 0; i < len; ++i) {
    assert(ms[i] >= 1);
    mb0 = modSystem(mb0.first, mb0.second, ms[i], bs[i]);
    if (!mb0.first) return mb0;
  }
  return mb0;
}
template <class S>
pair<S, S> modSystem(const vector<pair<S, S>> &mbs) {
  pair<S, S> mb0(1, 0);
  for (const auto &mb1 : mbs) {
    assert(mb1.first >= 1);
    mb0 = modSystem(mb0, mb1);
    if (!mb0.first) return mb0;
  }
  return mb0;
}


int main() {
  Int N;
  vector<Int> M(3), B(3);
  for (; ~scanf("%lld", &N); ) {
    for (int i = 0; i < 3; ++i) scanf("%lld", &M[i]);
    for (int i = 0; i < 3; ++i) scanf("%lld", &B[i]);
    const auto res = modSystem(M, B);
    Int ans = 0;
    if (res.first && res.second <= N) {
      ans += (N - res.second) / res.first + 1;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
0