【BZOJ1643】[Usaco2007 Oct]Bessie's Secret Pasture 貝茜的祕密草坪【暴力】

【題目鏈接】

n = a^2 + b^2 + c^2 + d^2。

暴力枚舉a,b,c,都是sqrt(n)級別的,然後判斷d^2是否是完全平方數。

/* Telekinetic Forest Guard */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

int n, m;

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

int main() {
	n = iread(); m = sqrt(n);
	int ans = 0;
	for(int i = 0; i <= m; i++) for(int j = 0; j <= m; j++) for(int k = 0; k <= m; k++) {
		int t = n - i * i - j * j - k * k;
		if(t < 0) break;
		int st = sqrt(t);
		if(st * st == t) ans++;
	}
	printf("%d\n", ans);
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章