1480-The Gougu Theorem


                Time Limit: 2 Sec  Memory Limit: 32 MB

Description
Pythagorean Theorem is “humanity’s greatest scientific discoveries of the ten” is a basic elementary geometry theorems. “This theorem has a very long history, almost all ancient civilizations (Greece, China, Egypt, Babylon, India, etc.) have studied this theorem. Pythagorean Theorem in the West known as the Pythagorean Theorem, are said to Ancient Greek mathematician and philosopher Pythagoras (Pythagoras, BC572 ~ BC497) was first discovered in BC550.

Pythagorean Theorem in China known as the Gougu/勾股 Theorem. Around 1100 BC, the Western Zhou period, the ancient Chinese mathematician, Shanggao, first described the Gougu Theorem. In the famous ancient mathematics book, 《九章算术》,the proof was given (left). The Gougu Theorem is that “in the right triangle, the sum of the squares of two right sides is equal to the square of the hypotenuse.” In other words, the three sides (a, b, c) of right-angled triangle satisfies the following equation:

a2+b2=c2
Where a is called 勾/Gou, b is 股/Gu, and c is 弦/Xian.

For given c, how many different positive integer solutions are there? ((a,b,c)are relatively-prime. ) It is an interesting problem.

Now, your task is to solve it.

Input
There are several test cases; each test case contains one positive integer c (0 < c < 230 ) in a line. c = 0 is the end of input and need not to process.

Output
For each test case, your program should output by following format:

Case #:
There are n solution(s).
a^2 + b^2 = c^2
…..(total n line(s) )….

Where # is the test case number starting from 1, two consecutive cases should be separated by a single blank line.

For each given c, output all solutions satisfying: a2+b2=c2

Each solution should be in one line, and in order a

Sample Input
65
20
0
Sample Output
Case 1:
There are 2 solution(s).
16^2 + 63^2 = 65^2
33^2 + 56^2 = 65^2

Case 2:
There are 0 solution(s).

题目链接:HZNU-1480

题目大意:给出一个c,问满足a^2+b^2=c^2的(a,b)有几对,并且a,b,c三者互质。

题目思路:

勾股数的一个性质。

设a,b,c是直角三角形的三条边,a,b是直角边,c是斜边,则有:
a = 2 * m * n;
b = m^2 - n^2;
c = m^2 + n^2;
该公式只能求出基本的勾股数,无法求出派生勾股数,如6,8,10无法求出。

还需要注意两点:
1)由于两边之和要大于第三边,所以a*m*n+m^2-m^2 > m^2+n^2.即 m > n
2)a,b,c必须互质,否则无法得到派生的勾股数

另外输出注意,从小到大输出!!(WA了2个小时/(ㄒoㄒ)/~~)

以下是代码:

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
#define ll long long
ll c;
struct node
{ll a,b;friend bool operator<( node x, node y ){return x.a<y.a;}
}p[42770];
ll gcd(ll a, ll b)
{    return b == 0 ? a : gcd(b, a%b);
}
bool cmp(node x,node y)
{return x.a < y.a;
}
int main()
{int ok = 0;int loy = 1;while(cin >> c && c){if (ok) cout << endl;ok = 1;printf("Case %d:\n",loy++);int k = 0;ll len = (ll)sqrt(c + 0.5);for (ll m = 1;m <= len;m++){ll n = sqrt(c - m * m + 0.5);ll a = 2 * n * m;ll b = abs(m * m - n * n);ll g = gcd(gcd(a,b),c);if(m < n && m * m  + n * n  == c && g == 1){p[k].a = min(a,b);p[k++].b = max(a,b);}}printf("There are %d solution(s).\n",k);sort(p,p + k, cmp);for (int i = 0; i < k; i++){printf("%lld^2 + %lld^2 = %lld^2\n",p[i].a,p[i].b,c);}}return 0;
}

