hdoj 4430 Yukari's Birthday

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4430

解题思路:二分

  1 ///////////////////////////////////////////////////////////////////////////
  2 //problem_id: hdoj 4430
  3 //user_id: SCNU20102200088
  4 ///////////////////////////////////////////////////////////////////////////
  5 
  6 #include <algorithm>
  7 #include <iostream>
  8 #include <iterator>
  9 #include <iomanip>
 10 #include <cstring>
 11 #include <cstdlib>
 12 #include <string>
 13 #include <vector>
 14 #include <cstdio>
 15 #include <cctype>
 16 #include <cmath>
 17 #include <queue>
 18 #include <stack>
 19 #include <list>
 20 #include <set>
 21 #include <map>
 22 using namespace std;
 23 
 24 ///////////////////////////////////////////////////////////////////////////
 25 #pragma comment(linker,"/STACK:1024000000,1024000000")
 26 
 27 #define lson l,m,rt<<1
 28 #define rson m+1,r,rt<<1|1
 29 ///////////////////////////////////////////////////////////////////////////
 30 
 31 ///////////////////////////////////////////////////////////////////////////
 32 const double EPS=1e-8;
 33 const double PI=acos(-1.0);
 34 
 35 const int x4[]={-1,0,1,0};
 36 const int y4[]={0,1,0,-1};
 37 const int x8[]={-1,-1,0,1,1,1,0,-1};
 38 const int y8[]={0,1,1,1,0,-1,-1,-1};
 39 ///////////////////////////////////////////////////////////////////////////
 40 
 41 ///////////////////////////////////////////////////////////////////////////
 42 typedef long long LL;
 43 
 44 typedef int T;
 45 T max(T a,T b){ return a>b? a:b; }
 46 T min(T a,T b){ return a<b? a:b; }
 47 T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
 48 T lcm(T a,T b){ return a/gcd(a,b)*b; }
 49 ///////////////////////////////////////////////////////////////////////////
 50 
 51 ///////////////////////////////////////////////////////////////////////////
 52 //Add Code:
 53 LL power(LL a,LL n){
 54     LL ret=1;
 55     while(n){
 56         if(n&1) ret*=a;
 57         a*=a;
 58         n>>=1;
 59     }
 60     return ret;
 61 }
 62 
 63 LL cal(LL r,LL k){
 64     return (power(k,r+1)-k)/(k-1);
 65 }
 66 
 67 LL erfen(LL r,LL n){
 68     LL L=2,R=(LL)pow(n,1.0/r);
 69     while(R>=L){
 70         LL M=(L+R)/2,temp=cal(r,M);
 71         if(temp==n || temp==n-1) return M;
 72         if(temp>n) R=M-1;
 73         else L=M+1;
 74     }
 75     return -1;
 76 }
 77 ///////////////////////////////////////////////////////////////////////////
 78 
 79 int main(){
 80     ///////////////////////////////////////////////////////////////////////
 81     //Add Code:
 82     LL n;
 83     while(scanf("%I64d",&n)!=EOF){
 84         LL r=1,k=n-1;
 85         for(LL i=2;i<=40;i++){
 86             LL j=erfen(i,n);
 87             if(j!=-1 && i*j<r*k){
 88                 r=i;
 89                 k=j;
 90             }
 91         }
 92         printf("%I64d %I64d\n",r,k);
 93     }
 94     ///////////////////////////////////////////////////////////////////////
 95     return 0;
 96 }
 97 
 98 ///////////////////////////////////////////////////////////////////////////
 99 /*
100 Testcase:
101 Input:
102 18
103 111
104 1111
105 Output:
106 1 17
107 2 10
108 3 10
109 */
110 ///////////////////////////////////////////////////////////////////////////

posted on 2013-09-13 09:52  SCNU20102200088  阅读(258)  评论(0编辑  收藏  举报

导航