HDU 4342History repeat itself 数学

C - History repeat itself
Time Limit:1000MS     Memory Limit:32768KB     

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 announced 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 September 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.
 

Sample Input

4 1 3 6 10
 

Sample Output

2 2 5 7 8 13 13 28
 
题意:
  求第n个非平方数,
   求前n个数的sqrt(i)和
题解:
   求第n个非平方数,假设为y,y之前有x个平方数,则y=n+x,且sqrt(n+x)=x,可以得出y
   求前n个数的sqrt(i)和: 分段求就好了
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
typedef long long ll;
using namespace std;
#define inf 10000000
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
//***************************************************************
int main()
{
    int t;
    while(scanf("%d",&t)!=EOF)
    {
          while(t--)
          {
              ll n=read();
              ll tmp=(ll)sqrt(n*1.0);
              ll i=1;
              while((ll)sqrt(n+i)!=i)i++;
              ll x=n+i;
              ll flag=sqrt(n+i);
              ll ans=0;
              for(ll i=2;i<=flag;i++)
              {
                  ans+=(i*i-(i-1)*(i-1))*(i-1);
              }
              ans+=(x-flag*flag+1)*flag;
              cout<<x<<" "<<ans<<endl;
          }
    }
    return 0;
}
代码狗

 

 
 

转载于:https://www.cnblogs.com/zxhl/p/4746146.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值