Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5505 Accepted Submission(s): 1320
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
#include<cstdio>
#include <cmath>
typedef __int64 LL;
LL int_pow(const LL x,const int n)
{
LL ans=1;
for(int i=1;i<=n;i++) ans*=x;
return ans;
}
LL find_k(int r,LL n)
{
LL st=2,ed=pow(n,1.0/r),mid,sum; //注意上界取到n的r次方就夠了,多了會溢出
while(st<=ed)
{
mid=st+(ed-st)/2;
sum=mid*(1-int_pow(mid,r))/(1-mid); //計算r層蠟燭加起來總共多少根
if(sum > n) ed=mid-1;
else if(sum < n) st=mid+1;
else return mid;
}
return 0;
}
int main()
{
LL n,r,k;
while(scanf("%lld",&n)!=EOF)
{
r=1,k=n-1;
for(int i=2;i<=50;i++) //對於接下來的每層,嘗試是否能更新r、k
{
LL k1=find_k(i,n) , k2=find_k(i,n-1); //得到中心不插蠟燭的k,以及中間插一根蠟燭的k
if(k1 != 0){ //若是對i存在有中心不插蠟燭的k,嘗試更新r,k
if(i * k1 == r * k && i < r) {
r = i; k = k1;
}
else if(i * k1 < r * k){
r = i; k = k1;
}
}
if(k2 != 0){ //若是對i存在有中心插蠟燭的k,嘗試更新r,k
if(i * k2 == r * k && i < r) {
r = i; k = k2;
}
else if(i * k2 < r * k){
r = i; k = k2;
}
}
}
printf("%I64d %I64d\n",r,k);
}
}