This commit is contained in:
2026-08-06 23:20:25 +08:00
parent f87523a003
commit 94bdf44889
11 changed files with 426 additions and 50 deletions
+33 -21
View File
@@ -29,42 +29,54 @@ vector<int> get_divisors(const int n) {
return small_divisors;
}
/// 需要保证 sorted_cut_sticks 有序;需要保证 target_length 是总长度的因数
bool verify_length_dfs(const vector<int>& sorted_cut_sticks,
const int target_length,
const int remaining_length,
const int original_sticks,
const int remaining_sticks,
vector<bool>& used) {
if (target_length == 0) { return true; }
if (target_length < 0) { return false; }
// if (sorted_cut_sticks.empty()) { throw logic_error("Invalid target_times"); }
// if (sorted_cut_sticks.empty()) { return true; }
// if (sorted_cut_sticks.back() > target_length) { return false; }
// while (!sorted_cut_sticks.empty() && sorted_cut_sticks.back() >= target_length) {
// sorted_cut_sticks.pop_back(); // 直接用
// }
// if (sorted_cut_sticks.empty()) { return true; }
const int remaining_length = target_length;
for (int i = static_cast<int>(sorted_cut_sticks.size()) - 1; i >= 0; i--) {
if (sorted_cut_sticks[i] > target_length) { return false; }
size_t global_right_idx,
const size_t right_idx,
vector<bool>& used
) {
// clog << "verify_length_dfs(" << target_length << ") " << remaining_length << " " << remaining_sticks << "x " <<
// global_right_idx << "->" << right_idx << endl;
// log_vector(used, "used: ");
if (remaining_sticks == 0) { return true; }
bool result = false;
for (int i = static_cast<int>(right_idx); i >= 0; i--) {
if (target_length < sorted_cut_sticks[i]) { return false; }
if (used[i]) { continue; }
if (sorted_cut_sticks[i] > remaining_length) { continue; }
if (remaining_length < sorted_cut_sticks[i]) { continue; }
used[i] = true;
if (verify_length_dfs(sorted_cut_sticks, remaining_length - sorted_cut_sticks[i], remaining_sticks, used)) {
return true;
if (remaining_length == sorted_cut_sticks[i]) { // 下一根
result = verify_length_dfs(sorted_cut_sticks, target_length, target_length, original_sticks,
remaining_sticks - 1, global_right_idx,
global_right_idx, used);
} else { // 这一根
if (remaining_length == target_length) { // 这一根的第一节
global_right_idx = i;
}
result = verify_length_dfs(sorted_cut_sticks, target_length,
remaining_length - sorted_cut_sticks[i], original_sticks,
remaining_sticks, global_right_idx, i, used);
}
if (result) { return true; }
used[i] = false;
// 剪枝
while (i >= 0 && sorted_cut_sticks[i] == sorted_cut_sticks[i - 1]) { i--; } // 剪枝
// if (remaining_sticks == original_sticks && remaining_length == target_length) { return false; } // 剪枝
if (remaining_length == target_length) { return false; } // 剪枝
}
return false;
}
/// 需要保证 sorted_cut_sticks 有序;需要保证 target_length * remaining_sticks = 总长度
bool verify_length_dfs(const vector<int>& sorted_cut_sticks,
const int target_length,
const int remaining_sticks) {
vector<bool> used(sorted_cut_sticks.size(), false);
bool result = verify_length_dfs(sorted_cut_sticks, target_length, remaining_sticks, used);
log_vector(used, "used: ");
bool result = verify_length_dfs(sorted_cut_sticks, target_length, target_length, remaining_sticks,
remaining_sticks, sorted_cut_sticks.size() - 1, sorted_cut_sticks.size() - 1, used);
// log_vector(used, "used: ");
return result;
}