結果

問題 No.1696 Nonnil
ユーザー 👑 platinumplatinum
提出日時 2020-11-20 12:48:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 514 ms / 3,500 ms
コード長 2,136 bytes
コンパイル時間 1,144 ms
コンパイル使用メモリ 86,200 KB
実行使用メモリ 134,108 KB
最終ジャッジ日時 2023-09-26 15:36:10
合計ジャッジ時間 11,327 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,432 KB
testcase_01 AC 2 ms
7,484 KB
testcase_02 AC 2 ms
7,476 KB
testcase_03 AC 114 ms
97,032 KB
testcase_04 AC 3 ms
7,672 KB
testcase_05 AC 2 ms
7,432 KB
testcase_06 AC 2 ms
7,656 KB
testcase_07 AC 3 ms
7,496 KB
testcase_08 AC 72 ms
59,812 KB
testcase_09 AC 39 ms
18,260 KB
testcase_10 AC 60 ms
22,772 KB
testcase_11 AC 41 ms
15,808 KB
testcase_12 AC 51 ms
22,076 KB
testcase_13 AC 39 ms
15,992 KB
testcase_14 AC 338 ms
119,228 KB
testcase_15 AC 111 ms
27,288 KB
testcase_16 AC 402 ms
133,708 KB
testcase_17 AC 255 ms
109,148 KB
testcase_18 AC 203 ms
61,528 KB
testcase_19 AC 2 ms
7,416 KB
testcase_20 AC 3 ms
7,448 KB
testcase_21 AC 195 ms
88,548 KB
testcase_22 AC 243 ms
85,004 KB
testcase_23 AC 232 ms
77,320 KB
testcase_24 AC 221 ms
73,168 KB
testcase_25 AC 339 ms
106,048 KB
testcase_26 AC 278 ms
108,996 KB
testcase_27 AC 340 ms
130,908 KB
testcase_28 AC 295 ms
79,680 KB
testcase_29 AC 234 ms
97,412 KB
testcase_30 AC 181 ms
70,132 KB
testcase_31 AC 257 ms
115,852 KB
testcase_32 AC 293 ms
125,204 KB
testcase_33 AC 310 ms
90,740 KB
testcase_34 AC 208 ms
92,624 KB
testcase_35 AC 270 ms
102,880 KB
testcase_36 AC 514 ms
133,988 KB
testcase_37 AC 487 ms
134,008 KB
testcase_38 AC 484 ms
134,108 KB
testcase_39 AC 360 ms
97,768 KB
testcase_40 AC 449 ms
115,256 KB
testcase_41 AC 199 ms
35,512 KB
testcase_42 AC 118 ms
103,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cassert>
#include <algorithm>
#define rep(i,n) for(int i=0; i<(int)(n); i++)

using namespace std;
using LL = long long;
using P = pair<int,int>;

const int Max_N = 1e9;
const int Max_K = 1500;
const int Max_M = 2e5;
const LL mod = 998244353;

LL modpow(LL x, LL n){
  LL r = 1;
  while(n){
    if(n & 1) r = r * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return r;
}

LL dp[2000][2000][2];
LL sum1[2000][2000][2], sum2[4000][2000][2];

int main(){
  int N, K, M;
  cin >> N >> K >> M;
  assert(1 <= N and N <= Max_N);
  assert(1 <= K and K <= Max_K);
  assert(1 <= M and M <= min(Max_M, K * (K+1) / 2));
  vector<P> S(M);
  vector<int> L(1), R(1);
  vector<bool> erase(M);
  rep(i,M){
    int l, r;
    cin >> l >> r;
    assert(1 <= l and l <= r and r <= K);
    S[i] = P(l, r);
  }
  sort(S.begin(), S.end());
  rep(i,M-1) assert(S[i] != S[i+1]);
  rep(i,M-1) if(S[i+1].second <= S[i].second) erase[i] = true;
  rep(i,M-1){
    if(erase[i]) continue;
    for(int j=i+1; j<M; j++){
      if(S[j].second <= S[i].second){
        erase[i] = true;
        break;
      }
    }
  }
  rep(i,M) if(!erase[i]){
    L.push_back(S[i].first);
    R.push_back(S[i].second);
  }
  M = L.size();
  sum1[0][0][0] = 1, sum2[Max_K][0][0] = 1;
  rep(i,M-1){
    int l = L[i+1], r = R[i+1], d = r - l + 1;
    int pre_r = R[i];
    int s = *(lower_bound(R.begin(), R.end(), l) - 1);
    rep(j,K+1){
      rep(p,2){
        if(j >= d) dp[j][r][p] += sum1[j-d][s][p^1];
        dp[j][r][p] += sum2[j-r+Max_K][pre_r][p^1] - sum2[j-r+Max_K][s][p^1];
        dp[j][r][p] %= mod;
        (sum1[j][r][p] += sum1[j][pre_r][p] + dp[j][r][p]) %= mod;
        (sum2[j-r+Max_K][r][p] += sum2[j-r+Max_K][pre_r][p] + dp[j][r][p]) %= mod;
      }
    }
  }
  vector<LL> power(K+1);
  rep(i,K+1) power[i] = modpow(i, N);
  LL res = 0, ans = 0;
  rep(j,K+1){
    rep(k,K+1){
      rep(p,2) (dp[j][k][p] *= power[K-j]) %= mod;
    }
  }
  rep(j,K+1){
    rep(k,K+1) (res += dp[j][k][1] - dp[j][k][0]) %= mod;
  }
  ans = (modpow(K, N) - res) % mod;
  while(ans < 0) ans += mod;
  cout << ans << endl;

  return 0;
}
0