A Simple Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 5748    Accepted Submission(s): 1515

Problem Description
For a given positive integer n, please find the smallest positive integer x that we can find an integer y such that y^2 = n +x^2.
 
Input
The first line is an integer T, which is the the number of cases.
Then T line followed each containing an integer n (1<=n <= 10^9).
 
Output
For each integer n, please print output the x in a single line, if x does not exit , print -1 instead.
 
Sample Input
2
2
3
 
Sample Output
-1
1
 
 

// y^2 = n +x^2.即 (y+x)(y-x) = n
//设 i = y-x, n/i = y+x , x = (n/k-k)%2 由于 x, y是正整数, 则 y,x不会等于0, 所以这边还有一点最重要的(y+x) (y-x) 不能相等

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

int main()
{
  int t, n, i, flag, x;
  cin >> t;
  while(t--)
  {
    cin >> n;
    flag = 0;
    i = sqrt(n);
    for(int k = i; k >=1; k--) //题意是求符合条件,最小的x, 所以 k 要从最大因子开始判断
    {
      if(n%k == 0 && (n/k-k)%2 == 0 && n/k != k) //控制好符合条件是最重要的
      {
        x = (n/k - k)/2;
        cout << x << endl;
        flag = 1;
        break;
      }
    }
    if(flag == 0)
    {
      cout << -1 << endl;
    }
  }
  return 0;
}

hdu4143 A Simple Problem的更多相关文章

  1. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  2. POJ 3468 A Simple Problem with Integers(线段树/区间更新)

    题目链接: 传送门 A Simple Problem with Integers Time Limit: 5000MS     Memory Limit: 131072K Description Yo ...

  3. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 58269   ...

  4. ACM: A Simple Problem with Integers 解题报告-线段树

    A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...

  5. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  6. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

    A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...

  7. BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...

  8. POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92632   ...

  9. A Simple Problem with Integers(树状数组HDU4267)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

随机推荐

  1. asp.net -mvc框架复习(6)-基于MVC实现简单计算器

    1.创建好文件夹 2.视图层代码编写 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dyn ...

  2. 安装mysql后运行.net程序出错

    安装mysql后运行.net程序出错: 出错位置:C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config  出错信息:未 ...

  3. 我的java学习之路--Reflect专题

    学习网址:http://www.imooc.com/video/3725 1.Class类的使用 class类 在面向对象的世界里,万事万物皆对象 java语言中,静态的成员.普通数据类型类不是对象. ...

  4. synchronized内存可见性理解

    一.背景 最近在看<Java并发编程实战>这本书,看到共享变量的可见性,其中说到"加锁的含义不仅仅局限于互斥行为,还包括内存可见性". 我对于内存可见性第一反应是vol ...

  5. hibernate_xml映射exception

    错误原因:在通过hibernate指示生成两个表之间的外键关系之后,一个表中引用的外键不在另一个表的参考范围里面. 解决:使之满足参考完整性 org.hibernate.TransientObject ...

  6. GIT工程迁移方法总结

    Git工程迁移方法总结 Git最近准备迁移一下位置,这里采用命令行的方式,做如下操作. 1.git init  初始化git仓库,这个时候发现本地文件夹多了个.git的文件夹. 2.git remot ...

  7. JAVA中pdf转图片的方法

    JAVA中实现pdf转图片可以通过第三方提供的架包,这里介绍几种常用的,可以根据自身需求选择使用. 一.icepdf.有收费版和开源版,几种方法里最推荐的.转换的效果比较好,能识别我手头文件中的中文, ...

  8. JSP错误页面的处理和exception对象

    exception对象可以使用的主要方法如下所示. l        getMessage():该方法返回错误信息. l        printStackTrace():该方法以标准错误的形式输出一 ...

  9. thinkphp5学习(一)——thinkphp5的目录结构与开发规范

    开发规范: 目录和文件 目录使用小写+下划线: 类库.函数文件统一以.php为后缀: 类的文件名均以命名空间定义,并且命名空间的路径和类库文件所在路径一致: 类文件采用驼峰法命名(首字母大写),其它文 ...

  10. python监控微信报警

    微信接口调用代码: #coding=utf8 import itchat from flask import Flask, request itchat.auto_login(enableCmdQR= ...