您的位置:首页 > 其它

hdoj 4143 A Simple Problem

2013-10-10 21:22 204 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4143

解题思路:枚举 n=a*b, a<b. 则有 若a,b 奇偶性相同,则 x=(b-a)/2. 不断更新~

/**************************************************************************
user_id: SCNU20102200088
problem_id: hdoj 4143
problem_name: A Simple Problem
**************************************************************************/

#include <algorithm>
#include <iostream>
#include <iterator>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <bitset>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

//线段树
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

//手工扩展栈
#pragma comment(linker,"/STACK:102400000,102400000")

const double EPS=1e-9;
const double PI=acos(-1.0);
const double E=2.7182818284590452353602874713526;  //自然对数底数
const double R=0.5772156649015328606065120900824;  //欧拉常数:(1+1/2+...+1/n)-ln(n)

const int x4[]={-1,0,1,0};
const int y4[]={0,1,0,-1};
const int x8[]={-1,-1,0,1,1,1,0,-1};
const int y8[]={0,1,1,1,0,-1,-1,-1};

typedef long long LL;

typedef int T;
T max(T a,T b){ return a>b? a:b; }
T min(T a,T b){ return a<b? a:b; }
T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
T lcm(T a,T b){ return a/gcd(a,b)*b; }

///////////////////////////////////////////////////////////////////////////
//Add Code:
const int INF=1000000000;
///////////////////////////////////////////////////////////////////////////

int main(){
std::ios::sync_with_stdio(false);
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
///////////////////////////////////////////////////////////////////////
//Add Code:
int Case,n,i;
scanf("%d",&Case);
while(Case--){
scanf("%d",&n);
int ans=INF;
for(i=1;i<=sqrt(n);i++){
if(n%i==0){
int a=i,b=n/i;
if(b-a>0 && (b-a)%2==0){
int x=(b-a)/2;
if(x<ans) ans=x;
}
}
}
if(ans==INF) printf("-1\n");
else printf("%d\n",ans);
}
///////////////////////////////////////////////////////////////////////
return 0;
}

/**************************************************************************
Testcase:
Input:
2
2
3
Output:
-1
1
**************************************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: