HDU ACM 4143 A Simple Problem

分析:y^2=n+x^2=>y^2-x^2=n。

(y-x)(y+x)=n。

令k1=y-x;k2=y+x。

则有:y=(k1+k2)/2,x=y-k1。

枚举n的所有因数k1,k2使得y为整数。则最小的x即为所求。

注意:x不能为0。

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

void Solve(int n)
{
	int i,x,y,minx;

	minx=0x7fffffff;
	for(i=1;i<=sqrt(n);i++)
		if(!(n%i))
		{
			if(!((i+n/i)&1))
			{
				y=(i+n/i)>>1;
				x=y-i;

				if(x)
					minx=minx<x?minx:x;
			}
		}

	if(minx==0x7fffffff)
		cout<<-1<<endl;
	else
		cout<<minx<<endl;
}

int main()
{

	int T,n;

	cin>>T;
	while(T--)
	{
		cin>>n;
		Solve(n);
	}
    return 0;
}
时间: 2024-10-12 21:06:41

HDU ACM 4143 A Simple Problem的相关文章

HDU ACM 2522 A simple problem 模拟除法

分析:在除的过程中,当出现相同余数时即出现循环节. #include<iostream> using namespace std; bool h[100002]; void div(int x) { int t; memset(h,false,x*sizeof(h[0])+1); h[1]=true; t=1; while(t) { t=t*10; cout<<t/x; t=t%x; if(h[t]) //再次出现相同余数,表示出现循环节 break; h[t]=true; } }

hdu 4143 A Simple Problem (变形)

题目 题意:给n,求x; 直接枚举肯定超时, 把给的式子变形, (y+x)(y-x) = n; 令y-x = b, y+x = a; 枚举b, b 的范围肯定是sqrt(n),  y = (a+b)/2;  x = (a-b)/2; b越大, x越小, 所以倒着枚举b 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 using namesp

HDU 4143 A Simple Problem(数论-水题)

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

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内部是以平衡二叉树实现的: 从内部数据结

hdu 5349 MZL&#39;s simple problem(multiset)

代码: #include<set> #include<cstdio> using namespace std; multiset<int> st; int main() { int n; multiset<int>::iterator it; while(scanf("%d",&n)==1) { st.clear(); int k,num; for(int i=0; i<n; i++) { scanf("%d&qu

hdu - 5349 MZL&#39;s simple problem(解题报告)

A - MZL's simple problem Time Limit:1500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description A simple problem Problem Description You have a multiple set,and now there are three kinds of operations: 1 x : add number

HDU 5349 MZL&#39;s simple problem(优先队列)

MZL's simple problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 776    Accepted Submission(s): 375 Problem Description A simple problem Problem Description You have a multiple set,and now

HDU 4267 A Simple Problem with Integers 多个树状数组

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4000    Accepted Submission(s): 1243 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with