結果
問題 | No.2197 Same Dish |
ユーザー |
![]() |
提出日時 | 2023-01-20 22:21:16 |
言語 | C (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 73 ms / 2,000 ms |
コード長 | 1,938 bytes |
コンパイル時間 | 1,271 ms |
コンパイル使用メモリ | 31,232 KB |
実行使用メモリ | 5,632 KB |
最終ジャッジ日時 | 2024-06-23 10:18:34 |
合計ジャッジ時間 | 2,111 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 20 |
ソースコード
#include <stdio.h>#include <stdlib.h>#define SEGT_SIZE 262144int cmp_int (const void *ap, const void *bp) {int a = *(int *)ap;int b = *(int *)bp;if (a < b) {return -1;}if (a > b) {return 1;}return 0;}long long power_mod (long long a, long long b, long long mod_num) {long long ans = 1LL;if (b > 0LL) {ans = power_mod(a, b/2LL, mod_num);ans = (ans * ans) % mod_num;if (b%2LL == 1LL) {ans = (ans * (a % mod_num)) % mod_num;}}return ans;}void set_segt (int *t, int idx, int val, int size) {idx = idx+size-1;t[idx] = val;while (idx > 0) {idx = (idx-1)/2;t[idx] = t[2*idx+1]+t[2*idx+2];}return;}int sum_segt_rec (int *t, int a, int b, int k, int l, int r) {int ans = 0;if (r <= a || b <= l) {return 0;}if (a <= l && r <= b) {return t[k];}ans = sum_segt_rec(t, a, b, 2*k+1, l, (l+r)/2);ans += sum_segt_rec(t, a, b, 2*k+2, (l+r)/2, r);return ans;}int sum_segt (int *t, int a, int b, int size) {return sum_segt_rec(t, a, b, 0, 0, size);}int main () {int n = 0;long long k = 0LL;int lr[100000][2] = {};int res = 0;long long ans = 1LL;long long mod_num = 998244353LL;int t[2*SEGT_SIZE-1] = {};res = scanf("%d", &n);res = scanf("%lld", &k);for (int i = 0; i < n; i++) {res = scanf("%d", lr[i]);res = scanf("%d", lr[i]+1);}qsort(lr, n, sizeof(int)*2, cmp_int);for (int i = 0; i < n; i++) {int cnt = sum_segt(t, 0, lr[i][0], SEGT_SIZE);int num = sum_segt(t, lr[i][1]-1, lr[i][1], SEGT_SIZE);if (k >= ((long long)(i-cnt))) {ans *= k-((long long)(i-cnt));ans %= mod_num;} else {ans = 0LL;}set_segt(t, lr[i][1]-1, num+1, SEGT_SIZE);}ans = mod_num-ans;ans += power_mod(k, (long long)n, mod_num);printf("%lld\n", ans%mod_num);return 0;}