提出 #73500532
ソースコード 拡げる
#include <iostream>
#include <cstring>
using namespace std;
const int MAX_M = 1000;
int M, A, B;
// 0:未检测, 1:合法, 2:不合法
int status[MAX_M][MAX_M];
// 标记当前DFS路径中的状态(检测循环)
bool in_path[MAX_M][MAX_M];
// 计算 (a % mod),确保结果非负
inline int mod(int a, int mod) {
return (a % mod + mod) % mod;
}
// 检测状态(x,y)是否合法,返回true=合法,false=不合法
bool check(int x, int y) {
// 初始状态含0 → 不合法
if (x == 0 || y == 0) {
status[x][y] = 2;
return false;
}
// 已检测过,直接返回结果
if (status[x][y] != 0) {
return status[x][y] == 1;
}
// 路径中重复出现 → 进入循环且无0,合法
if (in_path[x][y]) {
status[x][y] = 1;
return true;
}
in_path[x][y] = true; // 标记进入当前路径
// 计算下一个状态(严格保证非负)
long long next = 1LL * A * y + 1LL * B * x;
int ny = mod(next, M);
int nx = y;
// 下一个状态的y是0 → 当前状态不合法
bool valid = true;
if (ny == 0) {
valid = false;
} else {
// 递归检测下一个状态
valid = check(nx, ny);
}
in_path[x][y] = false; // 回溯,取消路径标记
// 记忆当前状态结果
status[x][y] = valid ? 1 : 2;
return valid;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> M >> A >> B;
// 初始化状态数组
memset(status, 0, sizeof(status));
memset(in_path, 0, sizeof(in_path));
int ans = 0;
// 遍历所有可能的(x,y)对
for (int x = 0; x < M; ++x) {
for (int y = 0; y < M; ++y) {
if (check(x, y)) {
ans++;
}
}
}
cout << ans << endl;
return 0;
}
提出情報
| 提出日時 | |
|---|---|
| 問題 | E - Multiple-Free Sequences |
| ユーザ | stone_shadow |
| 言語 | C++23 (GCC 15.2.0) |
| 得点 | 450 |
| コード長 | 1915 Byte |
| 結果 | AC |
| 実行時間 | 19 ms |
| メモリ | 9072 KiB |
ジャッジ結果
| セット名 | Sample | All | ||||
|---|---|---|---|---|---|---|
| 得点 / 配点 | 0 / 0 | 450 / 450 | ||||
| 結果 |
|
|
| セット名 | テストケース |
|---|---|
| Sample | 00-sample-01.txt, 00-sample-02.txt, 00-sample-03.txt |
| All | 00-sample-01.txt, 00-sample-02.txt, 00-sample-03.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt, 01-31.txt, 01-32.txt, 01-33.txt |
| ケース名 | 結果 | 実行時間 | メモリ |
|---|---|---|---|
| 00-sample-01.txt | AC | 3 ms | 8556 KiB |
| 00-sample-02.txt | AC | 6 ms | 8560 KiB |
| 00-sample-03.txt | AC | 15 ms | 8540 KiB |
| 01-01.txt | AC | 3 ms | 8412 KiB |
| 01-02.txt | AC | 3 ms | 8476 KiB |
| 01-03.txt | AC | 3 ms | 8596 KiB |
| 01-04.txt | AC | 3 ms | 8560 KiB |
| 01-05.txt | AC | 7 ms | 8520 KiB |
| 01-06.txt | AC | 9 ms | 8540 KiB |
| 01-07.txt | AC | 18 ms | 8600 KiB |
| 01-08.txt | AC | 19 ms | 8524 KiB |
| 01-09.txt | AC | 19 ms | 8600 KiB |
| 01-10.txt | AC | 17 ms | 8596 KiB |
| 01-11.txt | AC | 8 ms | 8540 KiB |
| 01-12.txt | AC | 8 ms | 8412 KiB |
| 01-13.txt | AC | 8 ms | 8476 KiB |
| 01-14.txt | AC | 8 ms | 8624 KiB |
| 01-15.txt | AC | 7 ms | 8400 KiB |
| 01-16.txt | AC | 7 ms | 8528 KiB |
| 01-17.txt | AC | 6 ms | 8540 KiB |
| 01-18.txt | AC | 6 ms | 8596 KiB |
| 01-19.txt | AC | 6 ms | 8500 KiB |
| 01-20.txt | AC | 6 ms | 8348 KiB |
| 01-21.txt | AC | 6 ms | 8436 KiB |
| 01-22.txt | AC | 8 ms | 8416 KiB |
| 01-23.txt | AC | 8 ms | 8596 KiB |
| 01-24.txt | AC | 8 ms | 8496 KiB |
| 01-25.txt | AC | 15 ms | 8440 KiB |
| 01-26.txt | AC | 8 ms | 9072 KiB |
| 01-27.txt | AC | 14 ms | 8480 KiB |
| 01-28.txt | AC | 13 ms | 8528 KiB |
| 01-29.txt | AC | 8 ms | 8520 KiB |
| 01-30.txt | AC | 17 ms | 8608 KiB |
| 01-31.txt | AC | 19 ms | 8624 KiB |
| 01-32.txt | AC | 19 ms | 8480 KiB |
| 01-33.txt | AC | 19 ms | 8564 KiB |