HDU 4430 Yukari\'s Birthday
2016-04-21 19:03
295 查看
Yukari's Birthday
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3989 Accepted Submission(s): 911
[align=left]Problem Description[/align]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.
[align=left]Input[/align]There are about 10,000 test cases. Process to the end of file.
Each test consists of only an integer 18 ≤ n ≤ 1012.
[align=left]Output[/align]For each test case, output r and k.
[align=left]Sample Input[/align]18
111
1111
[align=left]Sample Output[/align]1 17
2 10
3 10 题目大意就是给出数n 求满足条件的r,k 使得 k^1+k^2+k^3.......k^i(1<=i<=n)=n 或n-1 且在k*r最小.思路:我们可以估计r最大不超过40 因为2^40>10^12枚举r 通过r求k的大致范围 二分k代码如下:
#include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <iomanip> #include <list> #include <deque> #include <stack> #define ull unsigned long long #define ll long long #define mod 90001 #define INF 0x3f3f3f3f #define maxn 10000+10 #define cle(a) memset(a,0,sizeof(a)) const ull inf = 1LL << 62; const double eps=1e-5; using namespace std; bool cmp(int a,int b){ return a>b; } ll sum; bool judge(ll x,ll k,ll n){ sum=0; ll temp=1; for(int i=1;i<=x;i++){ temp*=k; sum+=temp; if(sum>n)return true; } return false; } ll check(ll x,ll n){ ll l=2,r=(ll)pow(n,1.0/x);//二分k,枚举k r = (LL)pow(n, 1.0/level); while(l<=r){ ll mid=(l+r)/2; if(judge(x,mid,n)){//分大了 r=mid-1; } else{ if(sum==n){ return mid; } else l=mid+1; } } return 0; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); ll n; while(scanf("%I64d",&n)!=EOF){ ll ans=inf; ll a,b; ll R,K; for(ll r=1;r<=45;r++){ a=check(r,n); b=check(r,n-1); if(a&&a*r<ans)ans=a*r,K=a,R=r; if(b&&b*r<ans)ans=b*r,K=b,R=r; } printf("%I64d %I64d\n",R,K); } return 0; }
相关文章推荐
- 快速幂
- HDU 5360 Hiking
- POJ 3273 Monthly Expense
- POJ 2411 Mondriaan\'s Dream
- POJ 1185 炮兵阵地
- HDU 5339 Untitled
- POJ 3254 Corn Fields
- HDU 5319 Painter
- HBase笔记:Region拆分策略
- HDU 5328 Problem Killer
- HDU 5326 Work
- BestCoder Round #47 ($) HDU 5280 Senior\'s Array
- 记录统计编程的插件 wakatime
- Kendo Grid MVC Hierarchy with Aggregate
- Python 高亮设置(转)
- A. Find Color
- A. Towers
- A. Extra-terrestrial Intelligence
- Mongoose使用操作
- A. Shell Game