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
。。。
题目里有用的公式就只有(1/a + 1/b) / (1 - 1/ab),经推倒可得(c + b)/ ( bc - 1 ) = 1/ a,推倒可得(b - a)(c - a) = a * a + 1, 由此可得(b - c)与(c - a) 是a*a+1的因子,于是我们遍历a*a+1的所有因子,取里面b+c最大的即可。
/*
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
. ' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
.............................................
*/
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define pi 3.1415926
#define INF 123456789
using namespace std;
int main(){
long long n, i, q, a, j, Min, sum;
while(~scanf("%lld", &a)){
q = a * a + 1;
Min = 999999999999;
for(i = 1; i <= a + 1; i ++){
if(q % i == 0){
sum = q / i + i + 2 * a;
if(sum < Min) Min = sum;
}
}
cout << Min << endl;
}
return 0;
}
/*
1 represents B, F, P, or V
2 represents C, G, J, K, Q, S, X, or Z
3 represents D or T
4 represents L
5 represents M or N
6 represents R
*/