uva106 - Fermat vs. Pythagoras 互素勾股数

 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 oftex2html_wrap_inline29 forn > 2.

The Problem

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

displaymath22

where x, y, and z are constrained to be positive integers less than or equal toN. You are to compute the number of triples (x,y,z) such thatx<y< z, and they are relatively prime, i.e., have no common divisor larger than 1. You are also to compute the number of valuestex2html_wrap_inline51 such thatp 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 istex2html_wrap_inline57 ). The second number is the number of positive integerstex2html_wrap_inline57 that are not part of any triple whose components are alltex2html_wrap_inline57 . There should be one output line for each input line.

Sample Input

10
25
100

Sample Output

1 4
4 9
16 27


  给一个N,求出满足x^2+y^2=z^2,x<y<z<=N,且x,y,z互素这样的x,y,z有多少对,还要求1到N有多少数没有出现在x^2+y^2=z^2,x<y<z<=N,x,y,z不一定互素。

  因为x,y,z互素,所以x,y不可能都是偶数。

  假设x,y都是奇数,设x=2*a+1,y=2*b+1,x^2是奇数,y^2是奇数,则z^2是偶数,z是偶数。因为偶数的平方一定能被4整除,但是z^2=x^2+y^2=4(a² + b² + a + b) + 2 不能被4整除,所以x,y不可能都是奇数。

  于是x,y只能是一奇一偶,z只能是奇数,设x为奇数,y为偶数。则z+x和z-x都是偶数。设z+x=2*u,z-x=2*v。

  z=u+v

  x=u-v

  因为z和x互素,所以u和v也一定互素(假设u和v不互素,u=w*k1,v=w*k2,z=w*(k1+k2),x=w*(k1-k2),z和x不互素,矛盾).。

  把z和x代入x^2+y^2=z^2得,y^2=4*u*v,也就是(y/2)^2=u*v。由于u和v互素,乘积又是一个平方数,所以u和v都是一个平方数(因为(u,v)=1,u=u*(u,v)=(u^2,uv)=(u^2,(y/2)^2)=(u,y/2)^2,u是平方数,同理v也是平方数)。

  设u=a^2,v=b^2,a,b互素并且是一奇一偶(若a,b不互素,则u,v不互素,矛盾。由于u,v互素,则至少其中一个是奇数,又因为y是偶数,所以u*v是偶数,所以u,v一奇一偶,奇数的平方是奇数,偶数的平方是偶数,所以a,b也一奇一偶)。

  最后就有 z=a^2+b^2,x=a^2-b^2,y=2*a*b (a,b互素,一奇一偶,a>b)。

  第二问只要把第一问求出的三元组的倍数都标记就行了,因为勾股定理的三个数要么两两互素,要么两两都不互素,所以不互素的都是互素的倍数得来的。

 

#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
int vis[1000010];
int gcd(int x,int y){
    return x%y?gcd(y,x%y):y;
}
int main(){
   // freopen("in.txt","r",stdin);
    int N;
    while(scanf("%d",&N)!=EOF){
        memset(vis,0,sizeof(vis));
        int L=(floor)(sqrt(N));
        int i,j,k,x,y,z,ans1=0,ans2=0;
        for(i=1;i<=L;i++)
        for(j=i+1;j<=L;j+=2){
            if((z=i*i+j*j)<=N&&gcd(i,j)==1){
                ans1++;
                x=j*j-i*i;
                y=2*i*j;
                for(k=1;k*z<=N;k++) vis[x*k]=vis[y*k]=vis[z*k]=1;
            }
        }
        for(i=1;i<=N;i++) if(!vis[i]) ans2++;
        printf("%d %d\n",ans1,ans2);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值