HDU 4342 History repeat itself 简单公式

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


这个题目题意就是求M 第N个非完全平方数,对M用公式求和

求第N个非完全平方数,就先对N与一个完全平方数减去之前出现的完全平方数个数进行比较,容易判断出当第一次出现N小的情况时就可以确实M

对已经求的M 如果M大于 I 的平方 则  I * I 到 (i - 1)*(i - 1) 之间的所有数都要加到总和中 如果M小 则对M到 (i - 1)*(i - 1) 之间求和

又因为对于该函数 两个平方数之间的数的函数值是一样的,所以求和可以简化为乘法;

#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define N 30100;
long long n;
long long p[200000];
int main()
{
    int t;
    scanf("%d",&t);
   // freopen("b.txt","w",stdout);
    while(t--)
    {
        scanf("%I64d",&n);
       // printf("%I64d\n",n);
        long long m;
        for(int i = 0; i <= (2<<16); i++) p[i] = (long long)(i*i);
       // printf("%I64d\n",n);
        for(int i = 1; i <= (2<<16); i++)
        {
            if( n <= (p[i] - i))
            {
                m = n + (i-1) ;
                break;
            }
        }
        long long ans = 0;
        for(int i = 1; i <= (2<<16); i++ )
        {
            if(m >= p[i])
            {
                ans += (p[i] - p[i-1])*(long long)(i-1);
            }
            if( m < p[i])
            {
                ans+= (m - p[i-1]+1) * (long long)(i-1);
                break;
            }
        }
        printf("%I64d %I64d\n",m,ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值