【codeforces 707C】Pythagorean Triples
【题目链接】:http://codeforces.com/contest/707/problem/C
【题意】
给你一个数字n;
问你这个数字是不是某个三角形的一条边;
如果是让你输出另外两条边的大小;
【题解】
首先明确n<=2的时候是无解的。
n>2之后都有解;
这里设
n2+b2=a2
则有
n2=a2−b2
也即
n2=(a+b)∗(a−b)
这里对n分两类讨论;
①
n为奇数
则令
a−b=1
a+b=n2
这样
2∗a=n2+1
因为n是奇数所以右边是个偶数;
得出来的a就是整数了符合题意;
然后b=a-1
②
n为偶数
则令
a−b=2
a+b=n2/2
则
2∗a=n2/2+2
a=n2/4+1
因为n是大于2的偶数,所以a最后得到的结果是个整数且大于2;
b=a-2;
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 110;
LL n;
LL sqr(LL x) { return x*x; }
int main(){
//freopen("F:\\rush.txt", "r", stdin);
rel(n);
if (n <= 2) return puts("-1"), 0;
if (n & 1)
{
LL b = (sqr(n) + 1) / 2;
LL a = b - 1;
printf("%lld %lld\n", a, b);
}
else
{
LL b = sqr(n) / 4 + 1;
LL a = b - 2;
printf("%lld %lld\n", a, b);
}
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 707C】Pythagorean Triples的更多相关文章
- 【Codeforces 707C】Pythagorean Triples(找规律)
一边长为a的直角三角形,a^2=c^2-b^2.可以发现1.4.9.16.25依次差3.5.7.9...,所以任何一条长度为奇数的边a,a^2还是奇数,那么c=a^2/2,b=c+1.我们还可以发现, ...
- codeforces 707C C. Pythagorean Triples(数学)
题目链接: C. Pythagorean Triples time limit per test 1 second memory limit per test 256 megabytes input ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
随机推荐
- 运行Android Studio总是未发现设备
1.未发现虚拟机设备
- Akka源码分析-Akka-Streams-Materializer(1)
本博客逐步分析Akka Streams的源码,当然必须循序渐进,且估计会分很多篇,毕竟Akka Streams还是比较复杂的. implicit val system = ActorSystem(&q ...
- idea下载安装指南
官网地址 https://www.jetbrains.com/idea/ 点击download 有收费版本和社区免费版.我下载了免费的. 有zip和exe两个版本.我先下载了zip绿色版,发现用不了. ...
- [C++ STL] 各容器简单介绍
什么是STL? 1.STL(Standard Template Library),即标准模板库,是一个高效的C++程序库. 2.包含了诸多常用的基本数据结构和基本算法.为广大C++程序员们提供了一个可 ...
- 总结MySQL中SQL语法的使用
--where子句操作符: where子句操作符 = 等于 <> 不等于(标准语法) != 不等于(非标准语法,可移植性差) < 小于 <= 小于等于 > 大于 > ...
- Position属性四个值:static、fixed、absolute和relative的区别
1.static(静态定位):默认值.没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明). 2.relative(相对定位):生成相对 ...
- 第四次团队作业——项目Alpha版本发布
这个作业属于哪个课程 <课程的链接> 这个作业要求在哪里 <作业要求的链接> 团队名称 Three cobblers 这个作业的目标 发布项目α版本,对项目进 ...
- Android屏幕尺寸与度量单位(px,dp,sp)简介
MarkdownPad Document *:first-child { margin-top: 0 !important; } body>*:last-child { margin-botto ...
- weex开发前配置
weex 环境配置:win10 node java git weex-toolkit :npm install weex-toolkit -g在安装weex-toolkit的时候,尽量不要使用cnpm ...
- HDU_1006_Tick and Tick
Tick and Tick Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...