結果

問題 No.435 占い(Extra)
ユーザー pekempeypekempey
提出日時 2016-10-15 00:01:00
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,936 bytes
コンパイル時間 1,583 ms
コンパイル使用メモリ 165,480 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2024-04-17 02:37:56
合計ジャッジ時間 6,417 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
11,520 KB
testcase_01 AC 10 ms
11,520 KB
testcase_02 AC 10 ms
11,392 KB
testcase_03 AC 10 ms
11,392 KB
testcase_04 AC 10 ms
11,264 KB
testcase_05 AC 10 ms
11,392 KB
testcase_06 WA -
testcase_07 AC 10 ms
11,264 KB
testcase_08 AC 10 ms
11,392 KB
testcase_09 AC 10 ms
11,392 KB
testcase_10 AC 11 ms
11,392 KB
testcase_11 AC 12 ms
11,392 KB
testcase_12 AC 28 ms
11,392 KB
testcase_13 AC 28 ms
11,264 KB
testcase_14 AC 28 ms
11,392 KB
testcase_15 AC 28 ms
11,264 KB
testcase_16 AC 35 ms
11,520 KB
testcase_17 AC 91 ms
11,520 KB
testcase_18 AC 29 ms
11,520 KB
testcase_19 AC 28 ms
11,392 KB
testcase_20 AC 284 ms
11,264 KB
testcase_21 AC 193 ms
11,520 KB
testcase_22 AC 190 ms
11,392 KB
testcase_23 AC 190 ms
11,392 KB
testcase_24 WA -
testcase_25 AC 260 ms
11,392 KB
testcase_26 AC 289 ms
11,520 KB
testcase_27 AC 205 ms
11,392 KB
testcase_28 AC 10 ms
11,392 KB
testcase_29 AC 193 ms
11,520 KB
testcase_30 WA -
testcase_31 AC 206 ms
11,392 KB
testcase_32 AC 171 ms
11,392 KB
testcase_33 AC 197 ms
11,264 KB
testcase_34 WA -
testcase_35 AC 192 ms
11,392 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:73:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   73 |                 scanf("%lld %lld %lld %lld %lld", &n, &x, &a, &b, &m);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

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 != 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