結果

問題 No.3365 Prefix and Suffix X
コンテスト
ユーザー テナガザル
提出日時 2025-11-17 23:13:29
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,242 bytes
コンパイル時間 829 ms
コンパイル使用メモリ 104,916 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-11-17 23:14:14
合計ジャッジ時間 31,263 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 1
other TLE * 14 -- * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void solve()
{
  string x;
  int m;
  cin >> x >> m;
  int siz = x.size();
  for (int i = siz; i <= 18; ++i)
  {
    string ans(i, '#');
    for (int j = 0; j < siz; ++j)
    {
      ans[j] = x[j];
      ans[i - siz + j] = x[j];
    }
    if (ans.substr(0, siz) != x || ans.substr(i - siz, siz) != x) continue;
    long long d = 0;
    for (auto c : ans) d = (d * 10LL + (c == '#' ? 0 : c - '0')) % m;
    d = (m - d) % m;
    if (d == 0)
    {
      for (auto &c : ans) if (c == '#') c = '0';
      cout << ans << endl;
      return;
    }
    int flag = 1;
    for (int j = 0; j < 1000; ++j)
    {
      long long tmp = j * m + d;
      if (tmp % 1000 == 0)
      {
        tmp /= 1000;
        for (int k = i - 4; k >= 0 && tmp > 0; --k)
        {
          ans[k] = tmp % 10;
          tmp /= 10;
        }
        if (tmp > 0) continue;
        flag = 0;
        break;
      }
    }
    for (auto c : ans) if (c == '#') flag = 1;
    if (ans.substr(0, siz) != x || ans.substr(i - siz, siz) != x) flag = 1;
    if (flag) continue;
    cout << ans << endl;
    return;
  }
  cout << -1 << endl;
}

int main()
{
  int t;
  cin >> t;
  while (t--) solve();
}
0