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