結果

問題 No.435 占い(Extra)
ユーザー bal4ubal4u
提出日時 2019-05-09 06:30:12
言語 C
(gcc 12.3.0)
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,153 bytes
コンパイル時間 396 ms
コンパイル使用メモリ 33,664 KB
実行使用メモリ 50,560 KB
最終ジャッジ日時 2024-04-17 03:00:48
合計ジャッジ時間 4,280 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 0 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 20 ms
6,528 KB
testcase_13 AC 14 ms
5,376 KB
testcase_14 AC 14 ms
5,376 KB
testcase_15 AC 13 ms
5,376 KB
testcase_16 AC 15 ms
5,376 KB
testcase_17 AC 28 ms
5,376 KB
testcase_18 AC 20 ms
6,528 KB
testcase_19 AC 15 ms
5,376 KB
testcase_20 MLE -
testcase_21 AC 129 ms
6,528 KB
testcase_22 AC 132 ms
5,376 KB
testcase_23 AC 121 ms
5,376 KB
testcase_24 AC 127 ms
5,376 KB
testcase_25 AC 137 ms
5,376 KB
testcase_26 MLE -
testcase_27 AC 129 ms
6,528 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 116 ms
6,528 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 145 ms
24,832 KB
testcase_32 AC 142 ms
7,808 KB
testcase_33 AC 160 ms
5,376 KB
testcase_34 AC 167 ms
5,376 KB
testcase_35 AC 156 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 'main':
main.c:9:15: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
    9 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:84:17: note: in expansion of macro 'pc'
   84 |                 pc('0'+s), pc('\n');
      |                 ^~

ソースコード

diff #

// yukicoder: No.435 占い(Extra)
// 2019.5.10 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;
}

#define MOD 9

int tbl[ 5000003][2]; int id = -1;
char  t[10000003];
int inv[10] = {0,1,5,0,7,2,0,4,8,0};  // mod9 の逆数

int p3(int *a, int n) {  // 3のべき乗を取り出す
	int r = 0;
	while (n % 3 == 0) r++, n /= 3;
	*a = n;              // *a は3のべき乗以外に残った部分  元のn = (*a) * 3^r
	return r;
}

void calcBC(int n)
{
	int i, a, b, _n;
	
	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;
	_n = n >> 1;
	for (i = 2; i <= _n; i++) {
		tbl[i][1] = tbl[i-1][1] + p3(&a, n+1-i) - p3(&b, i);  // 3のべき乗
		tbl[i][0] = (tbl[i-1][0] * a * inv[(b % MOD)]) % MOD; // 3のべき乗以外の部分
		if (tbl[i][1] >= 2) t[i] = 0;    // 9が含まれるので、二項係数は0
		else if (tbl[i][1] == 0) t[i] = tbl[i][0];   // そのままが二項係数
		else t[i] = tbl[i][0]*3 % MOD;   // 3をかけて二項係数になる
	}
	for (; i <= n; i++) t[i] = t[n-i]; // 後半の計算は前半を流用
}

int x, a, b, m;
int next() {   // つぎの入力数字
	x = ((x^a) + b) % m;
	return x % 10;
}

int main()
{
	int i, k, n, s, T;
	int zero;

	T = in();
	while (T--) {
		n = in()-1, x = in(), a = in(), b = in(), m = in();
		if (m == 1 || (x|a|b) == 0) s = 0; // 0のみの入力数列は数字根が0
		else {
			if (id != n) calcBC(n), id = n;  // 数列をすべて違う長さの入力データには本解答プログラムは無力
			s = x % 10;
			zero = (s == 0);
			for (i = 1; i <= n; i++) {
				k = next();
				if (k) {
					zero = 0;
					if (k < '9') s += k * t[i];  // 入力数列の各数字と二項係数の積
				}
			}
			if (zero) s = 0;
			else {
				s %= 9;
				if (s == 0) s = 9;  // 0以外の数列の数字根は0を9に読み替える
			}
		}
		pc('0'+s), pc('\n');
	}
	return 0;
}
0