結果

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

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;

using isize = size_t;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
using f64 = long double;
using p2 = pair<i64, i64>;
using el = tuple<i64, i64, i64>;
using mint = atcoder::modint998244353;

void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(18);
  _main();
}

void solve(string s, i64 m) {
  vector<i64> ten(20, 1);
  for (i64 i = 1; i < ten.size(); i++) ten[i] = ten[i - 1] * 10 % m;
  for (i64 i = 0; i < s.size(); i++) {
    bool ok = true;
    for (i64 j = 0; i + j < s.size(); j++) {
      ok &= s[i + j] == s[j];
    }
    if (!ok) continue;
    i64 x = 0;
    for (i64 j = 0; j < s.size(); j++) x = x * 10 + s[j] - '0';
    for (i64 j = s.size() - i; j < s.size(); j++) x = x * 10 + s[j] - '0';
    if (x % m == 0) {
      cout << x << "\n";
      return;
    }
  }
  i64 B = 1;
  for (i64 i = 0; i < s.size(); i++) B *= 10;
  for (i64 d = 1; d + s.size() * 2 <= 18; d++) {
    i64 p = 0;
    for (i64 i = s.size() - 1; i >= 0; i--) {
      p += (s[s.size() - i - 1] - '0') * ten[i + d + s.size()] % m;
      p += (s[s.size() - i - 1] - '0') * ten[i] % m;
      p %= m;
    }
    p = (m - p) % m;
    vector<i64> rrr = {0, p}, mmm = {B, m};
    auto [R, M] = atcoder::crt(rrr, mmm);
    if (M == 0) continue;
    if (R == 0) {
      cout << s;
      for (i64 c = 0; c < d; c++) cout << "0";
      cout << s;
      cout << "\n";
      return;
    }
    for (i64 c = 0; c < s.size(); c++) {
      assert(R % 10 == 0);
      R /= 10;
    }
    string t = to_string(R);
    if (t.size() > d) continue;
    reverse(t.begin(), t.end());
    while (t.size() < d) t += '0';
    reverse(t.begin(), t.end());
    cout << s << t << s << "\n";
    return;
  }
  cout << "-1\n";
}
void _main() {
  i64 ttt;
  cin >> ttt;
  for (;ttt--;) {
    string s;
    i64 m;
    cin >> s >> m;
    solve(s, m);
  }
}
0