SPOJ :Use of Function Arctan

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


//分析计算过程: 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便是需要的值。时间复杂度为 O(n)。此题代码长度
限制很变态。
#include<stdio.h>
#include<math.h>
int main(){
	long long t,a,i;
	scanf("%lld",&t);
	while(t--){
		scanf("%lld",&a);
		long long i=(long long)sqrt(1.0*(a*a+1));
		while((a*a+1)%i) i--;
		printf("%lld\n",2*a+i+(a*a+1)/i);
	} 
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值