結果

問題 No.1897 Sum of 2nd Max
ユーザー polylogK
提出日時 2021-12-12 21:29:29
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 101 ms / 2,000 ms
コード長 772 bytes
コンパイル時間 2,048 ms
コンパイル使用メモリ 193,312 KB
最終ジャッジ日時 2025-01-26 09:18:24
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:15:29: warning: format ‘%lld’ expects argument of type ‘long long int*’, but argument 2 has type ‘i64*’ {aka ‘long int*’} [-Wformat=]
   15 |         i64 n, k; scanf("%lld%lld", &n, &k);
      |                          ~~~^       ~~
      |                             |       |
      |                             |       i64* {aka long int*}
      |                             long long int*
      |                          %ld
main.cpp:15:33: warning: format ‘%lld’ expects argument of type ‘long long int*’, but argument 3 has type ‘i64*’ {aka ‘long int*’} [-Wformat=]
   15 |         i64 n, k; scanf("%lld%lld", &n, &k);
      |                              ~~~^       ~~
      |                                 |       |
      |                                 |       i64* {aka long int*}
      |                                 long long int*
      |                              %ld
main.cpp:32:20: warning: format ‘%lld’ expects argument of type ‘long long int’, but argument 2 has type ‘i64’ {aka ‘long int’} [-Wformat=]
   32 |         printf("%lld\n", ans);
      |                 ~~~^     ~~~
      |                    |     |
      |                    |     i64 {aka long int}
      |                    long long int
      |                 %ld
main.cpp:15:24: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   15 |         i64 n, k; scanf("%lld%lld", &n, &k);
      |                   ~~~~~^~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
using i64 = std::int_fast64_t;

i64 powmod(i64 x, i64 n, int mod = 998244353) {
	i64 ret = 1;
	while(n) {
		if(n & 1) (ret *= x) %= mod;
		(x *= x) %= mod;
		n >>= 1;
	}
	return ret;
}

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

	const int MOD = 998244353;

	auto f = [&](i64 x) -> i64 { // ソート後、二番目の要素が x 以下であるような組合せ数
		const i64 A = powmod(k, n);
		const i64 B = n * x % MOD * powmod(k - x, n - 1) % MOD;
		const i64 C = powmod(k - x, n);
		return ((A - B - C) % MOD + MOD) % MOD;
	};

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