hdu 4430 Yukari's Birthday 枚举+二分

时间:2023-03-08 17:59:19

注意会超long long

开i次根号方法,te=(ll)pow(n,1.0/i);

Yukari's Birthday

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3262    Accepted Submission(s): 695

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.
Input
There are about 10,000 test cases. Process to the end of file. 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
Source
 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<iostream>
#define maxi(a,b) (a)>(b)?(a):(b)
#define mini(a,b) (a)<(b)?(a):(b)
#define N 1000005
#define mod 10000
#define ll long long using namespace std; ll n;
ll ans;
ll r,k;
ll rr,kk; void ini()
{
ans=n-;
r=;
k=n-;
} void cal3(ll x)
{
ll te,d;
te=+*x;
d=sqrt(te);
if(d*d==te){
kk=(d-);
if(kk%==){
kk/=;
if(kk*<ans){
ans=kk*;
k=kk;r=;
}
}
}
} ll cal(ll a,ll cnt)
{
ll re=;
while(cnt)
{
if(cnt&){
re=(re*a);
cnt--;
}
cnt/=;
a=a*a;
}
return re;
} int cal2(ll a,ll en,ll now)
{
ll i;
for(i=en;i>=;i--){
now+=cal(a,i);
if(now>n) return ;
}
if(now<n-) return ;
if(now==n- || now==n){
if(a*(en+)<ans){
ans=a*(en+);
r=en+;
k=a;
}
else{
if(a*(en+)==ans && (en+)<r){
r=en+;
k=a;
}
}
}
return ;
} void solve()
{
ll te,temp;
cal3(n);
cal3(n-);
ll i;
for(i=;i<=;i++){
//temp=cal(2ll,i);
//if(temp>n) break;
te=(ll)pow(n,1.0/i);
for(kk=te;kk>=;kk--){
temp=cal(kk,i);
if(temp>n) continue;
if(cal2(kk,i-,temp)==){
break;
}
}
}
} void out()
{
printf("%I64d %I64d\n",r,k);
} int main()
{
//freopen("data.in","r",stdin);
//scanf("%d",&T);
// while(T--)
while(scanf("%I64d",&n)!=EOF)
{
ini();
solve();
out();
}
return ;
}