【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 ...
随机推荐
- Java多线程系列三——实现线程同步的方法
两种实现线程同步的方法 方法 特性 synchronized 不需要显式地加解锁,易实现 ReentrantLock 需要显式地加解锁,灵活性更好,性能更优秀,结合Condition可实现多种条件锁 ...
- Java setAccessible方法作用
在Java中可以通过反射进行获取实体类中的字段值,当未设置Field的setAccessible方法为true时,会在调用的时候进行访问安全检查,会抛出IllegalAccessException异常 ...
- js获取标签的三种方式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- MongoDB一些常用指令与他的JavaScript的对应表
- ACM_01背包
背包1 Time Limit: 2000/1000ms (Java/Others) Problem Description: 有n个重量和价值分别为Wi,Vi的物品,现从这些物品中挑选出总量不超过 W ...
- 轮播图-version1
实现目标 按'>'出现下一caption,按'<'出现上一caption 按下面的点到指定的caption 自动轮播 思路: 设置一个carousel容器,里面有carousel的每一张图 ...
- 多路开关模式的switch语句
在实例10中,将break语句去掉之后,会将符合检验条件后的所有语句都输出.利用这个特点,可以设计多路开关模式的switch语句,例如:在平年一年12个月,1.3.5.7.8.10.12月是31天,4 ...
- Android开发笔记(4)——MainActivity.java文件修改&布局嵌套
笔记链接:http://www.cnblogs.com/igoslly/p/6805020.html 笔记以开发名为CoffeeOrder的app活动为线索,介绍app如何从功能设计→ ...
- Android开发笔记(3)——GridLayout
笔记链接:http://www.cnblogs.com/igoslly/p/6799939.html GirdLayout 计算器实例及详尽的笔记:http://www.cnblogs.com/sky ...
- js中取整数的方法
1.取整的方法 Math.floor( ) Math 对象的方法--取比当前数值小的最大整数(下取整). Math.ceil( ) Math对象的方法--取比当前数值大的最小整数(上取整). Math ...