【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 ...
随机推荐
- 【168】ENVI入门系列
参考:ENVI-IDL中国的博客 [ENVI入门系列]01.ENVI产品简介与入门 [ENVI入门系列]02.自定义坐标系(北京54.西安80.2000坐标系) [ENVI入门系列]03.基于自带定位 ...
- SQL Server 添加描述
添加描述的格式 exec sys.sp_addextendedproperty @name = N'MS_Description' ,@value = 'value',@level0type=N'SC ...
- Hibernate的表之间的关系
表:A.B 一对多关系:A中的一个字段的记录(主键)引用B中的多个记录(外键) 多对一关系:A中的一个字段的记录(外键)引用B中的一个记录(主键) 一对一关系:A.B两个表的关联字段都是主键 多对多关 ...
- E20171214-sl
well-grounded 地基打的好 relevent adj. 有关的,中肯的; 相关联的 proposal n. 建议; 提议; 求婚; 〈美〉投标;
- E20171212-hm
odd adj. 古怪的; 奇数的; 剩余的; 临时的; odd number 奇数 even adj. 偶数的 even number 偶数
- Unity ScriptObject
http://godstamps.blogspot.com/2012/02/unity-3d-scriptableobject-assetbundle.html http://ivanozanchet ...
- GoAhead4.1.0 开发总结二(自定义使用)
环境 官方文档:https://www.embedthis.com/goahead/doc/ 源码下载: goahead-4.1.0-src.tgz 系统平台:Ubuntu 12.04.4 gcc v ...
- RT-Thread 设备驱动-硬件定时器浅析与使用
RT-Thread 4.0.0 访问硬件定时器设备 应用程序通过 RT-Thread 提供的 I/O 设备管理接口来访问硬件定时器设备,相关接口如下所示: 函数 描述 rt_device_find() ...
- 洛谷 P1865 A % B Problem(求区间质数个数)
题目背景 题目名称是吸引你点进来的 实际上该题还是很水的 题目描述 区间质数个数 输入输出格式 输入格式: 一行两个整数 询问次数n,范围m 接下来n行,每行两个整数 l,r 表示区间 输出格式: 对 ...
- [BZOJ1331]魔板
Description 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 我们知道魔板的每一个方格都有一种颜色.这8 ...