本原毕达哥拉斯三元组是由三个正整数x,y,z组成,且gcd(x,y,z)=1,x*x+y*y=z*z

对于所有的本原毕达哥拉斯三元组(a,b,c) (a*a+b*b=c*c,a与b必定奇偶互异,且c为奇数。这里我们设b为偶数)

则:和

a=st

b=(s*s-t*t)/2

c=(s*s+t*t)/2

其中s>t>=1且gcd(s,t)=1

是一一对应的。

看看别人得证明:

http://blog.csdn.net/loinus/article/details/7824841

看看我的证明

有了这个定理就这题就很好做了。

Fermat vs. Pythagoras
Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 1456   Accepted: 848

Description

Computer generated and assisted proofs and verification occupy a small niche in the realm of Computer Science. The first proof of the four-color problem was completed with the assistance of a computer program and current efforts in verification have succeeded in verifying the translation of high-level code down to the chip level. 
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

The 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

For 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
//
// main.cpp
// poj1305
//
// Created by 陈加寿 on 15/11/30.
// Copyright (c) 2015年 陈加寿. All rights reserved.
// #include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std; int mark[]; long long gcd(long long a,long long b)
{
if(b==) return a;
return gcd(b,a%b);
} int main(int argc, const char * argv[]) {
int n;
while(cin>>n)
{
memset(mark,,sizeof(mark));
int cnt=;
for(long long i=;;i+=)
{
long long a,b,c;
if( (i*i+(i+)*(i+))/ >n ) break; for(long long j=i+;;j+=)
{
if(gcd(i,j)==)
{
c=(i*i+j*j)/;
b=(j*j-i*i)/;
a=i*j;
if(c>n) break;
cnt++;
for(long long k=;k*c<=n;k++)
{
mark[a*k]=;
mark[b*k]=;
mark[c*k]=;
}
}
}
}
int ans=;
for(int i=;i<=n;i++)
if(mark[i]==) ans++;
cout<<cnt<<" "<<ans<<endl;
}
return ;
}

毕达哥拉斯三元组(勾股数组)poj1305的更多相关文章

  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 ...

  2. 勾股数组及其应用uva106

    勾股数组 设三元组(a,b,c)满足a^2 + b^2 = c^2的勾股数组,那么是否存在无穷多个勾股数组呢, 答案是肯定的,将三元组乘以d,可以得到新的三元组(da,db,dc) 即(da)^2 + ...

  3. URAL 2032 - Conspiracy Theory and Rebranding【本源勾股数组】

    [题意] 给出三角形的三个边长,均是10^7以内的整数,问三角形的三个角的坐标是否能均是整数,输出其中任意一个解. [题解] 一开始想的是枚举一条边的横坐标,然后通过勾股定理以及算角度求出其他点的坐标 ...

  4. Python练习题 037:Project Euler 009:毕达哥拉斯三元组之乘积

    本题来自 Project Euler 第9题:https://projecteuler.net/problem=9 # Project Euler: Problem 9: Special Pythag ...

  5. POJ 1305 Fermat vs. Pythagoras (毕达哥拉斯三元组)

    设不定方程:x^2+y^2=z^2若正整数三元组(x,y,z)满足上述方程,则称为毕达哥拉斯三元组.若gcd(x,y,z)=1,则称为本原的毕达哥拉斯三元组. 定理:正整数x,y,z构成一个本原的毕达 ...

  6. bzoj 1041: [HAOI2008]圆上的整点 本原勾股數組

    1041: [HAOI2008]圆上的整点 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2027  Solved: 853[Submit][Stat ...

  7. FZU1669 Right-angled Triangle【毕达哥拉斯三元组】

    主题链接: pid=1669">http://acm.fzu.edu.cn/problem.php?pid=1669 题目大意: 求满足以a.b为直角边,c为斜边,而且满足a + b ...

  8. hdu 3939(勾股+容斥)

    题意: 给定一个整数L(L<=1e12),计算(x,y,z)组的个数.其中x<y<z,x^2+y^2=z^2,gcd(x,y)==1,gcd(x,z)==1,gcd(y,z)==1. ...

  9. 欧拉计划之题目9:找出唯一的满足a + b + c = 1000的毕达哥拉斯三元组{a, b, c}

    本题来自:http://pe.spiritzhang.com/index.php/2011-05-11-09-44-54/10-9a--b--c--1000a-b-c #include <std ...

随机推荐

  1. 排序算法的实现(冒泡,选择,插入 O(N*N)--理解方法实现

    以前也看过很多排序算法的原理,每次都想自己实现一下,一直都再拖,现在着牛课网学习算法课程,希望自己能够坚持练习. //对于一个int数组,请编写一个选择冒泡算法,对数组元素排序. //给定一个int数 ...

  2. 《JAVA核心卷I》之Java基本程序结构

    Java基本程序结构 1.类名是以大写字母开头的名词代码能够执行的类,要有main函数,且声明必须是public 2.注释:  “//”,"/**/" 行注释  "/** ...

  3. Nginx反向代理、负载均衡及日志

    Nginx反向代理.负载均衡及日志 1.原理图   2.正向代理与反向代理 (1)代理服务器 代理服务器,客户机在发送请求时,不会直接发送给目的主机,而是先发送给代理服务器,代理服务接受客户机请求之后 ...

  4. idea 热部署

  5. 数据库中存在0,1,2.....或者1,null,2 排序时让0或者null在最后的sql语句

     select * from yryz_products_t order by isnull(sort),sort;     select * from yourtable order by cast ...

  6. Python3链接MySQL数据库

    Python 2.x 上连接MySQL的库倒是不少的,其中比较著名就是MySQLdb(Django项目都使用它:我也在开发测试系统时也使用过),见:http://sourceforge.net/pro ...

  7. 【Excle数据透视表】如何新建数据透视表样式

    如果觉得Excle给出的数据透视表样式不符合自己的心意,可以自己定义一个数据透视表样式 步骤1 单击数据透视表区域任意单元格→数据透视表工具→设计→样式组中的下拉按钮,打开数据透视表样式库→新建数据透 ...

  8. 谈一谈Http Request 与Http Response

    1.什么是HTTPRequest与HTTP Response? 我们平时打开浏览器,输入网址,点击Enter按键,然后我们想要的网页就呈现在我们的眼前,可是这个过程是怎么实现的呢? 简单来说是这样的: ...

  9. 正则化--L2正则化

    请查看以下泛化曲线,该曲线显示的是训练集和验证集相对于训练迭代次数的损失. 图 1 显示的是某个模型的训练损失逐渐减少,但验证损失最终增加.换言之,该泛化曲线显示该模型与训练集中的数据过拟合.根据奥卡 ...

  10. c#利用委托传递函数参数(1)

    本次旨在解决 同参不同名 的函数作为参数传递的情况 情景: 一下两个函数分别多次重复调用了两个同参不同名的函数(实际上总共有3个这样的函数),函数结构基本相同,只有调用的函数名不一样,显然可以整合在一 ...