From d87e2aabef413533393ec0268113c2c501beb0db Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 3 Sep 2022 17:15:50 +0800 Subject: [PATCH] =?UTF-8?q?UVA307=20=E5=B0=8F=E6=9C=A8=E6=A3=8D=20Sticks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/85779684 --- Luogu/UVA307/UVA307.cpp | 89 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 Luogu/UVA307/UVA307.cpp diff --git a/Luogu/UVA307/UVA307.cpp b/Luogu/UVA307/UVA307.cpp new file mode 100644 index 00000000..6a1a6cd4 --- /dev/null +++ b/Luogu/UVA307/UVA307.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 70; + +int n, m, l, a[N], next[N]; +bool flag, vis[N]; + +void dfs(int now, int last, int rest) { + if (!rest) { + if (now == m) { + flag = true; + + return; + } + + for (int i = 1; i <= n; i++) { + if (!vis[i]) { + vis[i] = true; + dfs(now + 1, i, l - a[i]); + if (flag) return; + vis[i] = false; + + break; + } + } + } + + int p = std::lower_bound(a + last + 1, a + n + 1, rest, std::greater()) - a; + + for (int i = p; i <= n; i++) { + if (!vis[i]) { + vis[i] = true; + dfs(now, i, rest - a[i]); + vis[i] = false; + + if (flag || rest == a[i] || rest == l) return; + i = next[i]; + } + } +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + while (cin >> n, n) { + flag = false; + std::fill_n(a, N, 0); + std::fill_n(next, N, 0); + std::fill_n(vis, N, false); + vis[1] = true; + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + } + + int sum = std::accumulate(a + 1, a + 1 + n, 0); + + std::sort(a + 1, a + 1 + n, std::greater()); + + for (int i = n; i; i--) { + next[i] = a[i] == a[i + 1] ? next[i + 1] : i; + } + + for (l = a[1]; l <= sum / 2; l++) { + if (sum % l) continue; + + m = sum / l; + dfs(1, 1, l - a[1]); + + if (flag) { + cout << l << endl; + + break; + } + } + + if (!flag) cout << sum << endl; + } + + return 0; +}