結果

問題 No.1897 Sum of 2nd Max
ユーザー polylogKpolylogK
提出日時 2021-12-18 13:56:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 4,149 bytes
コンパイル時間 756 ms
コンパイル使用メモリ 78,136 KB
実行使用メモリ 6,144 KB
最終ジャッジ日時 2024-05-06 06:43:06
合計ジャッジ時間 1,810 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 11 ms
5,888 KB
testcase_04 AC 11 ms
5,888 KB
testcase_05 AC 6 ms
5,376 KB
testcase_06 AC 7 ms
5,376 KB
testcase_07 AC 7 ms
5,376 KB
testcase_08 AC 6 ms
5,376 KB
testcase_09 AC 11 ms
6,016 KB
testcase_10 AC 10 ms
5,888 KB
testcase_11 AC 10 ms
6,144 KB
testcase_12 AC 10 ms
5,888 KB
testcase_13 AC 10 ms
5,888 KB
testcase_14 AC 10 ms
5,888 KB
testcase_15 AC 10 ms
6,144 KB
testcase_16 AC 9 ms
5,888 KB
testcase_17 AC 10 ms
5,888 KB
testcase_18 AC 10 ms
5,888 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 10 ms
5,888 KB
testcase_31 AC 10 ms
5,888 KB
testcase_32 AC 7 ms
5,376 KB
testcase_33 AC 11 ms
5,888 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
D_modint.cpp:1:18: warning: extra tokens at end of #include directive

ソースコード

diff #

#line 1 "D_modint.cpp"
#include <vector>;

#line 1 "/home/ecasdqina/cpcpp/libs/library_cpp/math/modint.hpp"



#include <iostream>

namespace cplib {
template <std::uint_fast64_t Modulus>
class modint {
	using u32 = std::uint_fast32_t;
	using u64 = std::uint_fast64_t;
	using i32 = std::int_fast32_t;
	using i64 = std::int_fast64_t;

	inline u64 apply(i64 x) { return (x < 0 ? x + Modulus : x); };

public:
	u64 a;
	static constexpr u64 mod = Modulus;

	constexpr modint(const i64& x = 0) noexcept: a(apply(x % (i64)Modulus)) {}

	constexpr modint operator+(const modint& rhs) const noexcept { return modint(*this) += rhs; }
	constexpr modint operator-(const modint& rhs) const noexcept { return modint(*this) -= rhs; }
	constexpr modint operator*(const modint& rhs) const noexcept { return modint(*this) *= rhs; }
	constexpr modint operator/(const modint& rhs) const noexcept { return modint(*this) /= rhs; }
	constexpr modint operator^(const u64& k) const noexcept { return modint(*this) ^= k; }
	constexpr modint operator^(const modint& k) const noexcept { return modint(*this) ^= k.value(); }
	constexpr modint operator-() const noexcept { return modint(Modulus - a); }
	constexpr modint operator++() noexcept { return (*this) = modint(*this) + 1; }
	constexpr modint operator--() noexcept { return (*this) = modint(*this) - 1; }
	const bool operator==(const modint& rhs) const noexcept { return a == rhs.a; };
	const bool operator!=(const modint& rhs) const noexcept { return a != rhs.a; };
	const bool operator<=(const modint& rhs) const noexcept { return a <= rhs.a; };
	const bool operator>=(const modint& rhs) const noexcept { return a >= rhs.a; };
	const bool operator<(const modint& rhs) const noexcept { return a < rhs.a; };
	const bool operator>(const modint& rhs) const noexcept { return a > rhs.a; };
	constexpr modint& operator+=(const modint& rhs) noexcept {
		a += rhs.a;
		if (a >= Modulus) a -= Modulus;
		return *this;
	}
	constexpr modint& operator-=(const modint& rhs) noexcept {
		if (a < rhs.a) a += Modulus;
		a -= rhs.a;
		return *this;
	}
	constexpr modint& operator*=(const modint& rhs) noexcept {
		a = a * rhs.a % Modulus;
		return *this;
	}
	constexpr modint& operator/=(modint rhs) noexcept {
		u64 exp = Modulus - 2;
		while (exp) {
			if (exp % 2) (*this) *= rhs;

			rhs *= rhs;
			exp /= 2;
		}
		return *this;
	}
	constexpr modint& operator^=(u64 k) noexcept {
		auto b = modint(1);
		while(k) {
			if(k & 1) b = b * (*this);
			(*this) *= (*this);
			k >>= 1;
		}
		return (*this) = b;
	}
	constexpr modint& operator=(const modint& rhs) noexcept {
		a = rhs.a;
		return (*this);
	}

	const modint inverse() const {
		return modint(1) / *this;
	}
	const modint power(i64 k) const {
		if(k < 0) return modint(*this).inverse() ^ (-k);
		return modint(*this) ^ k;
	}

	explicit operator bool() const { return a; }
	explicit operator u64() const { return a; }
	constexpr u64& value() noexcept { return a; }
	constexpr const u64& value() const noexcept { return a; }

	friend std::ostream& operator<<(std::ostream& os, const modint& p) {
		return os << p.a;
	}
	friend std::istream& operator>>(std::istream& is, modint& p) {
		u64 t;
		is >> t;
		p = modint(t);
		return is;
	}
};
}


#line 4 "D_modint.cpp"
using mint = cplib::modint<998244353>;

int main() {
	int n, k; scanf("%d%d", &n, &k);

	// 線形篩
	std::vector<int> primes, minp(k + 1, -1);
	for(int i = 2; i < minp.size(); i++) {
		if(minp[i] == -1) {
			minp[i] = i;
			primes.push_back(i);
		}

		for(int p: primes) {
			if(p * i >= minp.size() or p > minp[i]) break;

			minp[p * i] = p;
		}
	}

	// べき乗列挙
	std::vector<mint> powers(k + 1); powers[1] = 1;
	for(int p: primes) powers[p] = mint(p).power(n - 1);
	for(int i = 4; i < powers.size(); i++) if(minp[i] != i) {
		powers[i] = powers[i / minp[i]] * powers[minp[i]];
	}

	auto f = [&](int x) -> mint {
		const mint A = powers[k] * k;
		const mint B = powers[k - x] * x * n;
		const mint C = powers[k - x] * (k - x);
		return A - B - C;
	};

	mint ans = 0;
	for(int i = 1; i <= k; i++) {
		mint count = f(i) - f(i - 1);
		ans += count * (k - i + 1);
	}
	printf("%lld\n", ans.value());
}
0