HDU 4430 Yukari & #39; s Birthday, hduyukari

Source: Internet
Author: User

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 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.