Hdu4430: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4430

Question: give you a second, ask for K, R, K, R, and K ^ 1 + k ^ 2 + ..... + k ^ r = S | s-1, and K * r is the smallest. in multiple cases, r is the smallest.

Question: I don't know how to do it at the beginning. Let's say it's a second point. Then start to think over binary.

Thinking process: at the beginning, we thought about how to divide the two points. First, we had to find a linear relationship between the two points. After looking for a long time, we didn't find any linear relationship, so we didn't know how to divide the two points, I don't know what the binary variable is. After thinking for a long time, we can see that K * r is the smallest, k is the base number, and r is the exponent. So when r is the largest, k is the smallest, and K * r is the smallest, because K is exponential growth, when R is large, K can reach s as long as it is small. when K = 2, R can reach s as long as 40. So you can enumerate R and then find K. In this way, we will gradually think that, for a fixed R, the larger the K, the more the sub-side and the increasing, so the linear relationship comes out. We can divide K, and the initial K value is S | s-1, so we can do this in two cases. However, here we have to deal with some benefits. When calculating the sub-vertex, if the base number is greater than 1e7, other values will also work. As long as it is not smaller than 1e6, it will jump out directly without computing. Specifically, there are comments in the code.

1 # include <iostream> 2 # include <cstring> 3 # include <cstdio> 4 # include <algorithm> 5 using namespace STD; 6 long n; 7 long K, ans1, lastr, lastk, L, mid, R; 8 bool judge (long s, int num, long flag) {9 long ans = 0, temp = s; 10 if (S> 10000000) return true; // This is a special sentence, which means long. Because if this number is greater than 1e7, her square must be greater than N (considering N range) 11 For (INT I = 1; I <= num; I ++) {12 ans + = temp; 13 temp * = s; 14 if (ANS> = Flag) return true; // This is also true. Prevent overflow 15} 16 return false; 17} 18 bool judge2 (long s, int num) {// determine whether this number is the number to be searched 19 long ans = 0, temp = s; 20 if (S> 10000000) return false; 21 for (INT I = 1; I <= num; I ++) {22 ans + = temp; 23 temp * = s; 24 if (ANS> N) return false; 25} 26 if (ANS = n | ans = N-1) return true; 27 return false; 28} 29 int main () {30 31 While (~ Scanf ("% i64d", & N) {32 ans1 = n-1; lastr = 1; lastk = n-1; 33 for (INT I = 2; I <= 40; I ++) {34 L = 1, R = N; 35 while (L <r) {// The result of binary is either the number = N is found, either Exit 36 mid = (L + r)/2 cyclically; 37 If (Judge (MID, I, n) 38 R = mid; 39 else40 L = Mid + 1; 41} 42 if (judge2 (L, I) & ans1> I * l) {// check whether the number of = N is found. 43 ans1 = I * l; 44 lastr = I; 45 lastk = L; 46} 47 L = 1, R = n-1; 48 While (L <r) {// either find the number = n-1, either exit loop 49 mid = (L + r)/2; 50 if (Judge (MID, I, n-1) 51 r = mid; 52 else53 L = Mid + 1; 54} 55 if (judge2 (L, I) & ans1> I * l) {// same as 56 ans1 = I * l; 57 lastr = I; 58 lastk = L; 59} 60} 61 printf ("% i64d % i64d \ n", lastr, lastk); 62} 63}

View code