[codeforces] C - Pythagorean Triples 数学
2016-10-13 21:58
190 查看
C - Pythagorean Triples
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice CodeForces
707C
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 calledPythagorean 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?
Input
The only line of the input contains single integer n (1 ≤ n ≤ 109) — the length of some side
of a right triangle.
Output
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.
Sample Input
Input
Output
Input
Output
Input
Output
Input
Output
Input
Output
Hint
Illustration for the first sample.
对于直角三角形△ABC,如图:
我们知道的是
因为题目说如果题目存在多种解,输出任何一个就可以了,所以我们不妨假设输入的n是一条直角边的长度,那么
根据平方差公式可得
那么,这个时候,我们要求解的就是a,b
于是乎,我们分类讨论即可
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 10007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define PI acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1#define Rson mid+1, R, rt<<1|1const int maxn=5e2+10;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int main()
{
ll n;
scanf("%I64d",&n);
if(n==1||n==2)
puts("-1");
else if(n*n%2)
printf("%I64d %I64d\n",(n*n-1)/2,(n*n+1)/2);
else
printf("%I64d %I64d\n",(n*n/2-2)/2,(n*n/2+2)/2);
return 0;
}
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice CodeForces
707C
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 calledPythagorean 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?
Input
The only line of the input contains single integer n (1 ≤ n ≤ 109) — the length of some side
of a right triangle.
Output
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.
Sample Input
Input
3
Output
4 5
Input
6
Output
8 10
Input
1
Output
-1
Input
17
Output
144 145
Input
67
Output
2244 2245
Hint
Illustration for the first sample.
对于直角三角形△ABC,如图:
我们知道的是
因为题目说如果题目存在多种解,输出任何一个就可以了,所以我们不妨假设输入的n是一条直角边的长度,那么
根据平方差公式可得
那么,这个时候,我们要求解的就是a,b
于是乎,我们分类讨论即可
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 10007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define PI acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1#define Rson mid+1, R, rt<<1|1const int maxn=5e2+10;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int main()
{
ll n;
scanf("%I64d",&n);
if(n==1||n==2)
puts("-1");
else if(n*n%2)
printf("%I64d %I64d\n",(n*n-1)/2,(n*n+1)/2);
else
printf("%I64d %I64d\n",(n*n/2-2)/2,(n*n/2+2)/2);
return 0;
}
相关文章推荐
- CodeForces 24C Sequence of points (几何)
- Codeforces 347C Alice and Bob
- CodeForces 591B
- Codeforces 347C - Alice and Bob
- CodeForces - 622E dfs+贪心
- [CodeForces-721E]Road to Home
- CodeForces 245D Restoring Table
- Codeforces 460C Present (贪心 + 二分)
- 【CodeForces】[621A]Wet Shark and Odd and Even
- 【CodeForces】444A - DZY Loves Physics(图论规律)
- [线段树] Codeforces 794F Round #414 F. Leha and security system
- Codeforces 284E Coin Troubles【思维+拓扑排序+完全背包】好题!
- CodeForces 401C
- codeforces 414B B. Mashmokh and ACM(dp)
- codeforces 354 div2 C Vasya and String 前缀和
- CodeForces 732B Cormen — The Best Friend Of a Man
- Codeforces 104C Cthulhu dfs暴力 || 点双连通缩点
- CodeForces 867A Between the Offices
- Codeforces 413C Jeopardy!(贪心)
- CodeForces 425E Sereja and Sets