HDU 5675 ztr loves math (数学推导)

本文探讨了一个数学问题:如何判断一个给定的正整数N是否可以表示为两个完全平方数的差。通过对相邻自然数的平方差进行分析,得出了判断条件,并给出了具体的实现代码。

ztr loves math

题目链接:

http://acm.hust.edu.cn/vjudge/contest/123316#problem/A

Description

ztr loves research Math.One day,He thought about the "Lower Edition" of triangle equation set.Such as n = x^2 - y^2.

He wanted to know that ,for a given number n,is there a positive integer solutions?

Input

There are T test cases.
The first line of input contains an positive integer indicating the number of test cases.

For each test case:each line contains a positive integer ,.

Output

If there be a positive integer solutions,print true,else print false.

Sample Input

4
6
25
81
105

Sample Output

False
True
True
True

Hint

For the fourth case,105 = 13^2-8^2.

题意:

给出一个N(N<=10^18);
问N能否由两个完全平方数相减得到;

题解:

对于相邻自然数 n 和 n+1 :
(n+1)^2 - n^2 = 2n+1;
即相邻两平方数之差一定是一个奇数;
那么任意两个平方数之差一定是多个连续奇数的和.

而任意两个连续奇数之和为:
(2n-1) + (2n+1) = 4n;
即两个连续奇数之和必定能整除4;

那么本题满足条件的n须两个性质之一:

  1. N为奇数
  2. N能被4整除;

特判:由于题目要求任一平方数不能为0;
则1和4不满足要求.

TLE了一发:把puts和cin都换掉就过了...

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 1100
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

LL n;

int main(int argc, char const *argv[])
{
    //IN;

    int t;  scanf("%d", &t);
    while(t--)
    {
        scanf("%I64d", &n);

        if(n == 1 || n==4) {
            printf("False\n");
            continue;
        }

        if(n&1 || n%4==0) printf("True\n");
        else printf("False\n");
    }

    return 0;
}

转载于:https://www.cnblogs.com/Sunshine-tcf/p/5698800.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值