hdu 4430 Yukari's Birthday
Yukari's Birthday
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1665 Accepted Submission(s): 341
Problem Description
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 place n 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 like r ≥ 1 concentric circles. They place ki candles equidistantly on the i-th circle, where k ≥ 2, 1 ≤ i ≤ r. 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 of r and k satisfying these restrictions, they want to minimize r × k. If there is still a tie, minimize r.
To make the birthday cake look more beautiful, Ran and Chen decide to place them like r ≥ 1 concentric circles. They place ki candles equidistantly on the i-th circle, where k ≥ 2, 1 ≤ i ≤ r. 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 of r and k satisfying these restrictions, they want to minimize r × k. If there is still a tie, minimize r.
Input
There are about 10,000 test cases. Process to the end of file.
Each test consists of only an integer 18 ≤ n ≤ 1012.
Each test consists of only an integer 18 ≤ n ≤ 1012.
Output
For each test case, output r and k.
Sample Input
18 111 1111
Sample Output
1 17 2 10 3 10
// 因为 2^64-1 差不多是 10^18 所以 1+k^1+k^2+k^3+..+k^r == n ,r 不会很大 //所以我们可以枚举 r 接下来就是 二分查找 k 是不是存在 // 我们在计算 1+k^1+k^2+k^3+..+k^r 时 不要使用 等比公式 ,因为会超 long long // 我们可以一项项计算 1+k^1+k^2+k^3+..+k^r ; 在计算 k ^ i 时 要判断 是不是超n // 判断 k^i = k^(i-1) * k > n 所以 改为除法 k^(i-1) > n / k #include<iostream> #include<cstdio> #include<vector> #include<cstring> #include<cmath> #pragma comment(linker,"/STACK:1024000000,1024000000") using namespace std ; #define LL long long
LL n , kk , rr , m ; LL pow1( LL k , int r ) { LL ans = 1 , sum = 0 ; for( int i = 1 ; i <= r ;i++ ){ if( ans > n / k ) return n+1 ; ans *= k ; if( sum > n-ans ) return n+1 ; sum += ans ; } return sum ; } void dfs( int r ) { LL L , R , mid , sum ; L = 2 ; R = n-1 ; while( R >= L ) { mid = (R+L)/2 ; sum = pow1(mid,r) ; if( sum == n || sum == n-1 ) break ; if( sum > n ) R = mid-1 ; else L = mid+1 ; } LL k = mid ; if( L > R ) return ; if( sum == n || sum == n-1 ) { if(k*r < rr*kk) { rr = r ; kk = k ; } return ; } } int main() { int i ; while( scanf("%I64d" , &n ) != EOF ) { m = (LL)sqrt(n*1.0) ; kk = n-1 ; rr = 1 ; for( int j = 2 ; j <= 47 ;j++ ) dfs(j) ; // hdu 用 lld 会wa .... printf("%I64d %I64d\n" ,rr , kk ) ; } }