HDU 4430 Yukari's Birthday, hduyukari
Given n, Let n meet the following expression: k ^ 1 + k ^ 2 +... + k ^ r = n. and r * k is the smallest. (Ps: And it's optional to place at most one candle at the center of the cake. So k ^ 0, that is, 1 is dispensable. But this is not a circle, so when n = 30 and n = 31, their r is equal) for example: 2 ^ 1 + 2 ^ 2 + 2 ^ 3 + 2 ^ 4 = 30. n = 30, r = 4, k = 2. (When n = 31, r is 4, and k is 2) so there is a general solution r = 1, k = n-1. this is true for each n. However, we have to minimize the r * k, so we have to traverse it all.
Because 2 ^ 0 + 2 ^ 1 + 2 ^ 2 +... + 2 ^ 40 = 2 ^ 41-1> 10 ^ 12. so 1 <= r <= 40. r is not very violent. K.
# Include <iostream> # include <cstdio> # include <cmath> using namespace std; const long inf = 1000000000001; int main () {// freopen ("in.txt ", "r", stdin); long n; while (scanf ("% I64d", & n )! = EOF) {int min_r; long min_k, min_v = inf; for (int r = 1; r <= 40; r ++) {long low = 1, high = n, k; while (1) {k = (low + high)/2; long sum = 0; bool flag = true; for (int I = 1; I <= r; I ++) if (pow (k * 1.0, I)> n) // if it is interpreted here, check whether k ^ I is greater than n. if it is greater, no need to sum {sum = n + 1; flag = false; break;} if (flag) for (int I = 1; I <= r & sum <= n; I ++) sum + = (long) pow (k * 1.0, I ); if (sum = n | sum = N-1) // if (r * k <min_v) {min_r = r; min_k = k; min_v = r * k; // Save the minimum product} if (sum> n) high = k; else low = k; if (high-low = 1) break ;}} printf ("% d % I64d \ n", min_r, min_k);} return 0 ;}