2012 Multi-University Training Contest 5:History repeat itself

Problem Description
Tom took the Discrete Mathematics course in the 2011,but his bad attendance angered Professor Lee who is 
in  charge of the course. Therefore, Professor Lee decided to let Tom face a hard probability problem, and ann
ounced  that if he fail to slove the problem there would be no way for Tom to pass the final exam.
As a result , Tom passed.History repeat itself. You, the bad boy, also angered the Professor Lee when Septem
ber Ends. You have to faced  the problem too.
The problem comes that You must find the N-th positive non-square number M and printed it. And that's for normal
 bad student, such as Tom. But the real bad student has to calculate the formula below.

So, that you can really understand WHAT A BAD STUDENT YOU ARE!!
 
Input
There is a number (T)in the first line , tell you the number of test cases below. For the next T lines, there is just 
one  number on the each line which tell you the N of the case.
To simplified the problem , The N will be within 2 31 and more then 0.
 
Output
For each test case, print the N-th non square number and the result of the formula.
 
SampleInput
4
1
3
6
10
 
SampleOutput
2 2
5 7
8 13
13 28
 

//题意:给出一个n,求第n个非平方数m,并求出 1到m每个数的根号(取下界)之和。

//分析:1、假设不考虑是否为平方数 ,直接询问第n个数,那么就是n, 而n前面有sqrt(n)个平方数,那么再考虑平方数的情况下在 n前面就少计

算了sqrt(n)个数。  所以第n个平方数就是n+sqrt(n),但我们会发现 n到n+sqrt(n)之间可能也有平方数,那么仍旧计算小了。  写几组数据其实就

可以发现,在计算sqrt(n)取整的时候需要取较近的那个整数点。 所以第n个非平方数为n+[sqrt(n)];

2、找到m,则只需要计算1到m每个数的根号(取下界)之和。 从1一直到m每个数开根号会发现出现了3个1  ,  5个2  ,7个3  ,9个4,式子的

第二个数肯定是一直递增1,而第一个数递增2。那么到m的时候肯定是(2*k-1)*sqrt(m)的形式。 所以只要循环到sqrt(m)利用这个规律则可以累加

值,需要注意的是最后的sqrt(m)不一定恰好就是(2*k-1)个。  则需先处理到sqrt(m)-1。 而另t=(int)sqrt(m)剩余的便是(m-t*t+1)个sqrt(m)了。 时间

复杂度为sqrt(n)。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
#define LL __int64

int main()
{
	int t,i;
	scanf("%d",&t);
	while(t--)
	{
		LL n,m;
		scanf("%I64d",&n);
		double c=sqrt(1.0*n),tmp1=floor(c),tmp2=ceil(c);
		m=n+((c-tmp1)>(tmp2-c)?((int)tmp2):((int)tmp1));
	
		LL k=(int)sqrt(1.0*m);
		LL sum=0,cnt=3;
		for(i=1;i<=k-1;i++)
		{
			sum+=cnt*i;
			cnt+=2;
		}
		sum+=(m-k*k+1)*k;
		printf("%I64d %I64d\n",m,sum);
	}
	return 0;
}	


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值