zoukankan      html  css  js  c++  java
  • 数论(毕达哥拉斯定理):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)其中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就好了。

      
     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
    ",ans,tot);        
    33     }
    34 }


      
    尽最大的努力,做最好的自己!
  • 相关阅读:
    ubuntu下在apache部署python站点
    MySQL设置从库只读模式
    mysql数据库,创建只读用户
    BUG处理流程说明
    bug的处理流程
    nginx 502 Bad Gateway 错误解决办法
    Linux下批量替换文件内容方法
    centos 安装pecl
    php学习资源
    Docker容器进入的4种方式(转)
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5293479.html
Copyright © 2011-2022 走看看