HZNU-1480-The Gougu Theorem【勾股数】相关推荐

  1. 数论概论学习笔记(一)——勾股数

    版权声明:本文为博主原创文章,未经博主允许不得转载. Pythagoras theorem(勾股定理) 一个直角三角形中,两个直角边边长的平方加起来等于斜边长的平方. 如果设直角三角形的两条直角边长度 ...

  2. C语言 · 勾股数

    勾股数 勾股定理,西方称为毕达哥拉斯定理,它所对应的三角形现在称为:直角三角形. 已知直角三角形的斜边是某个整数,并且要求另外两条边也必须是整数. 求满足这个条件的不同直角三角形的个数. [数据格式] ...

  3. 【c语言】蓝桥杯算法提高 勾股数

    问题描述 勾股数是一组三个自然数,a < b < c,以这三个数为三角形的三条边能够形成一个直角三角形 输出所有a + b + c <= 1000的勾股数 a小的先输出:a相同的,b ...

  4. python【蓝桥杯vip练习题库】ADV-187 勾股数

    试题 算法提高 勾股数 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 勾股数是一组三个自然数,a < b < c,以这三个数为三角形的三条边能够形成一个直角三角形 输出 ...

  5. 将整数拆分为勾股数的问题解决

    在群里看到这样一个问题: 解法如下: 1 #include <stdio.h> 2 3 #define MAX 500000 4 unsigned g_array[MAX + 1] = { ...

  6. I00040 计算1000以内的勾股数

    对于三元组<a, b, c>,若满足a^2+b^2=c^2,则称该三元组为勾股数. 可以假定a<b<c,在这个前提下,用穷举法来求解该问题.并且假定a<b<c< ...

  7. 互为质数的勾股数c语言,c语言 寻求勾股数满足x2+y2=z2的正整数x,y和z称为一组勾股数(pythagorean...

    满意答案 ktjw7387 2014.12.11 采纳率:54%    等级:10 已帮助:1016人 # include "stdio.h" # include "ma ...

  8. 数学--数论--Find Integer(勾股数定理)

    Problem Description people in USSS love math very much, and there is a famous math problem give you ...

  9. c语言使用循环编写勾股数,刘徽《九章算术》中的勾股数

    若A.B.C为满足A2+B2=C2的正整数.我国古代数学书<周髀算经>曾经提到"勾广三,股修四,径偶五"这三个边都是正整数的直角三角形.在公元263年时,我国数学家:刘 ...

  10. 三个数差的平方公式推导过程_勾股数公式的简单推导

    勾股数 是指满足 的正整数,它们的通用公式为 ,下边我从定义出发,利用平方差公式举例实验找规律,推导出这一通用公式. 由 可知 当 为奇数时 和 全都是奇数:当 为偶数时 和 全都是偶数.( ,与 同 ...

最新文章

  1. python快速入门第三版-Python 快速入门:第3版 配套资源 PDF 完整版
  2. Careercup - Google面试题 - 5377673471721472
  3. jQuery 操作大全
  4. 初等代数(1):数的分类、基本运算规律、乘法及其因式分解公式、公式、比例、根式
  5. MusicXML 3.0 (4) - 谱号
  6. 控制dcom程序使用端口_使用VS Code调试.net控制台应用程序的方法
  7. 博客园背景设置CSS代码
  8. 使用flot绘图出现window.G_vmlCanvasManager is null or not an Object
  9. Redis集群搭建使用
  10. android常用网址
  11. python 关联矩阵_创建关联矩阵
  12. 医疗人工智能与未来医院信息化建设
  13. Java 在Word指定段落/文本位置插入分页符
  14. Oracle 时间差计算
  15. 计算机学院 拔河比赛加油词,校园拔河比赛加油稿
  16. 到底该不该用RTOS,这篇文章给你答案!
  17. Start Developing iOS Apps Today
  18. java获取分贝_android 声音强度、分贝 大小检测 源码下载(as版)
  19. autoCAD 符号表
  20. 转载:非常实用的15款开源PHP类库

热门文章

  1. 【EXLIBRIS】#小词旮旯#
  2. 【EXLIBRIS】#小词旮旯# 006 Wake
  3. Android开发常见问题汇总
  4. 组装http报文调用小黄鸡网页端消息接口
  5. Gerrit 工作流程及简单使用
  6. 用计算机写短文教学反思,《阿西莫夫短文两篇》教学反思6则
  7. python pil image_Python PIL的Image模块
  8. 数据结构习题及解析三
  9. 真假马云Deciphering Jack Ma
  10. java 聊天室 私聊_Java聊天室——实现多人聊天、私聊、群聊