CF707C Pythagorean Triples 题解

  • 题目
    • 链接
    • 字面描述
      • 题面翻译
      • 题目描述
      • 输入格式
      • 输出格式
      • 样例 #1
        • 样例输入 #1
        • 样例输出 #1
      • 样例 #2
        • 样例输入 #2
        • 样例输出 #2
      • 样例 #3
        • 样例输入 #3
        • 样例输出 #3
      • 样例 #4
      • 样例输入 #4
      • 样例输出 #4
    • 样例 #5
      • 样例输入 #5
      • 样例输出 #5
    • 提示
  • 思路
  • 代码实现

题目

链接

https://www.luogu.com.cn/problem/CF707C

字面描述

题面翻译

给出一个数字,要你求出另外的两个数使得这三个数构成勾股数

题目描述

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?

输入格式

The only line of the input contains single integer $ n $ ( $ 1<=n<=10^{9} $ ) — the length of some side of a right triangle.

输出格式

Print two integers $ m $ and $ k $ ( $ 1<=m,k<=10^{18} $ ), 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.

样例 #1

样例输入 #1

3

样例输出 #1

4 5

样例 #2

样例输入 #2

6

样例输出 #2

8 10

样例 #3

样例输入 #3

1

样例输出 #3

-1

样例 #4

样例输入 #4

17

样例输出 #4

144 145

样例 #5

样例输入 #5

67

样例输出 #5

2244 2245

提示

Illustration for the first sample.

思路

纯数学题

体面很好理解,就是给你一个数 n n n求另外2个数能与它构成勾股数。

勾股数: a 2 + b 2 = c 2 a^2+b^2=c^2 a2+b2=c2
n n n可能为 a , b , c a,b,c a,b,c

给2个数求1个很好办,几个 i f if if的事情,但1个求2个没有任何关系的量这个问题对我来说很未知

数学上遇到一个未知的问题先根据数的界或性质分类(引用至胡老师语录(初中数学老师))

∵本人有一点点因式分解的基础
∴突然想到了平方差公式

我可以把 n n n当成 a a a
∴ a 2 = c 2 − b 2 a^2=c^2-b^2 a2=c2−b2
∴ a 2 = ( c − b ) ( c + b ) a^2=(c-b)(c+b) a2=(c−b)(c+b)

已经推到这里,但是还不能得出任何结论,根据数性,我将 a a a分成2类:

  1. 奇数
  2. 偶数

若 a 为奇数 a为奇数 a为奇数
∵ b 、 c 均为整数 \because b、c均为整数 ∵b、c均为整数
∴ ( c − b ) = 1 , ( c + b ) = a 2 \therefore (c-b)=1,(c+b)=a^2 ∴(c−b)=1,(c+b)=a2
∴ b = ( a 2 − 1 ) / 2 , c = ( a 2 + 1 ) / 2 \therefore b=(a^2-1)/2,c=(a^2+1)/2 ∴b=(a2−1)/2,c=(a2+1)/2
a = 1 时无解 a=1时无解 a=1时无解
ok,奇数搞定

若 a 为偶数 a为偶数 a为偶数
根据等式性质,假设 a a a有 x x x个2可提,设 c n t = a / ( 2 x ) cnt=a/(2^x) cnt=a/(2x)

奇数的前面已经推出来了,只需要代入奇数的推一遍,最后在乘上 2 x 2^x 2x
∴ b = ( c n t 2 − 1 ) / 2 ⋅ 2 x , c = ( c n t 2 + 1 ) / 2 ⋅ 2 x \therefore b=(cnt^2-1)/2·2^x,c=(cnt^2+1)/2·2^x ∴b=(cnt2−1)/2⋅2x,c=(cnt2+1)/2⋅2x

但是如果 c n t = 1 cnt=1 cnt=1就不妙了, ∵ a = 1 无解 \because a=1 无解 ∵a=1无解
应最朴素的方式观察一下:3,4,5 这一组例子 非常的好观察到用 n n n表示4就可以了;
∴ b = 3 ⋅ 2 \therefore b=3·2 ∴b=3⋅2x-2 , c = 5 ⋅ 2 ,c=5·2 ,c=5⋅2x-2

时间复杂度分析
n n n为奇数时, 时间复杂度: ο ( 1 ) \omicron(1) ο(1)
n n n为偶数时, 时间复杂度: ο ( l o g 2 ( n ) ) \omicron(log2(n)) ο(log2(n))

代码实现

