SPOJ Use of Function Arctan

F - Use of Function Arctan
Time Limit:2000MS     Memory Limit:1572864KB     64bit IO Format:%lld & %llu

Description

It's easy to know that arctan(1/2)+arctan(1/3)=arctan(1).The problem is,to some fixed number A,you have to write a program to calculate the minimum sum B+C.A,B and C are all positive integers and satisfy the equation below:

arctan(1/A)=arctan(1/B)+arctan(1/C)

Input

The first line contains a integer number T.T lines follow,each contains a single integer A, 1<=A<=60000.

Output

T lines,each contains a single integer which denotes to the minimum sum B+C.

Sample Input

1
1

Sample Output

5

Hint

Some new test data has been added on Feb.15, 2009, 36 users lose their Accepted.

Source limit:    256B
Languages   :    All except: C99 strict ERL JS


題意:求滿足等式的最小的B+C的值。

可以等式兩邊同時求tan值,

arctan(1/A)=arctan(1/B)+arctan(1/C) ,則左右同時取tan的值,便將等式還原爲 1/A=(1/B+1/C) / (1-1/B*1/C)  
繼續化簡爲A=(B*C-1)/(B+C) 。 設 B=A+m  ,  C=A+n帶入上面的表達式化簡得到m*n=A*A+1, 而要求的是B+C=2*A+m+n的
最小值,知m+n>=2*sqrt(m*n)=2*sqrt(A*A+1),要取得‘=’號需m=n=sqrt(A*A+1),因A,B,C都爲 整數,所以m,n也必須爲整數
此時需要的是枚舉m=sqrt(A*A+1)到1,得到第一個能整除sqrt(A*A+1)的m便是需要的值。
這題代碼長度限制的很嚴。
#include <stdio.h>
#include <cmath>
int main()
{int T;long long a,b,c,n,m;scanf("%d",&T);
    while(T--)
    {scanf("%lld",&a);long long t=(long long)sqrt((a*a+1)*1.0);while((a*a+1)%t)t--;printf("%lld\n",2*a+t+(a*a+1)/t);
    }
    return 0;
}








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章