結果
| 問題 |
No.3365 Prefix and Suffix X
|
| コンテスト | |
| ユーザー |
こめだわら
|
| 提出日時 | 2025-11-17 23:11:21 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 512 ms / 2,000 ms |
| コード長 | 3,186 bytes |
| コンパイル時間 | 5,782 ms |
| コンパイル使用メモリ | 335,332 KB |
| 実行使用メモリ | 7,720 KB |
| 最終ジャッジ日時 | 2025-11-17 23:11:43 |
| 合計ジャッジ時間 | 21,675 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 30 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define rep(i,n) for(ll i=0;i<n;++i)
#define all(a) (a).begin(),(a).end()
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
template<class T> T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); }
template<class T> T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); }
template <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }
template <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }
template<typename T>
ostream &operator<<(ostream &os, const vector<T> &a){
if (a.empty()) return os;
os << a.front();
for (auto e : a | views::drop(1)){
os << ' ' << e;
}
return os;
}
void dump(auto ...vs){
((cout << vs << ' '), ...) << endl;
}
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
ll solve_all(ll X,ll M,ll e){
ll ans=2e18;
ll te=intpow(10,e);
for (ll d=e;d<=18-e;d++){
ll r=X*(1+pow_mod(10,d,M));
r=M-r;
r%=M;
r+=M;
r%=M;
ll g=gcd(te,M);
ll tg=te/g;
ll mg=M/g;
if (r%g!=0){
continue;
}
r/=g;
ll y=r*inv_mod(tg,mg);
y%=mg;
if (y<intpow(10,d-e)){
chmin(ans,X*(1+intpow(10,d))+y*te);
}
}
return ans;
}
void solve1(ll X,ll M){
string xs=to_string(X);
if (X%M==0){
cout<<X<<'\n';
return;
}
ll ans=solve_all(X,M,1);
if (ans>=1e18){
cout<<-1<<'\n';
}
else{
cout<<ans<<'\n';
}
return;
}
void solve2(ll X,ll M){
string xs=to_string(X);
if (X%M==0){
cout<<X<<'\n';
return;
}
if (xs[0]==xs[1]){
ll a=(xs[0]-'0')*100+X;
if (a%M==0){
cout<<a<<'\n';
return;
}
}
ll ans=solve_all(X,M,2);
if (ans>=1e18){
cout<<-1<<'\n';
}
else{
cout<<ans<<'\n';
}
}
void solve3(ll X,ll M){
string xs=to_string(X);
if (X%M==0){
cout<<X<<'\n';
return;
}
if (xs[0]==xs[1] and xs[1]==xs[2]){
ll a=(xs[0]-'0')*1000+X;
if (a%M==0){
cout<<a<<'\n';
return;
}
}
if (xs[0]==xs[2]){
ll a=(xs[0]-'0')*10000+(xs[1]-'0')*1000+X;
if (a%M==0){
cout<<a<<'\n';
return;
}
}
ll ans=solve_all(X,M,3);
if (ans>=1e18){
cout<<-1<<'\n';
}
else{
cout<<ans<<'\n';
}
}
void solve() {
ll X,M;
cin>>X>>M;
string xs=to_string(X);
if (xs.size()==1){
solve1(X,M);
}
else if (xs.size()==2){
solve2(X,M);
}
else if (xs.size()==3){
solve3(X,M);
}
return;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
ll T=1;
cin>>T;
while (T--){
solve();
}
return 0;
}
こめだわら