結果
| 問題 | No.3750 Mischievous Resident (Easy) |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-08-06 10:08:01 |
| 言語 | C++17 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
RE
不安定
|
| 実行時間 | - |
| コード長 | 2,666 bytes |
| 記録 | |
| コンパイル時間 | 1,225 ms |
| コンパイル使用メモリ | 220,740 KB |
| 実行使用メモリ | 9,920 KB |
| 最終ジャッジ日時 | 2026-10-02 20:51:01 |
| 合計ジャッジ時間 | 6,097 ms |
|
ジャッジサーバーID (参考情報) |
judge1_1 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 3 RE * 31 TLE * 1 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
const long long MAX_STATES = 2'000'000;
int Q;
cin >> Q;
while (Q--) {
int N, M;
cin >> N >> M;
vector<int> S(M), G(M);
for (int& x : S) cin >> x;
for (int& x : G) cin >> x;
vector<long long> power(M, 1);
long long states = 1;
bool too_large = false;
for (int i = 0; i < M; ++i) {
if (states > MAX_STATES / N) {
too_large = true;
break;
}
if (i + 1 < M) {
power[i + 1] = power[i] * N;
}
states *= N;
}
if (too_large) {
cerr << "State space is too large: N = "
<< N << ", M = " << M << '\n';
return 1;
}
auto encode = [&](const vector<int>& P) {
long long id = 0;
for (int i = 0; i < M; ++i) {
id += 1LL * (P[i] - 1) * power[i];
}
return id;
};
long long start = encode(S);
long long goal = encode(G);
vector<char> visited(states, false);
queue<long long> que;
visited[start] = true;
que.push(start);
bool answer = false;
vector<int> P(M);
while (!que.empty()) {
long long state = que.front();
que.pop();
if (state == goal) {
answer = true;
break;
}
long long x = state;
for (int i = 0; i < M; ++i) {
P[i] = x % N + 1;
x /= N;
}
// F 階のボタンを押す。
for (int F = 1; F <= N; ++F) {
int min_distance = N;
for (int i = 0; i < M; ++i) {
min_distance =
min(min_distance, abs(P[i] - F));
}
// 最短距離の台が複数あれば、
// そのどれを選ぶ遷移もすべて試す。
for (int i = 0; i < M; ++i) {
if (abs(P[i] - F) != min_distance) {
continue;
}
long long next_state =
state + 1LL * (F - P[i]) * power[i];
if (!visited[next_state]) {
visited[next_state] = true;
que.push(next_state);
}
}
}
}
cout << (answer ? "Yes\n" : "No\n");
}
}