結果

問題 No.800 四平方定理
ユーザー ty70ty70
提出日時 2019-03-17 23:15:48
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,871 bytes
コンパイル時間 1,272 ms
コンパイル使用メモリ 155,452 KB
実行使用メモリ 98,344 KB
最終ジャッジ日時 2023-09-22 10:24:29
合計ジャッジ時間 5,670 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
8,956 KB
testcase_01 AC 61 ms
4,480 KB
testcase_02 AC 49 ms
5,772 KB
testcase_03 AC 43 ms
4,380 KB
testcase_04 AC 35 ms
4,384 KB
testcase_05 AC 41 ms
4,380 KB
testcase_06 AC 77 ms
4,376 KB
testcase_07 AC 71 ms
4,988 KB
testcase_08 AC 140 ms
4,380 KB
testcase_09 AC 79 ms
6,120 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P,P> PP;

vector<P> div(int n){
	vector<P> res; res.clear();
	for (int i = 1; i * i <= n; ++i){
		if (n % i == 0){
			if (i * i != n){
				int a = i;
				int b = n / i;
				if ((a % 2 != 0) && (b % 2 == 0)) continue;
				if ((a % 2 == 0) && (b % 2 != 0)) continue;
				res.push_back(P(i, n/i));
				res.push_back(P(n/i, i));
			}else{
				res.push_back(P(i,i));
			} // end if
		} // end if
	} // end for

	return res;
}
	
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int N, D; cin >> N >> D;

	set<PP> res; res.clear();

	for (int x = 1; x <= N; ++x){
		for (int y = 1; y <= N; ++y){
			int x2y2 = x * x + y * y;
			int sign = (x2y2 - D < 0 ? -1 : 1);
			int X = abs(x2y2 - D);
			if (X == 0){
				for (int i = 1; i <= N; ++i){
					res.insert(PP(P(x,y), P(i,i)));
				} // end for
				continue;
			} // end if
			vector<P> ans = div(X);
			int m = (int)ans.size();
//			cerr << "m: " << m << endl;
			rep (i, m){
				int a = ans[i].first;
				int b = ans[i].second;
				int w = (a + b) / 2;
				int z = (a - b) / 2;
				if (w >= 1 && z >= 1 && w <= N && z <= N && x2y2 + z * z == w * w + D){
//					cerr << x << ' ' << y << ' ' << z << ' ' << w << endl;
					res.insert(PP(P(x,y),P(z,w)));
				} // end if
			} // end rep
			if (sign < 0){
				rep (i, m){
					int a = ans[i].first;
					int b = sign * ans[i].second;
					int w = (a + b) / 2;
					int z = (a - b) / 2;
						if (w >= 1 && z >= 1 && w <= N && z <= N && x2y2 + z * z == w * w + D){
//					cerr << x << ' ' << y << ' ' << z << ' ' << w << endl;
						res.insert(PP(P(x,y),P(z,w)));
					} // end if
				} // end rep
			} // end if
		} // end for
	} // end for
	cout << (int)res.size() << endl;
 
	return 0;
}
0