Fermat vs. Pythagoras
Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 1493   Accepted: 865

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
  
  题意:给定一个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的更多相关文章

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

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

  2. UVa 106 - Fermat vs Pythagoras(数论题目)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  3. CPC23-4-K. 喵喵的神数 (数论 Lucas定理)

    喵喵的神∙数 Time Limit: 1 Sec Memory Limit: 128 MB Description 喵喵对组合数比較感兴趣,而且对计算组合数很在行. 同一时候为了追求有后宫的素养的生活 ...

  4. acm数论之旅--数论四大定理

    ACM数论之旅5---数论四大定理(你怕不怕(☆゚∀゚)老实告诉我)   (本篇无证明,想要证明的去找度娘)o(*≧▽≦)ツ ----------数论四大定理--------- 数论四大定理: 1.威 ...

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

  6. 【威佐夫博奕】 betty定理 poj 1067

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  7. 数论问题(1) : poj 1061

    最近,本人发现了一个新网站poj(不算新) 当然了,上面的资源很好...... 就是还没搞清楚它的搜索该怎么弄,如果有大佬能教教我怎么弄,请在下方留言 闲话少说,回归我们的正题 题目转自poj 106 ...

  8. poj1305 Fermat vs. Pythagoras(勾股数)

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

  9. 组合数学 - 波利亚定理 --- poj : 2154 Color

    Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7873   Accepted: 2565 Description ...

随机推荐

  1. Session 原理

    Session天天用,但是你真的理解了么? 今天遇到了这个问题,于是研究了一下.要解决这个问题,首先就要明白一些Session的机理.Session在服务器是以散列表形式存在的,我们都知道Sessio ...

  2. js--小结⑦---格式转换

  3. jar包的生成及运行

    Hello, 大家好,我们见面了,今天是2015年7月30日,我在青岛,你好吗? 这里总结下刚学习到的jar包的生成和运行,网上的资料一搜一大片,我这里总结下适用的 一:jar包的生成: 1:命令行, ...

  4. ST3破解命令

      open terminal and input it!   printf '\x39' | dd seek=$((0x6f35)) conv=notrunc bs=1 of=/Applicatio ...

  5. 获取IP所在地

    $source=file_get_contents('http://www.ip138.com/ips138.asp?ip='.$ip.'&action=2'); preg_match_all ...

  6. 强制关闭myeclipse出现的问题

    重启时,可能会出现打不开关闭前所在的workspace.其他workspace可以正常打开. 今天遇到这个问题,以前就遇到过,但是忘记如何解决了.今天在我等了十多分钟后,神奇的myeclipse自己起 ...

  7. Orace数据库锁表的处理与总结<摘抄与总结二>

    当Oracle数据库发生TX锁等待时,如果不及时处理常常会引起Oracle数据库挂起,或导致死锁的发生,产生ORA-60的错误. TX锁等待的分析 Oracle数据库中一般使用行级锁. 当Oracle ...

  8. [总结]RTMP流媒体技术零基础学习方法

    本文主要总结一些我在学习RTMP流媒体技术过程中积累的经验.也为后来学习RTMP流媒体技术的人们一个参考.本文力图从简到难,循序渐进的介绍RTMP流媒体技术的方方面面,先从应用说起,逐步深化剖析相关工 ...

  9. OC - 26.CAAnimationGroup

    概述 简介 CAAnimationGroup又称组动画或动画组 将多个动画放到动画组中,并赋值给layer的animations属性,动画组中所有动画就会并发执行 注意事项 动画组中的动画不会被压缩, ...

  10. POJ 1236.Network of Schools (强连通)

    首先要强连通缩点,统计新的图的各点的出度和入度. 第一问直接输出入度为0的点的个数 第二问是要是新的图变成一个强连通图,那么每一个点至少要有一条出边和一条入边,输出出度和入度为0的点数大的那一个 注意 ...