POJ.Fermat vs. Pythagoras

来源:互联网 发布:2017淘宝网下载 编辑:程序博客网 时间:2024/05/16 05:02

题目:

Description

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

Sample Input

1025100

Sample Output

1 44 916 27

题意:求方程x^2 + y ^2 = z^2(a、b、c互素且0<x,y,z<=n)的解的个数和所有不在上面方程中的p(p <= n)的个数(P只要满足此方程即可,不要求p的限制条件和解一致,如n为10,x = 3,y = 4,z = 5是方程的解,而x = 6,y = 8,z = 10同样为方程的解但a b c没有互为素数)。

策略:对方程做变形,因为  (a^2 - b^2)^2 + (2*a*b)^2 = (a^2 + b^2)^2,所以x = a^2 - b^2,y = 2*a*b,z = a^2 + b^2;且a b的奇偶性不同。



#include <iostream>#include <stdio.h>#include <memory.h>#include <math.h>using namespace std;#define MAX_SIZE 1000010bool flag[MAX_SIZE];int gcd(int i,int j)//最大公约数{if(j == 0)return i;elsereturn gcd(j,i%j);}int main(){int n;while(scanf("%d",&n) != EOF){memset(flag,false,sizeof(flag));int count1 = 0;//统计符合条件的个数 int count2 = 0;//满足方程式int mid = sqrt(n + 0.0);for(int i = 1;i <= mid;i++)for(int j = i + 1;j <= mid;j += 2){if(i * i + j * j > n)break;if(gcd(i,j) == 1){int x = j * j - i * i;int z = j * j + i * i;int y = 2 * j * i;count1++;for(int k = 1;k * z <= n;k++){flag[k * x] = true;flag[k * y] = true;flag[k * z] = true;}}}for(int i = 1;i <= n;i++)if(flag[i] == false)count2++;cout << count1 << " " << count2 << endl;}    return 0;}                                 

原创粉丝点击