結果

問題 No.800 四平方定理
ユーザー bal4ubal4u
提出日時 2019-04-13 07:22:45
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 149 ms / 2,000 ms
コード長 1,481 bytes
コンパイル時間 260 ms
コンパイル使用メモリ 30,736 KB
実行使用メモリ 40,928 KB
最終ジャッジ日時 2023-10-13 09:42:12
合計ジャッジ時間 3,249 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 3 ms
4,352 KB
testcase_07 AC 3 ms
4,352 KB
testcase_08 AC 4 ms
4,352 KB
testcase_09 AC 3 ms
4,352 KB
testcase_10 AC 68 ms
21,032 KB
testcase_11 AC 69 ms
21,128 KB
testcase_12 AC 84 ms
23,180 KB
testcase_13 AC 59 ms
18,016 KB
testcase_14 AC 60 ms
19,220 KB
testcase_15 AC 78 ms
23,204 KB
testcase_16 AC 57 ms
19,392 KB
testcase_17 AC 53 ms
19,308 KB
testcase_18 AC 85 ms
27,084 KB
testcase_19 AC 87 ms
27,132 KB
testcase_20 AC 0 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 90 ms
27,184 KB
testcase_23 AC 149 ms
40,928 KB
testcase_24 AC 109 ms
33,052 KB
testcase_25 AC 145 ms
40,724 KB
testcase_26 AC 1 ms
4,348 KB
testcase_27 AC 0 ms
4,348 KB
testcase_28 AC 104 ms
32,836 KB
testcase_29 AC 124 ms
36,864 KB
testcase_30 AC 107 ms
32,908 KB
testcase_31 AC 96 ms
30,696 KB
testcase_32 AC 118 ms
33,916 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:10:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   10 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:18:24: 備考: in expansion of macro ‘gc’
   18 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

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

#include <stdio.h>
#include <math.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, zmax, 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++) {
		zmax = (int)sqrt(n2[w] + D - 2);
		if (zmax > N) zmax = N;
		k = n2[w] + D;
		for (z = 1; z <= zmax; 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