题目来源: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 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 for n > 2.

The Problem

Given a positive integer N, you are to write a program that computes two quantities regarding the solution of

where xy, 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<yz, and they are relatively prime, i.e., have no common divisor larger than 1. You are also to compute the number of values such that p is not part of any triple (not just relatively prime triples).

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

The 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  ). The second number is the number of positive integers  that are not part of any triple whose components are all  . There should be one output line for each input line.

Sample Input

10
25
100

Sample Output

1 4
4 9
16 27
解题思路:

这是一道数论题,用数学的语言描述就是:x, y, z∈N,给定一个数n,找出所有的x, y, z ≤ n,使得x2 + y2 = z2成立。如果要穷举所有的x, y, z的话,按照题目所给的数据量,肯定是无法在限定时间内完成的。考虑利用毕达哥拉斯数的性质生成所有的x, y, z来解决,数学推导简要介绍如下:

先假定x, y, z两两互质,由于x, y互质,故x, y中至少有1个是奇数。下面用反证法证明x和y中有且只有1个奇数。假定x, y都为奇数,设:

  • x = 2a + 1
  • y = 2b + 1
  • x2 + y2 = (2a + 1)2 + (2b + 1)2 
    = 4(a2 + b2 + a + b) + 2

又因为x2和y2是奇数,则z2是偶数,且必能被4整除,与上式矛盾,因此x, y中只有一个奇数。

假设x为奇数,y为偶数,则z为奇数,2z与2x的最大公因数为2,2z和2x可分别写作

  • 2z = (z + x) + (z - x)
  • 2x = (z + x) - (z - x)

那么跟据最大公因数性质,z + x和z - x的最大公因数也为2,又因为:

  • (z + x)(z - x) = y2,两边同除以4得:
    ((z + x) / 2)((z - x) / 2) = (y / 2)2

故可令:

  • z + x = 2m2, z - x = 2n2
    其中z = m + n, x = m - n(m与n互质)

则有:

  • y2 = z2 - x2 = 2m22n2 = 4m2n2
    即y = 2mn。

综上所述,可得到下式:

  • x = m2 - n2, y = 2mn, z = m2 + n2. (m, n为任意自然数)

这里还有一个问题:题目要求统计(x, y, z)三元组的数量时只统计x,y和z两两互质的的情况,这个问题用上面的算法就可以解决了。但对于统计p的数量,题目并不限定三元组是两两互质的。但是上式不能生成所有x, y, z并不是两两互质的情况。然而假设x与y最大公因数w不为1,则z也必能被w整除,因此w为x, y, z三个数的公因数。归纳总结可知,所有非两两互质的x0, y0, z0都可由一组互质的x, y, z乘以系数得到。根据以上理论就可以快速的求解了。


参考代码:
 #include <cstdio>
#include <cmath>
#include <cstring>
#define N 1000010
bool used[N]; long long gcd(long long a , long long b)
{ return b== ? a: gcd(b,a%b); } int main()
{
long long n,a,b,c;
long long count1,count2;
while(scanf("%lld",&n)!=EOF)
{
count1=count2=;
memset(used,,sizeof(used));
long long m=(long long)sqrt(n+0.5);
for(long long t=; t<=m; t+=)
for(long long s=t+; s*t<=n; s+=)
if(gcd(s,t)==) //s>t>=1且s与t互质
{
a=s*t; //奇数
b=(s*s-t*t)/; //偶数
c=(s*s+t*t)/; //奇数
if(c<=n) //在n范围内的PPT
{
count1++;
//printf("本原勾股数组:%lld %lld %lld\n",a,b,c);
if(!used[a]) { count2++; used[a]=; }
if(!used[b]) { count2++; used[b]=; }
if(!used[c]) { count2++; used[c]=; } for(int j=; c*j<=n; j++) //j是倍数
{
if(!used[a*j]) { count2++; used[a*j]=; }
if(!used[b*j]) { count2++; used[b*j]=; }
if(!used[c*j]) { count2++; used[c*j]=; }
}
}
}
printf("%lld %lld\n",count1,n-count2);
}
return ;
}

