POJ1305 HDU1615 UVA106 Fermat vs. Pythagoras【毕达哥拉斯三角形+暴力】

708 篇文章 18 订阅
509 篇文章 6 订阅

Fermat vs. Pythagoras
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 1739 Accepted: 1021

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

问题链接POJ1305 HDU1615 UVA106 Fermat vs. Pythagoras
问题简述:(略)
问题分析
    毕达哥拉斯三角形问题。设有不定方程:x^2+y^2=z^2,若正整数三元组(x,y,z)满足上述方程,则称为毕达哥拉斯三元组。若gcd(x,y,z)=1,则称为本原毕达哥拉斯三元组。
    有关定理:正整数x,y,z构成一个本原的毕达哥拉斯三元组且y为偶数,当且仅当存在互素的正整数m和n(m>n),其中m和n的奇偶性不同,并且满足x=m^2-n^2,y=2mn,z=m^2+n^2。
    这个题计算在n范围内(x,y,z<=n)本原毕达哥拉斯三元组的个数,以及n以内且毕达哥拉斯三元组不涉及数的个数。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++程序如下:

/* POJ1305 HDU1615 UVA106 Fermat vs. Pythagoras */

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>

using namespace std;

const int N = 1e6;
bool flag[N + 1];

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}

int main()
{
    int n;
    while(~scanf("%d", &n)) {
        memset(flag, false, sizeof(flag));

        int cnt1 = 0, cnt2 = 0, end = (int)sqrt(n);
        for(int a = 1 ; a <= end ; a++ )
            for( int b = a + 1; a * a + b * b <= n ; b++) {
                if(gcd(a, b) == 1 && a % 2 != b % 2) {
                    cnt1++;
                    int x = 2 * a * b;
                    int y = b * b - a * a;
                    int z = b * b + a * a;
                    for(int k = 1 ; k * z <= n ; k++)
                        flag[k * x] = flag[k * y] = flag[k * z] = true;
                }
            }
        for(int i = 1 ; i <= n ; i++)
            if( !flag[i] )
                cnt2++;

        printf("%d %d\n", cnt1, cnt2);
    }

    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值