結果

問題 No.435 占い(Extra)
コンテスト
ユーザー kwm_t
提出日時 2026-06-14 12:46:11
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 2,028 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,274 ms
コンパイル使用メモリ 331,064 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-06-14 12:46:18
合計ジャッジ時間 5,130 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 28 WA * 4
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
// using namespace atcoder;
// using mint = modint1000000007;
// const int mod = 1000000007;
// using mint = modint998244353;
// const int mod = 998244353;
// const int INF = 1e9;
// const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, l, r) for (int i = (l); i < (r); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rrep2(i, l, r) for (int i = (r)-1; i >= (l); --i)
#define all(x) (x).begin(), (x).end()
#define allR(x) (x).rbegin(), (x).rend()
#define P pair<int, int>
template<typename A, typename B> inline bool chmax(A &a, const B &b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A &a, const B &b) { if (a > b) { a = b; return true; } return false; }

static inline int inv_mod9(int x) {
	switch (x % 9) {
	case 1: return 1;
	case 2: return 5;
	case 4: return 7;
	case 5: return 2;
	case 7: return 4;
	case 8: return 8;
	}
	return 0;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int T;
	cin >> T;

	while (T--) {
		int n;
		long long x, a, b, m;
		cin >> n >> x >> a >> b >> m;

		int N = n - 1;

		long long r = x;
		long long sum = 0;
		int ans = 0;

		// C(N,0)=1
		int unit = 1; // 3 を除いた部分 mod 9
		int e = 0;    // 3 の指数

		rep(i, n) {
			if (i) {
				r = ((r ^ a) + b) % m;
			}

			sum += r;

			int coef;
			if (e >= 2) {
				coef = 0;
			}
			else if (e == 1) {
				coef = unit * 3 % 9;
			}
			else {
				coef = unit;
			}

			ans += coef * (int)(r % 10);
			ans %= 9;

			if (i == N) continue;

			long long num = N - i;
			long long den = i + 1;

			int cnum = 0;
			while (num % 3 == 0) {
				num /= 3;
				cnum++;
			}

			int cden = 0;
			while (den % 3 == 0) {
				den /= 3;
				cden++;
			}

			e += cnum - cden;

			unit = unit * (int)(num % 9) % 9;
			unit = unit * inv_mod9((int)(den % 9)) % 9;
		}

		if (ans == 0 && sum != 0) ans = 9;
		cout << ans << '\n';
	}

	return 0;
}
0