結果

問題 No.435 占い(Extra)
ユーザー pekempeypekempey
提出日時 2016-10-15 00:04:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 430 ms / 2,000 ms
コード長 1,941 bytes
コンパイル時間 1,499 ms
コンパイル使用メモリ 148,828 KB
実行使用メモリ 11,616 KB
最終ジャッジ日時 2023-07-30 00:21:49
合計ジャッジ時間 7,540 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
11,360 KB
testcase_01 AC 7 ms
11,336 KB
testcase_02 AC 7 ms
11,360 KB
testcase_03 AC 8 ms
11,364 KB
testcase_04 AC 8 ms
11,408 KB
testcase_05 AC 8 ms
11,296 KB
testcase_06 AC 8 ms
11,424 KB
testcase_07 AC 8 ms
11,360 KB
testcase_08 AC 8 ms
11,284 KB
testcase_09 AC 8 ms
11,280 KB
testcase_10 AC 10 ms
11,288 KB
testcase_11 AC 10 ms
11,360 KB
testcase_12 AC 30 ms
11,356 KB
testcase_13 AC 29 ms
11,364 KB
testcase_14 AC 29 ms
11,356 KB
testcase_15 AC 30 ms
11,408 KB
testcase_16 AC 36 ms
11,356 KB
testcase_17 AC 91 ms
11,488 KB
testcase_18 AC 30 ms
11,408 KB
testcase_19 AC 29 ms
11,352 KB
testcase_20 AC 419 ms
11,296 KB
testcase_21 AC 229 ms
11,340 KB
testcase_22 AC 222 ms
11,484 KB
testcase_23 AC 222 ms
11,284 KB
testcase_24 AC 231 ms
11,612 KB
testcase_25 AC 287 ms
11,568 KB
testcase_26 AC 430 ms
11,300 KB
testcase_27 AC 229 ms
11,616 KB
testcase_28 AC 8 ms
11,356 KB
testcase_29 AC 229 ms
11,352 KB
testcase_30 AC 419 ms
11,356 KB
testcase_31 AC 278 ms
11,364 KB
testcase_32 AC 199 ms
11,360 KB
testcase_33 AC 226 ms
11,484 KB
testcase_34 AC 260 ms
11,356 KB
testcase_35 AC 224 ms
11,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

pair<long long, long long> extgcd(long long a, long long b) {
	if (b == 0) return{ 1, 0 };
	long long x, y;
	tie(x, y) = extgcd(b, a % b);
	return{ y, x - a / b * y };
}

long long modulo(long long a, long long mod) {
	return (a % mod + mod) % mod;
}

long long modinv(long long a, long long mod) {
	return modulo(extgcd(a, mod).first, mod);
}

long long F[100], invF[100];

int count(int n) {
	int res = 0;
	n /= 3;
	while (n > 0) res += n, n /= 3;
	return res;
}

void init() {
	F[0] = 1;
	for (long long i = 1; i < 50; i++) {
		if (i % 3 == 0) {
			F[i] = F[i - 1];
		} else {
			F[i] = F[i - 1] * i % 9;
		}
	}

	invF[49] = modinv(F[49], 9);
	for (long long i = 48; i >= 0; i--) {
		if ((i + 1) % 3 == 0) {
			invF[i] = invF[i + 1];
		} else {
			invF[i] = invF[i + 1] * (i + 1) % 9;
		}
	}
};

long long cache[1000000];

long long FF(int n) {
	if (n == 0) return 1;
	return F[n % 18] * FF(n / 3) % 9;
}

long long invFF(int n) {
	if (n < 1000000) return cache[n];
	return invF[n % 18] * invFF(n / 3) % 9;
}

int main() {
	int T;
	cin >> T;

	init();

	cache[0] = 1;
	for (int i = 1; i < 1000000; i++) {
		cache[i] = invF[i % 18] * cache[i / 3] % 9;
	}

	while (T--) {
		long long n, x, a, b, m;
		scanf("%lld %lld %lld %lld %lld", &n, &x, &a, &b, &m);

		bool zero = true;

		n--;

		int N = count(n);
		int L = 0;
		int R = N;

		long long ans = 0;

		long long xF = FF(n);

		for (int i = 0; i <= n; i++) {
			if (x % 10 != 0) zero = false;

			long long comb = xF * invFF(n - i) * invFF(i);
			int cnt = N - L - R;
			if (cnt >= 2) comb = 0;
			else if (cnt == 1) comb *= 3;
			assert(cnt >= 0);
			(ans += comb * (x % 10)) %= 9;

			if (i < n) {
				for (int t = i + 1; t > 0 && t % 3 == 0; t /= 3) L++;
				for (int t = n - i; t > 0 && t % 3 == 0; t /= 3) R--;
			}

			x = ((x ^ a) + b) % m;
		}
		ans %= 9;
		if (ans == 0) ans = 9;
		if (zero) ans = 0;
		printf("%lld\n", ans);
	}
}
0