結果

問題 No.434 占い
ユーザー bal4ubal4u
提出日時 2019-05-08 23:34:56
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,466 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 30,796 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 17:26:39
合計ジャッジ時間 1,753 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 0 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 0 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 0 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 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: 関数 ‘main’ 内:
main.c:9:15: 警告: 関数 ‘putchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    9 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:77:17: 備考: in expansion of macro ‘pc’
   77 |                 pc('0'+s), pc('\n');
      |                 ^~

ソースコード

diff #

// yukicoder: No.432 占い(Easy)
// 2019.4.17 bal4u

#include <stdio.h>
#include <string.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;
}

int ins(char *s)  // 文字列の入力 スペース以下の文字で入力終了
{
	char *p = s;
	do *s = gc();
	while (*s++ > ' ');
	*--s = 0;
	return s - p;
}

#define MOD 9

char  S[100003], *p;
int tbl[100003][2]; int id = -1;
char  t[100003];
int inv[10] = {0,1,5,0,7,2,0,4,8,0};

int p3(int *a, int n) {
	int r = 0;
	while (n % 3 == 0) r++, n /= 3;
	*a = n;
	return r;
}

void calcBC(int n)
{
	int i, a, b;
	
	tbl[0][0] = 1, tbl[0][1] = 0, t[0] = 1;
	if (n == 0) return;
	tbl[1][1] = p3(&a, n), tbl[1][0] = a % MOD, t[1] = n % MOD;
	for (i = 2; i <= n; i++) {
		tbl[i][1] = tbl[i-1][1] + p3(&a, n+1-i) - p3(&b, i);
		tbl[i][0] = (tbl[i-1][0] * a * inv[(b % MOD)]) % MOD;
		if (tbl[i][1] >= 2) t[i] = 0;
		else if (tbl[i][1] == 0) t[i] = tbl[i][0] % MOD;
		else t[i] = tbl[i][0]*3 % MOD;
	}
}
		
int main()
{
	int i, w, s, T;

	T = in();
	while (T--) {
		w = ins(p = S)-1;
		while (*p == '0') p++;
		if (*p == 0) s = 0;
		else {
			if (id != w) calcBC(w), id = w;
			s = 0; for (i = 0; i <= w; i++) {
				if (S[i] < '9') s += (S[i] & 0xf) * t[i];
			}
			s %= 9;
			if (s == 0) s = 9;
		}
		pc('0'+s), pc('\n');
	}
	return 0;
}
0