結果

問題 No.873 バイナリ、ヤバいなり!w
ユーザー bal4u
提出日時 2019-08-31 12:18:30
言語 C
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,523 bytes
コンパイル時間 221 ms
コンパイル使用メモリ 30,848 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-24 06:35:10
合計ジャッジ時間 2,415 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 RE * 1
other AC * 34 WA * 2
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:8:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
    8 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:16:24: note: in expansion of macro 'gc'
   16 |         int n = 0, c = gc();
      |                        ^~
main.c: In function 'outs':
main.c:9:15: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
    9 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:21:33: note: in expansion of macro 'pc'
   21 | void outs(char *s) { while (*s) pc(*s++); }
      |                                 ^~
main.c: In function 'main':
main.c:63:57: warning: iteration 277 invokes undefined behavior [-Waggressive-loop-optimizations]
   63 |         i = 0; while (i < LIM) s01[i++] = '0', s01[i++] = '1';
      |                                                ~~~~~~~~~^~~~~
main.c:63:25: note: 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[300005];
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 pr_odd(int k) {
	int i;
	for (i = 1; a[i] <= k; i+=2) {
		if (dp[k] == dp[k-a[i]] + i) { k -= a[i], pr(i); return k; }
	}
	return -1;
}

int pr_even(int k) {
	int i = w; if (i & 1) i--;
	while (k < a[i]) 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 = pr_odd(k)) >= 0) k = j;
			else k = pr_even(k);
		} else {
			if ((j = pr_even(k)) >= 0) k = j;
			else k = pr_odd(k);
		}
	}
	pc('\n');
	return 0;
}
0