TOJ 4373 HDU 4430 ZOJ 3665 Yukari's Birthday / 二分

来源:互联网 发布:2016年7月份淘宝新规 编辑:程序博客网 时间:2024/06/03 09:25

Yukari's Birthday

时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte

描述

Today is Yukari's n-th birthday. Ran and Chen hold a celebration party for her. Now comes the most important part, birthday cake! But it's a big challenge for them to placen candles on the top of the cake. As Yukari has lived for such a long long time. Though she herself insists that she is a 17-year-old girl.

To make the birthday cake look more beautiful, Ran and Chen decide to place them liker ≥ 1 concentric circles. They placeki candles equidistantly on thei-th circle, wherek ≥ 2, 1 ≤ ir. And it's optional to place at most one candle at the center of the cake. In case that there are a lot of different pairs ofr andk satisfying these restrictions, they want to minimizer × k. If there is still a tie, minimize r.

输入

There are about 10,000 test cases. Process to the end of file.

Each test consists of only an integer 18 ≤ n ≤ 1012.

输出

For each test case, output r and k.

样例输入

181111111

样例输出

1 172 103 10

假设k最小是2 那么r最大45 2的45次超过了10的12次

然后枚举r 二分k

二分时左值l明显是从2 r的话我从100w开始 因为100w的平方就大于等于n最大

对于二分的左右范围不是很会判断

烂代码

#include <stdio.h>const long long  MAX = 1000010;long long n;long long check(long long k,long long r){long long res = 0,i,m = k;for(i = 1;i <= r; i++){res += m;//printf("%I64d\n",res);m *= k;if(res > n)return MAX * MAX;}return res;}long long erfen(long long l,long long r,long long k){if(k == 1)return n - 1;while(l <= r){long long m = (l + r) >> 1;long long res = check(m,k);//printf("%I64d\n",res);if(res == n || res == n - 1)return m;if(res > n)r = m - 1;elsel = m + 1;}return -1;}int main(){long long l,r,i,R,K,RR,KK;while(scanf("%lld",&n)!=EOF){RR = MAX;KK = MAX;for(i = 1;i <= 40; i++){l = 2;r = 1000000;long long res = erfen(l,r,i);if(res != -1){if(res * i < RR * KK || res * i == RR * KK && i < RR){RR = i;KK = res;}}}printf("%lld %lld\n",RR,KK);}return 0;}