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的时候是特殊数据。
1/* ***********************************************
2Author :111qqz
3Created Time :2016年01月28日 星期四 16时46分38秒
4File Name :code/uva/107.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-12;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33int a,b;
34int main()
35{
36 #ifndef ONLINE_JUDGE
37 freopen("code/in.txt","r",stdin);
38 #endif
39
40 while (scanf("%d %d",&a,&b)!=EOF)
41 {
42 if (a==0&&b==0) break;
43 int n ,h;
44 int ans1,ans2=0;
45 //b为1是特殊数据,除数为0了。。。。
46 if (b==1)
47 {
48 h = ceil(log(a)/log(2));
49 cout<<h<<" "<<2*a-1<<endl;
50 continue;
51 }
52
53 for ( int i = 1 ; ;i++)
54 {
55 if (fabs(log(a)*log(i)-log(b)*log(i+1))<eps)
56 {
57 n = i;
58 break;
59 }
60 }
61 h = (ceil)(log(b)/log(n)); //应该向上取整。。。可以看做一般规律。。换地公式求对数的时候。。想想为什么。
62 // cout<<"h:"<<h<<endl;
63 ans1 = (int)((1-b)/(1-n));
64 ans2=0;
65 int curh = 1;
66 for ( int i = 0 ; i <= h ;i++)
67 {
68 // cout<<"curh:"<<curh<<" b:"<<b<<endl;
69 ans2+=curh*b;
70 curh*=(n+1);
71 b/=n;
72 }
73 // cout<<ans1<<" "<<ans2<<endl;
74 printf("%d %d\n",ans1,ans2);
75
76
77 }
78
79 #ifndef ONLINE_JUDGE
80 fclose(stdin);
81 #endif
82 return 0;
83}