hdu 4342 History repeat itself 模拟题

本文介绍了一个算法问题,即求解第N个非平方数及其相关公式的计算方法。通过对输入N的处理,利用平方根函数找到非平方数,并通过一段段计算给出公式结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:求从1开始的第n个非平方数以及图中公式

Historyrepeatitself

TimeLimit:2000/1000MS(Java/Others)MemoryLimit:32768/32768K(Java/Others)
TotalSubmission(s):875AcceptedSubmission(s):313

ProblemDescription

TomtooktheDiscreteMathematicscourseinthe2011,buthisbadattendanceangeredProfessorLeewhoisinchargeofthecourse.Therefore,ProfessorLeedecidedtoletTomfaceahardprobabilityproblem,andannouncedthatifhefailtoslovetheproblemtherewouldbenowayforTomtopassthefinalexam.
Asaresult,Tompassed.
Historyrepeatitself.You,thebadboy,alsoangeredtheProfessorLeewhenSeptemberEnds.Youhavetofacedtheproblemtoo.
TheproblemcomesthatYoumustfindtheN-thpositivenon-squarenumberMandprintedit.Andthat'sfornormalbadstudent,suchasTom.Buttherealbadstudenthastocalculatetheformulabelow.


So,thatyoucanreallyunderstandWHATABADSTUDENTYOUARE!!

Input

Thereisanumber(T)inthefirstline,tellyouthenumberoftestcasesbelow.ForthenextTlines,thereisjustonenumberontheeachlinewhichtellyoutheNofthecase.
Tosimplifiedtheproblem,TheNwillbewithin231andmorethen0.

Output

Foreachtestcase,printtheN-thnonsquarenumberandtheresultoftheformula.

SampleInput

4

1

3

6

10

SampleOutput

22

57

813

1328

/*假如让求第n个非平方数的话,看n前面有多少个平方数,假设有x个,则第n个非平方数就是n+x
则有 sqrt(n+x)=x  x>根号n     之后暴力   
对于第二个式子 由于下取整,所以每两个平方数之间的数的sqrt(i),都等于前面的那个平方数,
 这样就很好计算了,一段一段的算
*/
#include <stdio.h>
#include <math.h>
int main()
{
	__int64 i,m,n,tt;
	__int64 x;
	__int64 s;
	while(scanf("%I64d",&m)!=EOF)
	{
		while(m--)
		{
			scanf("%I64d",&n);
			
			i=(__int64)sqrt(n*1.0);
			while((__int64)sqrt((n+i)*1.0)!=i)//这里必须要乘1.0 否则编译错误
			{
				i++;
			}
			x=n+i;
			n=n+i;
			tt=(__int64)(sqrt(x*1.0));
			s=0;
			for(i=2;i<=tt;i++)
			{
				s=s+(i*i-(i-1)*(i-1))*(i-1);//从i*i到 (i-1)*(i-1)的数  开方后向下取整 均为i-1
			}
			s=s+(x-tt*tt+1)*tt;
			printf("%I64d %I64d\n",n,s);
		}
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值