PKU 1046 Square Number (公式化简推导)

题目链接:哆啦A梦传送门

参考博客:https://blog.csdn.net/haut_ykc/article/details/76404547

题解:

这题先把a分离出来,

令 a*(a+b)=(a+t)*(a+t),得 a=t*t/(b-2*t),其中 a,t为非负整数。

我们观察下这个式子,要想满足a为非负整数,则t的范围为 [0,b/2),而且a是随着t的增大而增大的,故我们要尽量靠近b/2。

如果b为奇数,那么显然 t=(b-1)/2,则a=t*t。

      b为偶数,首先t=(b-1)/2,假设此时t为偶数,那么a=t*t/2。要是此时t为奇数,因为a要非负整数,此时t*t不能整除2,所以此时t要减一,即t=(b-4)/2, a=t*t/4。

 

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>

using namespace std;

typedef long long LL;

int main()
{

    LL a,b,t;
    int ncase;

    scanf("%d",&ncase);

    while(ncase--)
    {
        scanf("%lld",&b);

        if(b&1){
            t=(b-1)/2;
            printf("%lld\n",t*t);
        }
        else{
            t=(b-2)/2;
            if(t&1){
                t--;
                printf("%lld\n",t*t/4);
            }
            else{
                printf("%lld\n",t*t/2);
            }
        }

    }
    return 0;

}

                                              

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值