Title Source:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page= Show_problem&problem=42
Background
Computer generated and assisted proofs and verification occupy a small niche in the realms of computer science. The first proof of the four-color problem is completed with the assistance of a computer program and current efforts in V Erification has 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 is no integer solution s of for n > 2.
The problem
Given a positive integer N, you is to write a program that computes the quantities regarding the solution of
where x, y, and z is constrained to is positive integers less than or equal to N. Compute the number of triples (x,y,z) such that x<y< z , and they is relatively prime, i.e., with no common divisor larger than 1. You were also to compute the number of values such that p was not part of any triple (not just relatively prime t Riples).
The Input
The input consists of a sequence of positive integers, one per line. Each integer in the input file would be less than or equal to 1,000,000. Input is terminated by End-of-file.
The Output
For each of the integer N in the input file print, the integers separated by a space. The first integer is the number of relatively prime triples (such, component of the triple is). The second number is the number of positive integers, and is not a part of any triple whose. There should is one output line for each input line.
Sample Input
1025100
Sample Output
1 44 916 27
Problem Solving Ideas:
This is a number theory, in the language of mathematics is described is: x, Y, z∈n, given a few N, find all x, Y, z≤n, make x2 + y2 = z2 set up. If you want to be poor at all x, Y, Z, according to the amount of data given by the topic, it must not be completed within a limited time. Consider using the nature of the Pythagorean number to generate all x, Y, z to Solve, the mathematical deduction is briefly described as follows:
First assume x, Y, z 22 coprime, because X, y coprime, so x, y in at least one is an odd number. The following uses contradiction to prove that there are only 1 odd numbers in X and Y. Assuming x, y are odd, set:
- x = 2a + 1
- y = 2b + 1
- x2 + y2 = (2a + 1) 2 + (2b + 1) 2
= 4 (A2 + b2 + A + B) + 2
And because X2 and y2 are odd, then Z2 is even, and must be divisible by 4, with the above contradiction, so X, Y has only one odd number.
Assuming that x is odd and y is even, then Z is odd, and the maximum common factor of 2z and 2x is 2,2z and 2x can be written separately
- 2z = (z + x) + (z-x)
- 2x = (z + x)-(Z-x)
The maximum common factor of z + x and Z-x is also 2 because of the maximum common factor properties, and because:
- (z + x) (z-x) = y2, divided by 4 on both sides:
((z + x)/2) ((z-x)/2) = (Y/2) 2
Therefore, it can make:
- z + x = 2m2, z-x = 2n2
where z = m + N, x = M-n (M and n coprime)
Then there are:
- y2 = z2-x2 = 2M22N2 = 4m2n2
That is, y = 2mn.
In summary, the following formula can be obtained:
- x = m2-n2, y = 2mn, z = m2 + n2. (M, n is any natural number)
Here's another question: the number of questions required to count (x, Y, z) triples only counts X, Y and z 22 coprime, which can be solved with the algorithm above. But for the number of statistics p, the problem is not limited to the ternary group is 22 coprime. But the above cannot generate all x, Y, Z is not 22 coprime case. However, assuming that X and Y Max Common factor w are not 1, then Z must also be divisible by W, so W is the male factor for x, Y, z three numbers. It is concluded that all non-22 coprime x0, y0, z0 can be obtained by a set of Coprime x, Y, z times coefficients. According to the above theory, it can be solved quickly.
Reference code:
1#include <cstdio>2#include <cmath>3#include <cstring>4 #defineN 10000105 BOOLUsed[n];6 7 Long LonggcdLong LongALong Longb)8{returnb==0? A:GCD (b,a%b); }9 Ten intMain () One { A Long Longn,a,b,c; - Long LongCount1,count2; - while(SCANF ("%lld", &n)! =EOF) the { -Count1=count2=0; -memset (Used,0,sizeof(used)); - Long LongM= (Long Long) sqrt (n+0.5); + for(Long Longt=1; t<=m; t+=2) - for(Long Longs=t+2; s*t<=n; s+=2) + if(GCD (s,t) = =1)//S>t>=1 and S and T coprime A { atA=s*t;//Odd -b= (s*s-t*t)/2;//even -C= (s*s+t*t)/2;//Odd - if(c<=n)//ppt within the N range - { -count1++; in //printf ("Primitive hook array:%lld%lld%lld\n", a,b,c); - if(!used[a]) {count2++; used[a]=1; } to if(!used[b]) {count2++; used[b]=1; } + if(!used[c]) {count2++; used[c]=1; } - the for(intj=2; c*j<=n; J + +)//J is a multiplier * { $ if(!used[a*j]) {count2++; used[a*j]=1; }Panax Notoginseng if(!used[b*j]) {count2++; used[b*j]=1; } - if(!used[c*j]) {count2++; used[c*j]=1; } the } + } A } theprintf"%lld%lld\n", count1,n-count2); + } - return 0; $}
UVa 106-fermat vs Pythagoras (number theory topic)