题目
题意: 将x进行分解,使得分解后的所有数的乘积达到最大。对于x,可以将其分解为x/2(上取整)和x/2(下取整)
思路: 记忆化搜索。当x<=4,不用分。否则,一直分。
时间复杂度: O(logX) or O(logX * loglogX)
采用unordered_map
假设n = 2 ^ k,T(n) = T(n/2)+1.T(n) = k = logn
假设n != 2^k, 2^k-1 <= n < 2^k,可以证明最多执行2k次f(x),T(n) < 2 * (log(x)+1) = O(log(x)).
若采用map
则记忆化需要花logn的时间,n至多为2(log(x)+1)。logn即上述提到的loglogX时间。
代码:
// Problem: A - Floor, Ceil - Decomposition// Contest: AtCoder - AtCoder Regular Contest 135// URL: https://atcoder.jp/contests/arc135/tasks/arc135_a// Memory Limit: 1024 MB// Time Limit: 2000 ms// // Powered by CP Editor (https://cpeditor.org)#include#include#include#include#include#include#include#include