uva 107 The Cat in the Hat

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=43 题意:其实就是给了两个式子。。。(N+1)^h=a,N^h=b,a,b已知,然后求关于N的两个式子.。。 思路:数学上这个方程貌似不可解。。? 所以只能枚举一下==。。。注意精度问题把。。。

然后用换底公式求对数的时候要向上取整。

还有b为1的时候是特殊数据。

/* ***********************************************
Author :111qqz
Created Time :2016年01月28日 星期四 16时46分38秒
File Name :code/uva/107.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-12;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6int a,b;
 7int main()
 8{
 9	#ifndef  ONLINE_JUDGE 
10	freopen("code/in.txt","r",stdin);
11  #endif
 1	while (scanf("%d %d",&a,&b)!=EOF)
 2	{
 3	    if (a==0&&b==0) break;
 4	    int n ,h;
 5	    int ans1,ans2=0;
 6	    //b为1是特殊数据,除数为0了。。。。
 7	    if (b==1)
 8	    {
 9		h = ceil(log(a)/log(2));
10		cout<<h<<" "<<2*a-1<<endl;
11		continue;
12	    }
 1	    for ( int i = 1 ;  ;i++)
 2	    {
 3		if (fabs(log(a)*log(i)-log(b)*log(i+1))<eps)
 4		{
 5		    n = i;
 6		    break;
 7		}
 8	    }
 9	     h = (ceil)(log(b)/log(n));   //应该向上取整。。。可以看做一般规律。。换地公式求对数的时候。。想想为什么。
10	  //  cout<<"h:"<<h<<endl;
11	     ans1 = (int)((1-b)/(1-n));
12	     ans2=0;
13	    int curh = 1;
14	    for ( int i = 0 ; i <= h ;i++)
15	    {
16	//	cout<<"curh:"<<curh<<" b:"<<b<<endl;
17		ans2+=curh*b;
18		curh*=(n+1);
19		b/=n;
20	    }
21	   // cout<<ans1<<" "<<ans2<<endl;
22	    printf("%d %d\n",ans1,ans2);
	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}