Went the wrong direction during the contest. :disappointed:
Solution #
Let the number of edges of the polygons be . It’s easy to find that has to be a multiple of , thus we can rewrite as . Hence if we know the number of edges of the first polygon, all we left if to find the longest sequence such that is a multiple if and .
Note that are all multiple of , so if we divide them by we get a sequence starting with again! This means we get a smaller subproblem and we can use dynamic programming to solve it: let be the length of the longest such sequence described above which sums to . Since we can get a longer sequence by multiplying a shorter one by a constant and prepending a , so the transition is:
Code #
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt = 1;
cin >> tt;
constexpr int N = 1e6;
vector<int> dp(N + 1, 1);
for (int i = 1; i <= N; i++) {
for (int j = 2 * i + 1; j <= N; j += i) {
dp[j] = max(dp[j], dp[i] + 1);
}
}
for (int cas = 1; cas <= tt; cas++) {
int x;
cin >> x;
int ans = 1;
cout << "Case #" << cas << ": ";
for (int f = 3; f <= x; f++) {
if (x % f == 0) ans = max(ans, dp[x / f]);
}
cout << ans << '\n';
}
return 0;
}