結果

問題 No.3365 Prefix and Suffix X
コンテスト
ユーザー risujiroh
提出日時 2025-11-17 21:59:44
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 369 ms / 2,000 ms
コード長 3,414 bytes
コンパイル時間 3,113 ms
コンパイル使用メモリ 295,664 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-11-17 22:00:01
合計ジャッジ時間 15,898 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#if __INCLUDE_LEVEL__ == 0

#include __BASE_FILE__

using Mint = atcoder::modint;

void Solve() {
  string X;
  int M;
  IN(X, M);

  Mint::set_mod(M);

  for (int o : Rep1(0, Sz(X))) {
    string s = X;
    for (int i : Rep(0, Sz(X))) {
      if (o + i < Sz(X)) {
        if (s[o + i] != X[i]) {
          s.clear();
          break;
        }
      } else {
        s += X[i];
      }
    }
    if (!s.empty()) {
      Mint t = 0;
      for (char c : s) {
        t *= 10;
        t += c - '0';
      }
      if (t == 0) {
        OUT(s);
        return;
      }
    }
  }

  for (int w : Rep1(1, 18 - 2 * Sz(X))) {
    Mint::set_mod(M);
    Mint A = Mint(10).pow(Sz(X));
    Mint B = 0;
    for (char c : X) {
      B *= 10;
      B += c - '0';
    }
    B *= Mint(10).pow(w + Sz(X)) + 1;
    B = -B;

    int g = gcd(A.val(), M);
    if (B.val() % g) {
      continue;
    }
    A = A.val() / g;
    B = B.val() / g;
    Mint::set_mod(M / g);
    if (A == 0) {
      if (B == 0) {
        OUT(X + string(w, '0') + X);
        return;
      }
    } else {
      if ((B / A).val() < Pow(INT64_C(10), w)) {
        OUT(X + to_string(Pow(INT64_C(10), w) + (B / A).val()).substr(1) + X);
        return;
      }
    }
  }

  OUT(-1);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int t;
  IN(t);
  while (t--) {
    Solve();
  }
}

#elif __INCLUDE_LEVEL__ == 1

#include <bits/stdc++.h>

#include <atcoder/modint.hpp>

template <class T, std::integral N, class Op = std::multiplies<>>
constexpr T Pow1(T x, N n, Op op = {}) {
  assert(n >= 1);
  while (n % 2 == 0) {
    x = op(x, x);
    n >>= 1;
  }
  T ret = x;
  while (n >>= 1) {
    x = op(x, x);
    if (n % 2) {
      ret = op(ret, x);
    }
  }
  return ret;
}

template <class T, std::integral N>
constexpr T Pow(T x, N n) {
  if (n < 0) {
    if constexpr (requires { T(1) / x; }) {
      x = T(1) / x;
      n = -n;
    } else {
      assert(n >= 0);
    }
  }
  if (n == 0) {
    return T(1);
  }
  return Pow1(x, n);
}

template <class T> concept MyRange = std::ranges::range<T> && !std::convertible_to<T, std::string_view>;
template <class T> concept MyTuple = std::__is_tuple_like<T>::value && !MyRange<T>;

namespace std {

istream& operator>>(istream& is, MyRange auto&& r) {
  for (auto&& e : r) is >> e;
  return is;
}
istream& operator>>(istream& is, MyTuple auto&& t) {
  apply([&](auto&... xs) { (is >> ... >> xs); }, t);
  return is;
}

ostream& operator<<(ostream& os, MyRange auto&& r) {
  auto sep = "";
  for (auto&& e : r) os << exchange(sep, " ") << e;
  return os;
}
ostream& operator<<(ostream& os, MyTuple auto&& t) {
  auto sep = "";
  apply([&](auto&... xs) { ((os << exchange(sep, " ") << xs), ...); }, t);
  return os;
}

template <class T, atcoder::internal::is_modint_t<T>* = nullptr>
istream& operator>>(istream& is, T& x) {
  int v;
  is >> v;
  x = T::raw(v);
  return is;
}

template <class T, atcoder::internal::is_modint_t<T>* = nullptr>
ostream& operator<<(ostream& os, const T& x) {
  return os << x.val();
}

}  // namespace std

using namespace std;

#define Rep(...) [](int l, int r) { return views::iota(min(l, r), r); }(__VA_ARGS__)
#define Rep1(...) [](int l, int r) { return Rep(l, r + 1); }(__VA_ARGS__)
#define Sz(r) int(size(r))
#define IN(...) (cin >> forward_as_tuple(__VA_ARGS__))
#define OUT(...) (cout << forward_as_tuple(__VA_ARGS__) << '\n')

#endif  // __INCLUDE_LEVEL__ == 1
0