poj 1305

Fermat vs. Pythagoras

Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 1450   Accepted: 846

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

Source

Duke Internet Programming Contest 1991,UVA 106

暴力枚举就好

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <stack>
 5 #include <queue>
 6 #include <map>
 7 #include <algorithm>
 8 #include <vector>
 9 #include <cmath>
10
11 using namespace std;
12
13 const int maxn = 1000001;
14
15 typedef long long LL;
16
17 bool flag[maxn];
18
19 int gcd(int a,int b)
20 {
21     if(b == 0) return a;
22     else return gcd(b,a%b);
23 }
24 void solve(int t)
25 {
26     int temp,m,i,j,k,n,ans1,ans2,x,y,z;
27     memset(flag,0,sizeof(flag));
28     temp = sqrt(t+0.0);
29     ans1 = ans2 = 0;
30     for( i=1;i<=temp;i++){
31         for(j = i+1;j<=temp;j++){
32             if(i*i + j*j > t) break;
33             if((i%2) != (j%2)){
34                 if(gcd(i,j) == 1){
35                     x = j*j - i*i;
36                     y = 2*i*j;
37                     z = j*j + i*i;
38                     ans1++;
39                     for( k=1;;k++){
40                         if( k*z > t) break;
41                         flag[k*x] = 1;
42                         flag[k*y] = 1;
43                         flag[k*z] = 1;
44                     }
45                 }
46             }
47         }
48     }
49     for(int i=1;i<=t;i++){
50         if(!flag[i]) ans2++;
51     }
52     printf("%d %d\n",ans1,ans2);
53 }
54 int main()
55 {
56     int n;
57     while(scanf("%d",&n)!=EOF){
58         solve(n);
59     }
60     return 0;
61 }

时间: 2024-10-08 10:15:12

poj 1305的相关文章

数论(毕达哥拉斯定理):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

Fermat vs. Pythagoras POJ - 1305 (数论之勾股数组(毕达哥拉斯三元组))

题意:(a, b, c)为a2+b2=c2的一个解,那么求gcd(a, b, c)=1的组数,并且a<b<c<=n,和不为解中所含数字的个数,比如在n等于10时,为1, 2, 7,9则输出4. 好了!把所用知识点说一下: 数论之勾股数组(毕达哥拉斯三元组) 本原勾股数组(a,b,c)(a为奇数,b偶数)都可由如下公式得出:a=st,b=(s2-t2)/2, c = (s2+t2)/2, 其中s>t>=1是没有公因数的奇数. 再把勾股数公式拿过来: 套路一: 当a为大于1的奇数

POJ 1305-Fermat vs. Pythagoras(毕达哥拉斯三元组的解)

题目地址:POJ 1305 题意:给一个整数N,求N范围内的本原的毕达哥拉斯三元组的个数,以及N以内毕达哥拉斯三元组不涉及数的个数. 思路: 首先我们先来了解一下一些基本的定义 毕达哥拉斯三元组: 设不定方程:x^2+y^2=z^2若正整数三元组(x,y,z)满足上述方程,则称为毕达哥拉斯三元组. 本原毕格拉斯三元组: 在毕格拉斯三元组的基础上,若gcd(x,y,z)=1,则称为本原的毕达哥拉斯三元组. 定理: 正整数x,y,z构成一个本原的毕达哥拉斯三元组且y为偶数,当且仅当存在互素的正整数m

毕达哥拉斯三元组的解

对于一个方程: x^2 + y^2 = z^2,他两两互质的正整数解满足一下条件 1. x = 2*m*n ,y = m^2 - n^2 ,z = m^2 + n^2 2. gcd( m , n) =1, m > n , m 与 n 的奇偶性不同 ( 2*m*n )^2 + ( m^2 - n^2 )^2 = ( m^2 + n^2 )显然成立. 下面我们来看一个例题 POJ 1305 传送门 题意: 给定一个n,然后求满足上面那个方程的解的个数以及不满足勾股数的个数. 分析: 题目的数据范围比

POJ 3449 Geometric Shapes --计算几何,线段相交

题意: 给一些多边形或线段,输出与每一个多边形或线段的有哪一些多边形或线段. 解法: 想法不难,直接暴力将所有的图形处理成线段,然后暴力枚举,相交就加入其vector就行了.主要是代码有点麻烦,一步一步来吧. 还有收集了一个线段旋转的函数. Vector Rotate(Point P,Vector A,double rad){ //以P为基准点把向量A旋转rad return Vector(P.x+A.x*cos(rad)-A.y*sin(rad),P.y+A.x*sin(rad)+A.y*co

ACM训练方案-POJ题目分类

ACM训练方案-POJ题目分类 博客分类: 算法 ACM online Judge 中国: 浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 杭州电子科技大学(HDU):http://acm.hdu.edu.cn/ 中国科技大学(USTC):http://acm.ustc.edu.cn/ 北京航天航空大学(BUAA)http://acm.buaa.edu.cn/oj/index.php 南京

转载:poj题目分类(侵删)

转载:from: POJ:http://blog.csdn.net/qq_28236309/article/details/47818407 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K–0.50K:中短代码:0.51K–1.00K:中等代码量:1.01K–2.00K:长代码:2.01K以上. 短:1147.1163.1922.2211.2215.2229.2232.2234.2242.2245.2262.2301.2309.2313.2334.2346.2348

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)