数论(毕达哥拉斯定理):POJ 1305 Fermat vs. Pythagoras
| Time Limit: 2000MS | Memory Limit: 10000K | |
| Total Submissions: 1493 | Accepted: 865 |
Description
This problem deals with computing quantities relating to part of
Fermat's Last Theorem: that there are no integer solutions of a^n + b^n =
c^n for n > 2.
Given a positive integer N, you are to write a program that computes
two quantities regarding the solution of x^2 + y^2 = z^2, where x, y,
and z are constrained(驱使)
to be positive integers less than or equal to N. You are to compute the
number of triples (x,y,z) such that x < y < z, and they are
relatively prime, i.e., have no common divisor(除数)
larger than 1. You are also to compute the number of values 0 < p
<= N such that p is not part of any triple (not just relatively prime
triples).
Input
input consists of a sequence of positive integers, one per line. Each
integer in the input file will be less than or equal to 1,000,000. Input
is terminated by end-of-file
Output
each integer N in the input file print two integers separated by a
space. The first integer is the number of relatively prime triples (such
that each component of the triple is <=N). The second number is the
number of positive integers <=N that are not part of any triple whose
components are all <=N. There should be one output line for each
input line.
Sample Input
10
25
100
Sample Output
1 4
4 9
16 27
题意:给定一个n,输出三元组(a,b,c)其中GCD(a,b,c)=1,且a²+b²=c²,以及1~n中没有在任何一个三元组中出现过的数的个数。
这道题需要知道勾股数的性质。
最最开始GCD(a,b,c)=1 ==> a,b,c两两互质,通过a²+b²=c²易证。
首先,对于一组勾股数,①a与b的奇偶性不同,②c一定为奇数。
证明①:若a与b同为偶数,则c也为偶数,与GCD(a,b,c)=1矛盾;若a与b同为奇数,c一定为偶数,设a=2*i+1,b=2*j+1,c=2*k -> a²+b²=c²->2*i²+2*i+2*j²+2*j+1=2*k²,这个式子是矛盾的。
证明②:a,b一奇一偶,显然。
然后将 a²+b²=c² 变形,b为偶数时,a²=(c+b)*(c-b),容易发现c-b与c+b互质,所以c-b与c+b都是平方数,设x²=c-b,y²=c+b,得a=x*y,b=(y²-x²)/2,c=(y²+x²)/2.
易得x,y都为奇数,直接枚举x,y就好了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn=;
bool vis[maxn];
long long Gcd(long long a,long long b){
return b?Gcd(b,a%b):a;
}
int main(){
int n,m,ans,tot;
while(~scanf("%d",&n)){
m=(int)sqrt(n+0.5);ans=tot=;
memset(vis,,sizeof(vis));
for(int t=;t<=m;t+=)
for(int s=t+;(s*s+t*t)/<=n;s+=)
if(Gcd(s,t)==){
int a=s*t;
int b=(s*s-t*t)/;
int c=(s*s+t*t)/
ans++;
for(int k=;k*c<=n;k++){
vis[k*a]=true;
vis[k*b]=true;
vis[k*c]=true;
}
}
for(int i=;i<=n;i++)
if(!vis[i])
tot++;
printf("%d %d\n",ans,tot);
}
}
数论(毕达哥拉斯定理):POJ 1305 Fermat vs. Pythagoras的更多相关文章
- POJ 1305 Fermat vs. Pythagoras (毕达哥拉斯三元组)
设不定方程:x^2+y^2=z^2若正整数三元组(x,y,z)满足上述方程,则称为毕达哥拉斯三元组.若gcd(x,y,z)=1,则称为本原的毕达哥拉斯三元组. 定理:正整数x,y,z构成一个本原的毕达 ...
- UVa 106 - Fermat vs Pythagoras(数论题目)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- CPC23-4-K. 喵喵的神数 (数论 Lucas定理)
喵喵的神∙数 Time Limit: 1 Sec Memory Limit: 128 MB Description 喵喵对组合数比較感兴趣,而且对计算组合数很在行. 同一时候为了追求有后宫的素养的生活 ...
- acm数论之旅--数论四大定理
ACM数论之旅5---数论四大定理(你怕不怕(☆゚∀゚)老实告诉我) (本篇无证明,想要证明的去找度娘)o(*≧▽≦)ツ ----------数论四大定理--------- 数论四大定理: 1.威 ...
- Fermat vs. Pythagoras POJ - 1305 (数论之勾股数组(毕达哥拉斯三元组))
题意:(a, b, c)为a2+b2=c2的一个解,那么求gcd(a, b, c)=1的组数,并且a<b<c<=n,和不为解中所含数字的个数,比如在n等于10时,为1, 2, 7,9 ...
- 【威佐夫博奕】 betty定理 poj 1067
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- 数论问题(1) : poj 1061
最近,本人发现了一个新网站poj(不算新) 当然了,上面的资源很好...... 就是还没搞清楚它的搜索该怎么弄,如果有大佬能教教我怎么弄,请在下方留言 闲话少说,回归我们的正题 题目转自poj 106 ...
- poj1305 Fermat vs. Pythagoras(勾股数)
题目传送门 题意: 设不定方程:x^2+y^2=z^2若正整数三元组(x,y,z)满足上述方程,则称为毕达哥拉斯三元组.若gcd(x,y,z)=1,则称为本原的毕达哥拉斯三元组. 定理:正整数x,y, ...
- 组合数学 - 波利亚定理 --- poj : 2154 Color
Color Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7873 Accepted: 2565 Description ...
随机推荐
- JQ 让光标在文本框最末尾
function setFocus() { //文本末尾获得焦点 var obj = event.srcElement; var txt = obj.createTextRange(); txt.mo ...
- reactjs 入门
地址搜集:http://www.cocoachina.com/webapp/20150604/12035.html class 属性需要写成 className ,for 属性需要写成 htmlFor ...
- mysql - 初探
1,查询所有数据库名称: show databases; 2,查询所有表: use database_name; show tables; 3,查询表中的所有字段: desc table_name;
- PHP上传原理及应用
概要 1.FORM表现enctype属性 2.$_FILES系统函数 3.move_uploaded_file函数 4.is_uploaded_file函数 1.FORM标签的enctype属性 只有 ...
- 平衡搜索树(一) AVL树
AVL树 AVL树又称为高度平衡的二叉搜索树,是1962年有俄罗斯的数学家G.M.Adel'son-Vel'skii和E.M.Landis提出来的.它能保持二叉树的高度 平衡,尽量降低二叉树的高度,减 ...
- 【HDU4391】【块状链表】Paint The Wall
Problem Description As a amateur artist, Xenocide loves painting the wall. The wall can be considere ...
- js作用域与作用域链
一直对Js的作用域有点迷糊,今天偶然读到JavaScript权威指南,立马被吸引住了,写的真不错.我看的是第六版本,相当的厚,大概1000多页,Js博大精深,要熟悉精通需要大毅力大功夫. 一:函数作用 ...
- 关于css float 属性以及position:absolute 的区别。
1.float 属性定义元素在哪个方向浮动.以往这个属性总应用于图像,使文本围绕在图像周围,不过在 CSS 中,任何元素都可以浮动.浮动元素会生成一个块级框,而不论它本身是何种元素.div一个典型的块 ...
- 自动化工具word文档批量转html
企业有很多的科室,科室的每个人或多或少都会写一些文档,有些文档领导需要浏览,解决的办法是将编辑的文档打印出来,供领导浏览,或是为了节约企业成本,文档就在人与人这间或部门之间copy过来,copy过去. ...
- nodejs版本控制
本方法基于https://segmentfault.com/a/1190000004855835修改 配置: 使用的nvmw的git 地址https://github.com/hakobera/nvm ...