POJ 1183 反正切函数的应用 (推公式)

反正切函数的应用

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 14468   Accepted: 5227

Description

反正切函数可展开成无穷级数,有如下公式 

(其中0 <= x <= 1) 公式(1) 

使用反正切函数计算PI是一种常用的方法。例如,最简单的计算PI的方法: 

PI=4arctan(1)=4(1-1/3+1/5-1/7+1/9-1/11+...) 公式(2) 

然而,这种方法的效率很低,但我们可以根据角度和的正切函数公式: 

tan(a+b)=[tan(a)+tan(b)]/[1-tan(a)*tan(b)] 公式(3) 

通过简单的变换得到: 

arctan(p)+arctan(q)=arctan[(p+q)/(1-pq)] 公式(4) 

利用这个公式,令p=1/2,q=1/3,则(p+q)/(1-pq)=1,有 

arctan(1/2)+arctan(1/3)=arctan[(1/2+1/3)/(1-1/2*1/3)]=arctan(1) 

使用1/2和1/3的反正切来计算arctan(1),速度就快多了。 
我们将公式(4)写成如下形式 

arctan(1/a)=arctan(1/b)+arctan(1/c) 

其中a,b和c均为正整数。 

我们的问题是:对于每一个给定的a(1 <= a <= 60000),求b+c的值。我们保证对于任意的a都存在整数解。如果有多个解,要求你给出b+c最小的解。 

Input

输入文件中只有一个正整数a,其中 1 <= a <= 60000。

Output

输出文件中只有一个整数,为 b+c 的值。

Sample Input

1

Sample Output

5

Source

 
一道推公式的题目,注意数据范围

1/a = (1/b + 1/c)/ (1 - 1/(b*c)) => bc-1 = a(b+c) assume b=a+m and c=a+n (b and c is always bigger than a) (a+m)(a+n)-1=a(a+m+a+n) => a*a+a*n+a*m+m*n-1=2*a*a+m*a+n*a => m*n=a*a+1

再枚举m(或者n)即可

关键是b=a+m,c=a+n这里的一个转换

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<cstring>
 4 #include<stdlib.h>
 5 #include<algorithm>
 6 #define LL __int64
 7 using namespace std;
 8 int main()
 9 {
10     //freopen("in.txt","r",stdin);
11     LL a;
12     scanf("%I64d",&a);
13     for(LL i=a;i>=0;i--)//枚举m
14     {
15         if((a*a+1)%i==0)//n=(a*a+1)/i   m=i
16         {
17             printf("%I64d
",a+i+a+(a*a+1)/i);
18             break;
19         }
20     }
21     return 0;
22 }
View Code
原文地址:https://www.cnblogs.com/clliff/p/3932970.html