結果

問題 No.800 四平方定理
コンテスト
ユーザー bal4u
提出日時 2019-04-13 07:28:23
言語 C(gnu17)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=gnu17 -Wno-error=implicit-function-declaration -Wno-error=implicit-int -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
AC  
実行時間 169 ms / 2,000 ms
コード長 1,392 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 279 ms
コンパイル使用メモリ 40,040 KB
最終ジャッジ日時 2026-02-22 02:55:40
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// yukicoder: No.800 四平方定理
// 2019.4.13 bal4u
// ハッシュテーブル x^2 + y^2 = w^2 + D - z^2

#include <stdio.h>

//// 高速入力
#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif
int in()    // 非負整数の入力
{
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

//// ハッシュテーブル
#define HASHSIZ 19999999 //9999991
typedef struct { int s, cnt; } HASH;
HASH hash[HASHSIZ+5], *hashend = hash + HASHSIZ;

int lookup(int s)
{
	HASH *p = hash + s % HASHSIZ;
	while (p->s) {
		if (p->s == s) return p->cnt;
		if (++p == hashend) p = hash;
	}
	return 0;
}

void insert(int s)
{
	HASH *p = hash + s % HASHSIZ;
	while (p->s) {
		if (p->s == s) { p->cnt++; return; }
		if (++p == hashend) p = hash;
	}
	p->s = s, p->cnt = 1;
}

#define MAX 2000
int n2[MAX+5];

int main()
{
	int i, k, N, D, x, y, z, w, ans;

	for (i = 0; i < MAX; i++) {
		n2[i+1] = n2[i] + (i<<1) + 1;
	}
	
	N = in(), D = in();

	// w^2 + D - z^2
	for (w = 1; w <= N; w++) {
		k = n2[w] + D;
		for (z = 1; z <= N; z++) if (k-n2[z] > 1) insert(k - n2[z]);
	}

	// x ^ 2 + y ^ 2
	ans = 0;
	for (x = 1; x < N; x++) for (y = x + 1; y <= N; y++) {
		ans += lookup(n2[x] + n2[y]) << 1;
	}
	for (x = 1; x <= N; x++) ans += lookup(n2[x] << 1);

	printf("%d\n", ans);
	return 0;
}
0