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
解题代码:
View Code
解题思路:假设 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
相关文章推荐
- 【解题报告】poj openjudge 拼写检查 pku数算mooc 检索
- OpenJudge / POJ 1002 487-3279 解题报告 C++
- 【Noi OpenJudge】 核电站 解题报告
- 【Noi OpenJudge】 算24 解题报告
- 【解题报告】openjudge Freda的越野跑 数据结构与算法mooc 内排序
- openjudge 9277 堆木头的解题报告
- 【解题报告】openjudge DNA排序 数据结构与算法mooc 内排序
- PKU Online Judge 1046 Square Number
- 【Noi OpenJudge】 带通配符的字符串匹配 解题报告
- pku 1489解题报告
- pku 1970 搜索 The Game 解题报告
- pku 1961 KMP period 解题报告
- FOJ--1046--Tempter of the Bone--解题报告(典型的迷宫搜索)
- POJ1046解题报告
- pku 2392 space elevator 多重背包 dp 解题报告
- Pku acm 1080 Humman Gene Function 动态规划题目解题报告(八)
- PKU Open Judge 1036:Gugle Seating
- pku数学题简单解题报告(1)
- pku 1011 解题报告
- poj1046 解题报告