您的位置:首页 > 运维架构

PKU openjudge 1046 Square Number 解题报告

2013-06-03 16:55 417 查看
题意:给定正整数b,求最大的整数a,满足a*(a+b) 为完全平方数

解题思路:假设 a^2+a*b = (a+t)^2 -> a^2 + a*b = a^a + 2*a*t +t^2 -> a*b = 2*a*t +t^2 -> a = t^2/(b-2*t);

因为 a = t^2/(b-2*t); 易知( t >= 0 && t < b/2)

假如 可知 在t的范围内 a 随 t 的增大而增大 ,又因为 a 必须是 整数

所以(1) b为奇数的时候 t = (b-1)/2;

(2) b 为 偶数 且 (b-2)/2 为偶数 ,那么t = (b-2)/2

(3)b 为 偶数且(b-2)/2 为奇数,那么 t = (b - 4)/2

解题代码:

// File Name: a.c
// Author: darkdream
// Created Time: 2013年06月03日 星期一 16时04分55秒

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>

int main(){

//freopen("/home/plac/problem/input.txt","r",stdin);
//freopen("/home/plac/problem/output.txt","w",stdout);
int n ;
scanf("%d",&n);
while(n--)
{
long long b;
scanf("%lld",&b);
if(b %2 == 1 )
{
printf("%lld\n",(b-1)/2*(b-1)/2);
}
else
{
if((b-2)/2 %2 == 1)
{
printf("%lld\n",(b-4)/4*(b-4)/4);
}
else
printf("%lld\n",(b-2)/2*(b-2)/2/2);

}
}
return 0 ;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: