結果
| 問題 |
No.800 四平方定理
|
| コンテスト | |
| ユーザー |
ty70
|
| 提出日時 | 2019-03-17 23:20:13 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,987 bytes |
| コンパイル時間 | 1,483 ms |
| コンパイル使用メモリ | 169,444 KB |
| 実行使用メモリ | 10,752 KB |
| 最終ジャッジ日時 | 2024-07-08 02:42:23 |
| 合計ジャッジ時間 | 5,318 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 3 |
| other | WA * 10 TLE * 1 -- * 19 |
ソースコード
#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> solve(int n, 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;
int w = (a + b) / 2;
int z = (a - b) / 2;
if (w < 1 || w > N || z < 1 || z > N) 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 = solve(X, N);
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;
}
ty70