[UOJ 12]猜数

Description

Input

Output

Sample Input1

1
1 4

Sample Output1

4 5

Sample Explanation1

Sample Input2

1
2 8

Sample Output2

8 10

HINT

题解

此题巨坑无比。不要用$unsigned/cin$。

其次$a,b$是$g$的倍数的意思是“对于输入的$g$,$a,b$一定要是$g$的倍数”,而不是“对于所有$a,b$,输入的$g$满足$a,b$是$g$的倍数”(这种理解其实直接就是$g=1$,而输入并不保证$g=1$)。

我们有$n=l×g$。那么我们分开来讨论:

对于最小值,由均值不等式:$a+b≥2\sqrt{ab}=2\sqrt{n}=2\sqrt{lg}$(当且仅当$a=b$取等)。由于$a,b≥g$,显然可以取等,满足。

对于最大值,由于$a+b≥2\sqrt{lg}$,我们记$f(x)=a+b={n\over b}+b$,显然在$(0,\sqrt n]$是单调递减的。由于$b≥g$,故在$b=g$处取最大值。

综上最小值为$2\sqrt{lg}$,最大值为$l+g$。

精度问题
有人可能会写:

ans_min = (long long)sqrt((double)g × l);

这样会被卡精度,因为$double$大概只有$15$位$10$进制有效数字。只能得到$60$分。
解决方法是:

ans_min = (long long)sqrt(l / g) × g;

当然有人可能直接$long double$保平安了……

 1 #include<set>
 2 #include<map>
 3 #include<ctime>
 4 #include<cmath>
 5 #include<queue>
 6 #include<stack>
 7 #include<cstdio>
 8 #include<string>
 9 #include<vector>
10 #include<cstring>
11 #include<cstdlib>
12 #include<iostream>
13 #include<algorithm>
14 #define LL long long
15 #define RE register
16 #define IL inline
17 using namespace std;
18 
19 LL a,b;
20 int t;
21 
22 int main()
23 {
24     cin>>t;
25     while (t--)
26         {scanf("%lld%lld",&a,&b);
27             printf("%lld %lld\n",2*(LL)sqrt(b/a)*a,a+b);}
28     return 0;
29 }

 

posted @ 2017-07-31 20:21  NaVi_Awson  阅读(269)  评论(0)    收藏  举报