結果

問題 No.3118 Increment or Multiply
ユーザー rii922
提出日時 2025-04-19 23:02:48
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 83 ms / 2,000 ms
コード長 4,893 bytes
コンパイル時間 3,311 ms
コンパイル使用メモリ 278,212 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-04-19 23:02:55
合計ジャッジ時間 5,676 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define reps(i, n) for(int i=1, i##_len=(n); i<=i##_len; ++i)
#define rrep(i, n) for(int i=((int)(n)-1); i>=0; --i)
#define rreps(i, n) for(int i=((int)(n)); i>0; --i)
#define all(v) (v).begin(), (v).end()
#define el '\n'
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
using vs = vector<string>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
template<class T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T> bool chmaxeq(T &a, const T &b) { if (a<=b) { a=b; return 1; } return 0; }
template<class T> bool chmineq(T &a, const T &b) { if (b<=a) { a=b; return 1; } return 0; }
bool yes(bool a=true) { cout << (a?"yes":"no") << el; return a; }
bool no(bool a=true) { cout << (a?"no":"yes") << el; return a; }
bool Yes(bool a=true) { cout << (a?"Yes":"No") << el; return a; }
bool No(bool a=true) { cout << (a?"No":"Yes") << el; return a; }
bool YES(bool a=true) { cout << (a?"YES":"NO") << el; return a; }
bool NO(bool a=true) { cout << (a?"NO":"YES") << el; return a; }
template<class T1, class T2> istream &operator>>(istream &is, pair<T1, T2> &p);
template<class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p);
template<class T> istream &operator>>(istream &is, vector<T> &v);
template<class T> ostream &operator<<(ostream &os, const vector<T> &v);
template<class T1, class T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
	return is >> p.first >> p.second;
}
template<class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
	return os << p.first << ' ' << p.second;
}
template<class T>
istream &operator>>(istream &is, vector<T> &v) {
	int sz = v.size();
	for (int i = 0; i < sz; i++) is >> v[i];
	return is;
}
template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
	int sz = v.size();
	for (int i = 0; i < sz; i++) {
		if (i) os << ' ';
		os << v[i];
	}
	return os;
}
void _main();
int main() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(16); _main(); return 0; }


template<int mod>
struct static_mint {
	using mint = static_mint<mod>;
	static_mint(long long x = 0) : _x((x%mod+mod)%mod) {}
	long long val() const { return _x; }
	mint operator+() const { return mint(_x); }
	mint operator-() const { return mint(-_x); }
	mint &operator+=(const mint a) {
		if ((_x += a._x) >= mod) _x -= mod;
		return *this;
	}
	mint &operator-=(const mint a) {
		if ((_x += mod - a._x) >= mod) _x -= mod;
		return *this;
	}
	mint &operator*=(const mint a) {
		(_x *= a._x) %= mod;
		return *this;
	}
	mint &operator++() {
		*this += 1;
		return *this;
	}
	mint operator++(int) {
		mint temp = *this;
		++*this;
		return temp;
	}
	mint &operator--() {
		*this -= 1;
		return *this;
	}
	mint operator--(int) {
		mint temp = *this;
		--*this;
		return temp;
	}
	mint operator+(const mint a) const { return mint(*this) += a; }
	mint operator-(const mint a) const { return mint(*this) -= a; }
	mint operator*(const mint a) const { return mint(*this) *= a; }
	friend mint operator+(const long long a, const mint b) { return mint(a) + b; }
	friend mint operator-(const long long a, const mint b) { return mint(a) - b; }
	friend mint operator*(const long long a, const mint b) { return mint(a) * b; }
	mint pow(long long t) const {
		if (t < 0) return pow(-t).inv();
		if (t == 0) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}
	mint inv() const { return pow(mod - 2); }
	mint &operator/=(const mint a) { return *this *= a.inv(); }
	mint operator/(const mint a) const { return mint(*this) /= a; }
	friend mint operator/(const long long a, const mint b) { return mint(a) / b; }
	bool operator==(const mint a) const { return _x == a._x; }
	bool operator!=(const mint a) const { return _x != a._x; }
	friend istream &operator>>(istream &is, mint &a) {
		long long x;
		is >> x;
		a = mint(x);
		return is;
	}
	friend ostream &operator<<(ostream &os, const mint &a) {
		os << a.val();
		return os;
	}
private:
	long long _x;
};
// using mint = static_mint<1000000007>;
// using mint = static_mint<998244353>;

using mint = static_mint<998244353>;

void _main() {
	ll T; cin >> T;
	rep(_, T) {
		ll N, A; cin >> N >> A;
		if (A == 1) {
			cout << mint(N)*(N-1)/2 << el;
			continue;
		}
		mint ans = 0;
		ll p = N;
		ll x = 0;
		while (p) {
			ll np = p/A;
			mint d = p-np;
			ans += d*(d-1)/2+d*x;
			x += p-np*A+1;
			p = np;
		}
		cout << ans << el;
	}
}
0