Project Euler 009 Special Pythagorean triplet

题意:求一组勾股数 (a,b,c) ,满足 a+b+c=1000
分析:暴力枚举 a,b O(10002) 的。
考虑公式 a=m2n2,b=2mn,c=m2+n2(m>n) 已经可以生成所有的基本勾股数了,我们只需要在所有基本勾股数中找一组周长是 1000 的约数的即可,由于 c<500 ,因此 m 的范围是O(500)的, n<m ,这样的复杂度就是 O(500) 了。

#include <bits/stdc++.h>

#define ll long long

#define pii std::pair<int,int>
#define mp std::make_pair
#define fi first
#define se second

#define SZ(x) (int)(x).size()
#define pb push_back

template<class T>inline void chkmax(T &x, const T &y) {if(x < y) x = y;}
template<class T>inline void chkmin(T &x, const T &y) {if(x > y) x = y;}

template<class T>
inline void read(T &x) {
    char c;int f = 1;x = 0;
    while(((c=getchar()) < '0' || c > '9') && c != '-');
    if(c == '-') f = -1;else x = c-'0';
    while((c=getchar()) >= '0' && c <= '9') x = x*10+c-'0';
    x *= f;
}
static int outn;
static char out[(int)2e7];
template<class T>
inline void write(T x) {
    if(x < 0) out[outn++] = '-', x = -x;
    if(x) {
        static int tmpn;
        static char tmp[20];
        tmpn = 0;
        while(x) tmp[tmpn++] = x%10+'0', x /= 10;
        while(tmpn) out[outn++] = tmp[--tmpn];
    }
    else out[outn++] = '0';
}

int main() {
    for(int m = 1; m * m < 500; ++m)
        for(int n = 1; n < m; ++n) {
            int a = m * m - n * n, b = 2 * m * n, c = m * m + n * n;
            if(1000 % (a + b + c) == 0) {
                int t = 1000 / (a + b + c);
                printf("%d\n", a * b * c * t * t * t);
                return 0;
            }
        }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值