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 for n > 2.
The Problem
Given a positive integer N, you are to write a program that computes two quantities regarding the solution of
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 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 ). The second number is the number of positive integers that are not part of any triple whose components are all . 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;}
- POJ.Fermat vs. Pythagoras
- POJ 1305 Fermat vs. Pythagoras
- POJ 1305 Fermat vs. Pythagoras
- poj 1305 Fermat vs. Pythagoras
- poj 1305——Fermat vs. Pythagoras
- POJ 1305 Fermat vs. Pythagoras(勾股数)
- POJ 1305 Fermat vs. Pythagoras 笔记
- POJ-1305-Fermat vs. Pythagoras-(本原勾股数)
- 106 - Fermat vs. Pythagoras
- uva106 - Fermat vs. Pythagoras()
- 106 - Fermat vs. Pythagoras
- UVaOJ106 - Fermat vs. Pythagoras
- Fermat vs. Pythagoras
- POJ-1305(勾股定理)(Fermat vs. Pythagoras)
- POJ 1035 Fermat vs. Pythagoras(毕达哥拉斯三元组)
- UVa 106/POJ 1305 Fermat vs. Pythagoras(数论&勾股数)
- POJ 1305 Fermat vs. Pythagoras 解原毕达哥拉斯三元组
- POJ 1305 Fermat vs. Pythagoras (构造 毕达哥拉斯三元组)
- 移植EMCV到DM6467(3)——CCS调试XDAIS算法
- iPhone开发中的技巧整理
- ref参数和out参数
- 项目计划
- IOS 文件操作
- POJ.Fermat vs. Pythagoras
- hdu 1671 Phone List(二叉树)
- 函数堆栈
- Leetcode: N-Queens II
- MarkdownPad使用心得小窍门--> 开源code编辑工具
- VS2010 C++编译报错LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- oracle 最基本命令
- ubuntu12.04安装java6,验证可用
- SDL 和SDL_TTF 联合实现 Video Player 加OSD