d

· · 个人记录

include <iostream>

include <algorithm>

include <cstring>

using namespace std;

const int MAXN = 70; int a[MAXN]; // 存储每根小木棍的长度 bool used[MAXN]; // 标记木棍是否被使用 int n, sum, max_len, L; // n:木棍总数 sum:总长度 max_len:最长小段长度 L:当前枚举的原始木棍长度

// 降序排序,优先尝试长木棍,加速剪枝 bool cmp(int x, int y) { return x > y; }

/**

int main() { cin >> n; sum = 0; max_len = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; sum += a[i]; max_len = max(max_len, a[i]); } // 降序排序,优先放长木棍,加速剪枝 sort(a, a + n, cmp);

// 枚举原始木棍的可能长度L,从最长小段开始,到总长度结束
for (L = max_len; L <= sum; ++L) {
    // 总长度必须能被L整除,否则不可能拼接成功
    if (sum % L != 0) {
        continue;
    }
    // 初始化使用标记
    memset(used, 0, sizeof(used));
    // 开始DFS搜索,找到第一个可行的L就是最小解
    if (dfs(0, 0, 0)) {
        cout << L << endl;
        return 0;
    }
}
return 0;

}