HDOJ 4342 —— 数学

History repeat itself

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1251    Accepted Submission(s): 563


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 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
 

Author
HIT
 

Source
 

Recommend
zhuyuanchen520
 
题意是给你一个N,问你把自然数数列除去平方数后的第N个数的大小。
引用某大神的证明:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17
1,1,1,2,2,2,2,2,3, 3, 3, 3, 3, 3, 3, 4,4
第I个数为满足K^2<=i<(K+1)^2,,,,K为第I个数之前的那个平方数
(K+1)^2-K^2=2K+1;
对于每一个k都有:(2K+1)*K;
设N前面的那个平方数为M=(N)^1/2
则有(M-1)= A个这样的K,这些项的和为:A(A+1)(2A+1)/3+(A+1)*A/2
(N^2的前n项和的公式为n(n+1)(2n+1)/6)
再加从M^2~N 的数之和(N-M*M+1)×M ;
所以总和为A(A+1)(2A+1)/3+(A+1)*A/2 + (N-M*M+1)×M;

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>

using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
//#define mid ((l + r) >> 1)
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 100000 + 500;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const double e = 2.718281828;
#define eps 1e-8
#define DEBUG 1
#define mod 1000000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
///#pragma comment(linker, "/STACK:102400000,102400000")__int64 a[10050];

int main()
{
    __int64 n , a,  m , sum;
    int t;
    scanf("%d" , &t);
    while(t--)
    {
        sum = 0;
        scanf("%I64d" , &n);
        n = n + sqrt(n * 1.0) + 0.5;
        m = sqrt(n * 1.0);
        a = m - 1;
        sum = (a * (a + 1) * ( 2 * a + 1)) / 3;
        sum += (a * (a + 1)) / 2;
        sum += (n - m * m + 1) * m;
        printf("%I64d %I64d\n", n , sum);
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值