UVa 106 - Fermat vs Pythagoras(数论题目)的更多相关文章

  1. Uva 106 - Fermat vs. Pythagoras 解题报告

    数论题,考查了本原勾股数(PPT) 对一个三元组(a,b,c)两两互质 且满足 a2 + b2 = c2 首先有结论 a 和 b 奇偶性不同 c总是奇数(可用反证法证明,不赘述) 设 a为奇数 b为偶 ...

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

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

  3. 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. uva 11246 - K-Multiple Free set(数论)

    题目链接:uva 11246 - K-Multiple Free set 题目大意:给定n,k.求一个元素不大于n的子集,要求该子集的元素尽量多,而且不含两个数满足a∗k=b. 解题思路:容斥原理.f ...

  5. uva 11300 - Spreading the Wealth(数论)

    题目链接:uva 11300 - Spreading the Wealth 题目大意:有n个人坐在圆桌旁,每个人有一定的金币,金币的总数可以被n整除,现在每个人可以给左右的人一些金币,使得每个人手上的 ...

  6. UVA 10622 - Perfect P-th Powers(数论)

    UVA 10622 - Perfect P-th Powers 题目链接 题意:求n转化为b^p最大的p值 思路:对n分解质因子,然后取全部质因子个数的gcd就是答案,可是这题有个坑啊.就是输入的能够 ...

  7. UVA 11426 - GCD - Extreme (II) (数论)

    UVA 11426 - GCD - Extreme (II) 题目链接 题意:给定N.求∑i<=ni=1∑j<nj=1gcd(i,j)的值. 思路:lrj白书上的例题,设f(n) = gc ...

  8. UVA 1426 - Discrete Square Roots(数论)

    UVA 1426 - Discrete Square Roots 题目链接 题意:给定X, N. R.要求r2≡x (mod n) (1 <= r < n)的全部解.R为一个已知解 思路: ...

  9. Uva - 12050 Palindrome Numbers【数论】

    题目链接:uva 12050 - Palindrome Numbers 题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然 ...

随机推荐

  1. 介绍WEB站点结构

    在这节里,我们将抛开Umbraco来看看已创建的站点.在我们介绍Umbraco之前,需要了解站点是如何工作的,如何使用使用浏览器工具. 我们看到在标签顶端的内容叫做页面标题.每个页面的标题都会改变表示 ...

  2. ASP.NET MVC数组模型绑定

    在ASP.NET MVC中使用Razor语法可以在视图中方便地展示数组,如果要进行数组模型绑定,会遇到索引断裂问题,如下示例: <input type="text" name ...

  3. SQL Server里ORDER BY的歧义性

    在今天的文章里,我想谈下SQL Server里非常有争议和复杂的话题:ORDER BY子句的歧义性. 视图与ORDER BY 我们用一个非常简单的SELECT语句开始. -- A very simpl ...

  4. UWP开发入门(十八)——使用ContentControl减少页面元素数量

    我们今天学习一下ContentControl,主要介绍如何使用ContentControl搭配DataTemplate来进行界面的复用,以及通过ContentTemplateSelector进一步减少 ...

  5. Java集合框架源码剖析:LinkedHashSet 和 LinkedHashMap

    Java LinkedHashMap和HashMap有什么区别和联系?为什么LinkedHashMap会有着更快的迭代速度?LinkedHashSet跟LinkedHashMap有着怎样的内在联系?本 ...

  6. sprint3 总结

    sprint3 本次的主要任务是找项目中的bug,并与客户不断地沟通以满足客户的要求.队友主要负责找项目中的bug或提出一些建议.我主要是负责与客户沟通和修复bug.总的来说进展还算顺利. 团队贡献分 ...

  7. P6 EPPM R16.1安装与配置指南(三)

    P6 EPPM R16.1安装与配置指南(三) 解压:V137390-01.zip 修改 D:\P6_R161\p6suite\database\dbsetup.bat   的行 SET JAR_FI ...

  8. java 接口学习

    你应该知道接口是一种契约,它与实现方式无关 但是类,即使是抽象类,你都能自定义成员变量,而成员变量往往就与实现方式有关. 这一点的实际意义不大. 但是有一点,类会暴露太多不必要,甚至不能暴露的东西,你 ...

  9. VS 自定义新建文件模板方法

    自定义新建文件模板方法     VS 2010 及VS2008 自定义模板的方法如下: 结合VS工具,其下的插件也层出不穷.今天重点给大家介绍如何使用VS2010自定义新建文件模版,新建文件时,添加个 ...

  10. 内核移植和文件系统制作(4):UBIFS根文件系统制作总结

    UBIFS文件系统简介: 无排序区块图像文件系统(UnsortedBlock Image File System, UBIFS)是用于固态硬盘存储设备上,并与LogFS相互竞争,作为JFFS2的后继文 ...