程序控

IPPP (Institute of Penniless Peasent-Programmer) Fellow

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: :: 管理 ::

Time limit: 3.000 seconds
限时:3.000秒

 

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 an + bn = cn for n > 2.
本问题要来计算的值与“费马最后定理”有关,即:当n>2时,an + bn = cn没有整数解。

 

The Problem
问题

Given a positive integer N, you are to write a program that computes two quantities regarding the solution of
给定一个整数N,你要写一个程序来计算出两个关于以下方程解的量:

x2 + y2 = z2

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).
其中x,y和Z都限定为正整数且小于等于N。你要计算出所有满足x < y < z的三元组(x,y,z)的数量,并且使得x,y和z两两互质,即没有大于1的公因数。你还要计算出所有满足下面条件的p的数量:0 < p ≤ N,且p没有在所有这样的三元组中出现(并不限定为两两互质的三元组)。

 

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.
输入包括一系列的正整数,每行一个。输入数据中的每个整数都小于或等于1,000,000。输入由EOF表示结束。

 

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 ≤ 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.
对应于输入的每个整数N,都要输出两个整数,由空格隔开。第一个整数是互质三元组的数量(每个三元组的任何元素都 ≤ N)。第二个数为 ≤ N且没有出现在这些三元组中的整数的个数。每行输入对应一行输出。

 

Sample Input
输入示例

10
25
100

 

Sample Output
输出示例

1 4
4 9
16 27

 

Analysis
分析

这是一道数论题,用数学的语言描述就是: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乘以系数得到。根据以上理论就可以快速的求解了。

 

Solution
解答

#include <iostream>
#include <math.h>
using namespace std;
//主函数
int main(void) {
	//以下算法的描述详见相关文档
	//循环处理所有输入的N,变量i表示N
	for (int i, m, n, nMaxM, nMaxN, nCnt = 0; cin >> i; nCnt = 0) {
		//aFlags用于标记哪些整数在三元组中出现过
		bool aFlags[1000001] = {false};
		//循环生成所有的m和n,根据公式,m的最大值必小于N的开平方
		for (m = 2, nMaxM = (int)sqrt((float)i - 1); m <= nMaxM; ++m) {
			//求出n的最大值,小于等于m,且小于等于i-m^2
			nMaxN = (int)sqrt((float)i - m * m);
			nMaxN = nMaxN >= m ? m - 1 : nMaxN;
			//此时已得到所有可能的m和n的范围,循环生成所有三元组
			for (n = 1; n <= nMaxN; ++n) {
				//n和m必须互质,忽略都是偶数的情况
				if (n % 2 != m % 2) {
					//进一步判断互质,辗转相除法求出n和m的最大公因数
					int a = m, b = n, c;
					for(int r; (r = a % b) != 0; a = b, b = r);
					//公因数为1才互质
					if (b == 1) {
						//生成了一个互质三元组,总数增1
						++nCnt;
						//根据此三元组,生成所有并不互质的三元组,
						a = m * m - n * n, b = 2 * m * n, c = m * m + n * n;
						//标记在这些三元组中出现的整数
						for (int k = 0; c * k <= i; ++k) {
							aFlags[a * k] = aFlags[b * k] = aFlags[c * k] = 1;
						}
					}
				}
			}
		}
		//输出三元组的数量
		cout << nCnt << ' ';
		//统计并输出没有使用过的整数(p)的数量
		for (nCnt = 0, m = 1; m <= i; nCnt += !aFlags[m++]);
		cout << nCnt << endl;
	}
	return 0;
}
posted on 2010-08-14 19:22  Devymex  阅读(2499)  评论(0编辑  收藏  举报