結果

問題 No.3310 mod998
コンテスト
ユーザー cho435
提出日時 2025-10-24 23:00:51
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 750 ms / 2,000 ms
コード長 2,042 bytes
コンパイル時間 6,867 ms
コンパイル使用メモリ 441,960 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-10-24 23:01:16
合計ジャッジ時間 18,378 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define all(x) begin(x), end(x)

template <class T> bool chmin(T& x, T y) {
	return x > y ? (x = y, true) : false;
}
template <class T> bool chmax(T& x, T y) {
	return x < y ? (x = y, true) : false;
}

struct io_setup {
	io_setup() {
		ios::sync_with_stdio(false);
		cin.tie(nullptr);
		cout << fixed << setprecision(15);
	}
} io_setup;

constexpr int MOD = 998;
void madd(int& x, int y) {
	x = (x + y % MOD + MOD) % MOD;
}
// Bint
#include <boost/multiprecision/cpp_int.hpp>
using Bint = boost::multiprecision::cpp_int;
// using Bint = __int128_t;

void solve() {
	int n, m;
	cin >> n >> m;
	int l_st = -1, ln = -1, ad = -1;
	{
		map<int, pair<int, int>> mp;
		int sm = 1;
		int nw = 1;
		mp[nw] = {1, 0};
		rep(i, 1, 1000) {
			int nx = nw * n % MOD;
			sm = (sm + nx) % MOD;
			if (mp.count(nx)) {
				auto [bsm, j] = mp[nx];
				l_st = nx;
				ln = i - j;
				ad = (sm - bsm + MOD) % MOD;
				break;
			}
			mp[nx] = {sm, i};
			nw = nx;
		}
	}
	assert(ln != 0);
	rep(i, 0, m) {
		string s;
		cin >> s;
		if (n == 0) {
			cout << "1\n";
			continue;
		}
		Bint k(s);
		k++;
		int ans = 0;
		int nw = 1;
		while (k > 0) {
			if (nw == l_st && k > ln) {
				int tmp = (int)((k / ln) % MOD);
				madd(ans, tmp * ad);
				k %= ln;
				continue;
			}
			madd(ans, nw);
			nw = (nw * n) % MOD;
			k--;
		}
		cout << ans << '\n';
	}
}

int main() {
	int t = 1;
	cin >> t;
	while (t--) solve();

	// rep(n, 1, MOD) {
	// 	int l_st = -1, ln = -1, ad = -1;
	// 	{
	// 		map<int, pair<int, int>> mp;
	// 		int sm = 1;
	// 		int nw = 1;
	// 		mp[nw] = {1, 0};
	// 		rep(i, 1, 1000) {
	// 			int nx = nw * n % MOD;
	// 			sm = (sm + nx) % MOD;
	// 			if (mp.count(nx)) {
	// 				auto [bsm, j] = mp[nx];
	// 				l_st = nx;
	// 				ln = i - j;
	// 				ad = (sm - bsm + MOD) % MOD;
	// 				break;
	// 			}
	// 			mp[nx] = {sm, i};
	// 			nw = nx;
	// 		}
	// 	}
	// 	cout << n << " ; " << ln << '\n';
	// }
}
0