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

题意是求最小的X;

解:用简单的暴力求解必然超时,10^9,;

因 :y^2 = n +x^2 ,y^2 - x^2 = n; (y-x)(y+x) = n;

满足条件的示例有:    3^2 =  5 + 2^2;

8^2 = 39 + 5^2;

(5^2 = 24 + 1^2;)/(7^2 = 24 + 5^2).......

明显可以看出      n = R(x+y);R是常数;  5 = 1(3+2),39 = 3(8+5),24 = 4(1+5),24 = 2(7+5).。。。。

故:

x  =  (n/R-R)/2;y = (n/R+R)/2;

然后进行枚举R,即可AC;

296KB   140ms
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        long long int R;
        int flag = 0;
       for(R=(int)sqrt(n);R>=1;R--)
       {//if里面的条件,要自己进行一些调试工作
         if((n/R-R)%2==0&& (n%R)==0 &&(n/R-R)/2!=0&& n!=R*R)
         {
            printf("%lld\n",(n/R-R)/2);
            flag=1;
            break;
         }
       }
          if(!flag)
            printf("-1\n");

    }
    return 0;
}

A Simple Problem

时间: 2024-07-31 19:17:16

A Simple Problem的相关文章

NYOJ 707 A Simple Problem(结构体排序) 睡前一水~~

链接:click here 题意: A Simple Problem 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 You know, just as the title imply, this is a simple problem. In a contest, given the team-id, solved, penalty of all the teams, tell me the champion.If the numbers of solved pr

E - A Simple Problem with Integers

#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; #define N 100002 struct node { int l,r; long long lz,w; }q[4*N]; void pushup(int rt) { q[rt].w=q[rt*2].w+q[rt*2+1].w; } void pushdo

POJ 3468 A Simple Problem with Integers

链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 77302 Accepted: 23788 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two ki

poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 83959   Accepted: 25989 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. Yo

poj3468 A Simple Problem with Integers 线段树区间更新

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97722   Accepted: 30543 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ 3468 A Simple Problem with Integers(树状数组区间更新)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97217   Accepted: 30358 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ-3468 A Simple Problem with Integers(线段树)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 94899   Accepted: 29568 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

hdu 5349 MZL&#39;s simple problem

Problem Description A simple problem Problem Description You have a multiple set,and now there are three kinds of operations: 1 x : add number x to set 2 : delete the minimum number (if the set is empty now,then ignore it) 3 : query the maximum numbe

【multiset】hdu 5349 MZL&#39;s simple problem

[multiset]hdu 5349 MZL's simple problem 题目链接:hdu 5349 MZL's simple problem 题目大意 n次操作,插入元素.删除最小元素.查询最大元素并输出. C++STL的multiset的使用 set--多元集合(元素不可重复),multiset--可重复元素的多元集合 多元集合(MultiSets)和集合(Sets)相像,只不过支持重复对象.(具体用法请参照set容器) set和multiset内部是以平衡二叉树实现的: 从内部数据结

Poj 3468 A Simple Problem with Integers(线段树 区间更新 延迟标记)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 73239   Accepted: 22607 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of