uva 106 Fermat vs. Pythagoras(勾股数求法)

题意:

求1到n中互质的勾股数 a ^ 2 + b ^ 2 = c ^ 2的个数,和不为互质勾股数的个数。


解析:

知道勾股数可以表示为:(j * j - i * i) ^ 2 + (2 * i * j)^ 2 = (i * i + j * j) ^ 2.

其中 j > i , 且 j 与 i 不同奇且不同偶。

然后直接暴力就行了。


代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long

using namespace std;
const int maxn = 1e6;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = 4 * atan(1.0);
const double ee = exp(1.0);

int n;
int cnt;
int num[maxn];

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

void fun()
{
    int m = sqrt(n + 0.5);
    for (int i = 1; i <= m; i++)
    {
        for (int j = i + 1; j <= m; j++)
        {
            int c = i * i + j * j;
            if (n < c)
                break;
            int a = j * j - i * i;
            int b = 2 * i * j;
            if (gcd(gcd(a, b), c) == 1)
            {
                num[a] = num[b] = num[c] = true;
                cnt++;
                for (int k = 2; k * c <= n; k++)
                {
                    num[k * a] = num[k * b] = num[k * c] = true;
                }
            }
        }
    }
}

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    #endif // LOCAL
    while (scanf("%d", &n) == 1)
    {
        memset(num, false, sizeof(num));
        cnt = 0;
        fun();
        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            if (!num[i])
                ans++;
        }
        printf("%d %d\n", cnt, ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值