Pythagorean Triples

题目链接:

http://codeforces.com/contest/707/problem/C

Description


```
Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples.

For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples.

Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse.

Katya had no problems with completing this task. Will you do the same?

</big>

##Input
<big>

The only line of the input contains single integer n (1 ≤ n ≤ 109) — the length of some side of a right triangle.

</big>

##Output
<big>

Print two integers m and k (1 ≤ m, k ≤ 1018), such that n, m and k form a Pythagorean triple, in the only line.

In case if there is no any Pythagorean triple containing integer n, print  - 1 in the only line. If there are many answers, print any of them.

</big>

##Examples
<big>
input
3
output
4 5
input
6
output
8 10
input
1
output
-1
input
17
output
144 145
input
67
output
2244 2245
</big> ##Source
<big>
Codeforces Round #368 (Div. 2)
</big> <br/>
##题意:
<big>
构造一个直角三角形以N作为一条边长.(直角边和斜边皆可)
</big> <br/>
##题解:
<big>
N作为直角边时,N^2为两个平方数的差:
易知任意两平方数之差必定为奇数或者是4的倍数. [参考](http://www.cnblogs.com/Sunshine-tcf/p/5698800.html)
N作为斜边时,N^2为两个平方数的和:
由于N的规模较小,可以直接枚举找到 a^2+b^2 = N^2. (枚举规模sqrt(n))
<br/>
upd:这个问题其实有[公式](http://www.zybang.com/question/712e6230d81ecf844ef145786bf6918a.html):
奇数:(2n+1, 2n^2+2n, 2n^2+2n+1)
偶数:(2n, n^2-1, n^2+1)
</big> <br/>
##代码:
``` cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <list>
#define LL long long
#define eps 1e-8
#define maxn 101000
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std; int main(int argc, char const *argv[])
{
//IN; LL n;
while(scanf("%I64d", &n) != EOF)
{
n = n * n;
if(n!=1 && n!=4) {
if(n & 1) {
LL cur = (n - 1) >> 1;
printf("%I64d %I64d\n", cur,cur+1);
continue;
}
if(n % 4 == 0) {
LL cur = (n - 4) >> 2;
printf("%I64d %I64d\n", cur,cur+2);
continue;
}
} bool flag = 0;
for(LL i=1; i*i<=n; i++) {
LL cur = n - i * i;
if(cur < 1) continue;
LL sqt = (LL)sqrt(cur);
if(sqt * sqt == cur) {
printf("%I64d %I64d\n", i, sqt);
flag = 1;
break;
}
} if(!flag) printf("-1\n");
} return 0;
}

Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)的更多相关文章

  1. Codeforces Round #368 (Div. 2) C. Pythagorean Triples 数学

    C. Pythagorean Triples 题目连接: http://www.codeforces.com/contest/707/problem/C Description Katya studi ...

  2. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  3. Codeforces Round #368 (Div. 2) B. Bakery (模拟)

    Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...

  4. Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)

    Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...

  5. 暴力 Codeforces Round #183 (Div. 2) A. Pythagorean Theorem II

    题目传送门 /* 暴力:O (n^2) */ #include <cstdio> #include <algorithm> #include <cstring> # ...

  6. Codeforces Round #368 (Div. 2) C

    Description Katya studies in a fifth grade. Recently her class studied right triangles and the Pytha ...

  7. Codeforces Round #368 (Div. 2)A B C 水 图 数学

    A. Brain's Photos time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. Codeforces Round #368 (Div. 2) A , B , C

    A. Brain's Photos time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  9. Pythagorean Triples(Codeforces Round #368 (Div. 2) + 构建直角三角形)

    题目链接: https://codeforces.com/contest/707/problem/C 题目: 题意: 告诉你直角三角形的一条边,要你输出另外两条边. 思路: 我们容易发现除2外的所有素 ...

随机推荐

  1. 非常非常非常好!path-sum-iii

    https://leetcode.com/problems/path-sum-iii/ 最终我还是没做出好的解法.还是看的别人的解法. 即使看了别人的解法,开始还实现错了. 还有很长的路要走. pac ...

  2. Android开源库--PhotoView图片查看

    如果说我比别人看得更远些,那是因为我站在了巨人的肩上. github地址:https://github.com/chrisbanes/PhotoView 介绍 在一般的应用中,总会遇到查看图片的功能, ...

  3. zoj 3822 Domination (概率dp 天数期望)

    题目链接 参考博客:http://blog.csdn.net/napoleon_acm/article/details/40020297 题意:给定n*m的空棋盘 每一次在上面选择一个空的位置放置一枚 ...

  4. HTML的列表标签

    一.上下层列表标签:<dl>..</dl>: 上层dt 下层dd:封装的内容会被自动缩进的效果 <dl> <dt>运动户外</dt> < ...

  5. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  6. sql DROP 和DELETE、TRUNCATE用法

    DROP:删除数据库已存在的表DROP TABLE tbname DELETE:删除记录delete from tbname truncate:清空表,重置索引truncate table tbnam ...

  7. Linux删除文件后空间没有释放

    .COMMAND默认以9个字符长度显示的命令名称.可使用+c参数指定显示的宽度,若+c后跟的参数为零,则显示命令的全名.PID:进程的ID号.PPID父进程的IP号,默认不显示,当使用-R参数可打开. ...

  8. windows下github pages + hexo next 搭建个人博客

    一.github pages 搭建个人博客一般需要购买域名和空间,github pages为我们提供了这两样东西,而且是免费的,相关介绍和使用方法参考这里 github pages. 二.Hexo 一 ...

  9. myisam 与innodb的区别

    myisam 与innodb的区别 frm结构 fri索引 frd数据 innodb:一个表一个文件:frm文件 所有的innodb表,都使用表空间储存, 数据和索引的保存文件不同,myisam 分开 ...

  10. 【英语】Bingo口语笔记(28) - 表示“秘密”