joj 1839: Arctan

Result TIME Limit MEMORY Limit Run Times AC Times JUDGE
5s 8192K 262 42 Standard

Arctan function can expand to an infinite progression:

 

People often caculate pi using arctan functions.For example,the easiest way to calculate π is :

 

however,it's too inefficient,but we can use this formula:

 

that is

 

now,let , then ,so

 

It'll be more faster to calculate arctan(1) using and     :

 

a , b and c are positive integers。

The problem is:For a given a(),caculate the value of (b+c)。If there are several solutions,just give the minimum one.

Input

The input contains several positive integers,one per line.

Output

For each a, print the minimum (b+c).

Sample Input

1

Sample Output

5

/*

败在数据类型了,long long会TLE,int 会WA,仔细算了下可能会有50000到60000的a 会使a^2+1是素数,则要一个unsigned型变量

*/

#include <cstdio>
unsigned m,ans,i,a;
int main ()
{
    while(scanf("%u",&a)!=EOF)
    {
        m=a*a+1;
        for (i=a ; i>0 ; i--)
        {
             if(!(m%i))
              {
                  ans=a*2+i+m/i;break;//倒序搜索第一个就是b+c最小的答案,数学的公式推导很重要
              }
        }
        printf("%u/n",ans );
    }
    return 0;
}

你可能感兴趣的:(joj 1839: Arctan)