#include<bits/stdc++.h>
#define ll long long
using namespace std;ll x;
int main(){scanf("%lld",&x);if(x%2==1){if(x==1){printf("-1\n");return 0;}x*=x;printf("%lld %lld\n",x/2,x/2+1);}else {if(x==2){printf("-1\n");return 0;}ll cnt=0;while(x%2==0){++cnt;x/=2;}if(x!=1){x*=x;ll r=(ll)pow(2,cnt);printf("%lld %lld\n",x/2*r,(x/2+1)*r);}else{cnt-=2;ll r=(ll)pow(2,cnt);printf("%lld %lld\n",3*r,5*r);}}return 0;
}

CF707C Pythagorean Triples 题解相关推荐

  1. D. Pythagorean Triples(1487D)(打表找规律 + 二分)

    D. Pythagorean Triples(1487D)(打表找规律 + 二分) 题目来源:D. Pythagorean Triples 题意: 给定一个 n,求满足以下条件的数对 (a, b, c ...

  2. hdu2017青岛网络赛Pythagoras(Tree of primitive Pythagorean triples)

    题面: Given a list of integers a0,a1,a2,⋯,a2k−1. Pythagoras triples over 109 are all solutions of x2+y ...

  3. C. Pythagorean Triples

    链接:https://codeforces.com/problemset/problem/707/C Katya studies in a fifth grade. Recently her clas ...

  4. python:实现Pythagorean triples毕氏三元数(附完整源码)

    python:实现Pythagorean triples毕氏三元数 limit = int(input("Enter upper limit:")) c = 0 m = 2 whi ...

  5. JAVA:实现是否为Pythagorean Triples毕达哥斯拉三角数算法(附完整源码)

    JAVA:实现是否为Pythagorean Triples毕达哥斯拉三角数算法 package com.thealgorithms.maths;public class PythagoreanTrip ...

  6. D. Pythagorean Triples

    题目来源 一.题目 A Pythagorean triple is a triple of integer numbers (a,b,c) such that it is possible to fo ...

  7. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  8. D. Pythagorean Triples (math、暴力)

    题目 题解: 因此c=(a*a-1)/2+1 我们暴力枚举a,从a=1到(a * a-1)/2+1<=n (C需要<=n) 如果满足(a * a-1)%2==0,即b为整数,且c=b+1, ...

  9. Codeforces Round #368 (Div. 2) problem: (C) Pythagorean Triples

    本题就一个公式 n^2+((n^2-1)/2)^2=((n^2+1)/2)^2 0.当n==1或n==2时,不存在结果. 1.当n为奇数时此公式求得的数还是整数,成立 2.当n为偶数时分两种情况: ( ...

最新文章

  1. usaco Superprime Rib
  2. CVPR 2020:华为GhostNet,超越谷歌MobileNet,已开源
  3. 31 | 套路篇:磁盘 I/O 性能优化的几个思路
  4. 耗时6年的DK博物科普巨著,全面提升孩子的认知高度
  5. PCA主成分分析实战和可视化 | 附R代码和测试数据
  6. javascript概要
  7. php闭包原理,闭包原理及实例
  8. springMVC之与json数据交互方法
  9. 剑指offer 面试题63. 股票的最大利润
  10. 麦马计算机专业对化学的要求,2018麦克马斯特大学最新入学要求+热门专业全解析...
  11. EMVI5.3 将数据信息改为投影坐标系
  12. php phpmailer发送邮件
  13. 解决办法:error: 'size_t' does not name a type、unknown type name 'size_t'
  14. w10投影全屏设置_win10投影仪怎么铺满全屏|win10投影器全屏的设置方法
  15. 克隆虚拟机后的IP、路由配置以及mac地址冲突解决
  16. selenium 鼠标悬停事件
  17. 全球与中国iNOS 抗体市场深度研究分析报告
  18. 微软surface屏幕测试软件,【微软 Surface Pro 3 平板电脑使用感受】屏幕|软件_摘要频道_什么值得买...
  19. IntelliJ IDEA远程调试设置
  20. Symantec Backup Exec Agent For Linux防火墙问题

热门文章

  1. 腾讯云短视频服务端解决方案
  2. Java编程(3)-买飞机票
  3. Android app 上传到google play,需要设置 隐私政策声明,kotlin命令行传值
  4. 宾大计算机网络课程,【图片】CIS学员专访|那个被康奈尔、哥大、宾大、CMU等8所名校录取的女孩【康乃尔大学吧】_百度贴吧...
  5. 如何下载网页的字体图标相关文件
  6. RN新手上路-----底部导航栏图标设置
  7. 数字图像处理知识(2)
  8. genymotion 下载模拟器下n久特别慢无法下载进度为0教你用迅雷下载这些虚拟机多爽啊想暂停就暂停
  9. 基于pt100的温度测量系统设计 c语言程序 四臂电桥,基于PT100的温度测量系统设计...
  10. python创建Excel文件及写入保存数据