結果

問題 No.435 占い(Extra)
ユーザー bal4ubal4u
提出日時 2020-01-20 17:17:10
言語 C
(gcc 12.3.0)
結果
MLE  
実行時間 -
コード長 2,248 bytes
コンパイル時間 1,152 ms
コンパイル使用メモリ 30,588 KB
実行使用メモリ 81,528 KB
最終ジャッジ日時 2023-07-30 00:45:27
合計ジャッジ時間 4,388 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 3 ms
4,384 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 17 ms
9,804 KB
testcase_13 AC 14 ms
4,380 KB
testcase_14 AC 14 ms
4,380 KB
testcase_15 AC 14 ms
4,380 KB
testcase_16 AC 16 ms
4,384 KB
testcase_17 AC 27 ms
4,384 KB
testcase_18 AC 20 ms
9,852 KB
testcase_19 AC 14 ms
4,380 KB
testcase_20 MLE -
testcase_21 AC 129 ms
9,864 KB
testcase_22 AC 129 ms
4,380 KB
testcase_23 AC 128 ms
4,384 KB
testcase_24 AC 125 ms
4,384 KB
testcase_25 AC 140 ms
4,380 KB
testcase_26 MLE -
testcase_27 AC 129 ms
9,856 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 118 ms
9,804 KB
testcase_30 AC 0 ms
4,380 KB
testcase_31 AC 144 ms
42,580 KB
testcase_32 AC 149 ms
13,964 KB
testcase_33 AC 162 ms
4,380 KB
testcase_34 AC 171 ms
4,380 KB
testcase_35 AC 165 ms
5,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// yukicoder: No.435 占い(Extra)
// 2019.5.10 bal4u

#include <stdio.h>
#include <string.h>

#if 1
int getchar_unlocked(void);
int putchar_unlocked(int c);
#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[10000005][2]; int id = -1;
int t[10000005];
int inv[10] = {0,1,5,0,7,2,0,4,8,0};  // mod9 の逆数
//int inv[10] = {0,6,5,0,4,1,0,2,3,0};

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