数论(毕达哥拉斯定理):POJ 1305 Fermat vs. Pythagoras

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)其中a,b,c两两互质,且a²+b²=c²,以及1~n中没有在任何一个三元组中出现过的数的个数。  这道题需要知道勾股数的性质。  首先,对于一组勾股数,①a与b的奇偶性不同,②c一定为奇数。  证明①:若a与b同为偶数,则c也为偶数,与a,b,c两两互质矛盾;若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² 变形,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就好了。

  
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cmath>
 5 using namespace std;
 6 const int maxn=1000010;
 7 bool vis[maxn];
 8 long long Gcd(long long a,long long b){
 9     return b?Gcd(b,a%b):a;
10 }
11 int main(){
12     int n,m,ans,tot;
13     while(~scanf("%d",&n)){
14         m=(int)sqrt(n+0.5);ans=tot=0;
15         memset(vis,0,sizeof(vis));
16         for(int t=1;t<=m;t+=2)
17             for(int s=t+2;(s*s+t*t)/2<=n;s+=2)
18                 if(Gcd(s,t)==1){
19                     int a=s*t;
20                     int b=(s*s-t*t)/2;
21                     int c=(s*s+t*t)/2
22                     ans++;
23                     for(int k=1;k*c<=n;k++){
24                         vis[k*a]=true;
25                         vis[k*b]=true;
26                         vis[k*c]=true;
27                     }
28                 }
29         for(int i=1;i<=n;i++)
30             if(!vis[i])
31                 tot++;
32         printf("%d %d\n",ans,tot);
33     }
34 }

  
时间: 2024-10-12 21:27:40

数论(毕达哥拉斯定理):POJ 1305 Fermat vs. Pythagoras的相关文章

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

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=42  Fermat vs. Pythagoras  Background Computer generated and assisted proofs and verification occupy a small niche in the realm

【数论四大定理】

威尔逊定理.欧拉定理.孙子定理.费马小定理并称数论四大定理. [威尔逊定理] 当且仅当p为素数时:( p-1 )! ≡ -1(mod p): 即:如果p为合数,( p-1 )! mod p 答案为0:如果p为素数,那么由威尔逊定理可得( p-1 )! mod p的答案为n−1; [欧拉定理(费马-欧拉定理)] 若n, a为正整数,且n, a互质,则:aφ(n) ≡ 1 (mod n) 欧拉函数φ(n) 表示与 n互素且不超过n的正整数的个数: [费马小定理(Fermat Theory)] 若a是

poj 1305

Fermat vs. Pythagoras Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 1450   Accepted: 846 Description Computer generated and assisted proofs and verification occupy a small niche in the realm of Computer Science. The first proof of the

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

喵喵的神?数 Time Limit: 1 Sec Memory Limit: 128 MB Description 喵喵对组合数比較感兴趣,而且对计算组合数很在行. 同一时候为了追求有后宫的素养的生活,喵喵每天都要研究 质数. 我们先来复习一下什么叫做组合数.对于正整数P.T 然后我们再来复习一下什么叫质数.质数就是素数,假设说正整数N的约数仅仅有1和它本身,N就是质数:另外. 1不是质数. 今天,喵喵想要知道 Input 输入第一行是一个整数N(N<=1000). 接下来N行,每行包含一个正整

uva 10843 - Anne&#39;s game(数论cayley定理)

题目链接:uva 10843 - Anne's game 题目大意:给出n,问说有n个节点构成的标号树有多少种. 解题思路:cayley定理的躶题. #include <cstdio> #include <cstring> typedef long long ll; const ll MOD = 2000000011; ll Pow (ll x, ll n) { if (n < 0) return 1; ll ans = 1; while (n) { if (n&1)

数论四大定理

威尔逊定理.欧拉定理.孙子定理.费马小定理并称数论四大定理. 威尔逊定理 若p为质数,则p可整除(p-1)!+1. 欧拉定理 欧拉定理,也称费马-欧拉定理. 若n,a为正整数,且n,a互素,即gcd(a,n) = 1,则 a^φ(n) ≡ 1 (mod n) 孙子定理 孙子定理,又称中国剩余定理. 公元前后的<孙子算经>中有“物不知数”问题:“今有物不知其数,三三数之余二 ,五五数之余三 ,七七数之余二,问物几何?”答为“23”. 明朝程大位用歌谣给出了该题的解法:“三人同行七十稀,五树梅花廿

数论(Lucas定理) HDOJ 4349 Xiao Ming&#39;s Hope

题目传送门 题意:求C (n,0),C (n,1),C (n,2)...C (n,n)中奇数的个数 分析:Lucas 定理:A.B是非负整数,p是质数.AB写成p进制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0].则组合数C(A,B)与C(a[n],b[n])*C(a[n-1],b[n-1])*...*C(a[0],b[0])  mod p同.即:Lucas (n,m,p)=C (n%p,m%p) * Lucas (n/p,m/p,p)  我是打表找规律的,就是

数论篇5——数论四大定理

数论四大定理: 威尔逊定理 欧拉定理 孙子定理(中国剩余定理) 费马小定理 1.威尔逊定理 在初等数论中,威尔逊定理给出了判定一个自然数是否为素数的充分必要条件. 当且仅当$p$为素数时 $(p-1)!\equiv-1(mod\ p)$ 简单点说就是,若$p$为质数,则$p$能被 $(p-1)!+1$ 整除 但是由于阶乘是呈爆炸增长的,其结论对于实际使用不太多. 证明 首先,可以明确 $(p-1)\equiv-1(mod\ p)$ 根据同余式的性质,我们只需要证明 $(p-2)!\equiv1(

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则输出4. 好了!把所用知识点说一下: 数论之勾股数组(毕达哥拉斯三元组) 本原勾股数组(a,b,c)(a为奇数,b偶数)都可由如下公式得出:a=st,b=(s2-t2)/2, c = (s2+t2)/2, 其中s>t>=1是没有公因数的奇数. 再把勾股数公式拿过来: 套路一: 当a为大于1的奇数