結果

問題 No.873 バイナリ、ヤバいなり!w
ユーザー bal4ubal4u
提出日時 2019-08-31 12:41:19
言語 C
(gcc 12.3.0)
結果
RE  
実行時間 -
コード長 1,544 bytes
コンパイル時間 1,240 ms
コンパイル使用メモリ 30,116 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-06 11:57:46
合計ジャッジ時間 2,754 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
4,376 KB
testcase_01 AC 32 ms
4,380 KB
testcase_02 AC 150 ms
4,380 KB
testcase_03 AC 121 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 0 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 RE -
testcase_08 AC 27 ms
4,376 KB
testcase_09 AC 0 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 0 ms
4,376 KB
testcase_12 AC 7 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 0 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 0 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 0 ms
4,380 KB
testcase_23 AC 0 ms
4,380 KB
testcase_24 AC 0 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 0 ms
4,380 KB
testcase_28 AC 0 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,504 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 4 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 16 ms
4,380 KB
testcase_35 AC 162 ms
4,376 KB
testcase_36 AC 77 ms
4,376 KB
testcase_37 AC 159 ms
4,376 KB
testcase_38 AC 159 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:8:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    8 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:16:24: 備考: in expansion of macro ‘gc’
   16 |         int n = 0, c = gc();
      |                        ^~
main.c: 関数 ‘outs’ 内:
main.c:9:15: 警告: 関数 ‘putchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    9 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:21:33: 備考: in expansion of macro ‘pc’
   21 | void outs(char *s) { while (*s) pc(*s++); }
      |                                 ^~
main.c: 関数 ‘main’ 内:
main.c:63:57: 警告: iteration 277 invokes undefined behavior [-Waggressive-loop-optimizations]
   63 |         i = 0; while (i < LIM) s01[i++] = '0', s01[i++] = '1';
      |                                                ~~~~~~~~~^~~~~
main.c:63:25: 備考: within this loop
   63 |         i = 0; while (i < LIM) s01[i++] = '0', s01[i++] = '1';
      |                         ^

ソースコード

diff #

// yukicoder: 873 バイナリ、ヤバいなり!w
// 2019.8.31 bal4u

#include <stdio.h>
#include <stdlib.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); while ((c = gc()) >= '0');
	return n;
}

void outs(char *s) { while (*s) pc(*s++); }

#define INF 0x01010101
#define LIM 555
int N;
int a[LIM]; int w;
int dp[500005];
char s01[LIM];

int last = '1';
void pr(int w) {
	static char *s = s01;
	last = s[w], s[w] = 0;
	outs(s);
	s[w] = last;
	s = (last == '0')? s01+1: s01;
}

int adv(int odd, int k) {
	int i;
	for (i = 2-odd; a[i] <= k; i+=2) {
		if (dp[k] == dp[k-a[i]] + i) { k -= a[i], pr(i); return k; }
	}
	return -1;
}

int rev(int odd, int k) {
	int i = w; if ((i & 1) ^ odd) i--;
	while (a[i] > k) i -= 2;
	for ( ; i > 0; i-=2) {
		if (dp[k] == dp[k-a[i]] + i) { k -= a[i], pr(i); return k; }
	}
	return -1;
}

int main()
{
	int i, j, k;
	
	N = in();
	if (N == 1) { puts("0"); return 0; }
	w = 0; while (w*w <= N) w++, a[w] = w*w;
	i = 0; while (i < LIM) s01[i++] = '0', s01[i++] = '1';
	
	for (j = 1; j <= N; j++) {
		int x = INF;
		for (i = 1; a[i] <= j; i++)
			if (dp[j-a[i]] + i  < x) x = dp[j-a[i]] + i, k = i;
		dp[j] = dp[j-a[k]] + k;
	}

	k = N;
	while (k > 0) {
		if (last == '1') {
			if ((j = adv(1, k)) >= 0) k = j;
			else k = rev(0, k);
		} else {
			if ((j = adv(0, k)) >= 0) k = j;
			else k = rev(1, k);
		}
	}
	pc('\n');
	return 0;
}
0