結果

問題 No.2512 Mountain Sequences
ユーザー 👑 chro_96chro_96
提出日時 2023-10-20 23:08:19
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 3,006 bytes
コンパイル時間 1,598 ms
コンパイル使用メモリ 32,376 KB
実行使用メモリ 14,316 KB
最終ジャッジ日時 2023-10-20 23:08:39
合計ジャッジ時間 17,986 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
12,144 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 14 ms
12,144 KB
testcase_04 AC 14 ms
12,144 KB
testcase_05 AC 14 ms
12,144 KB
testcase_06 WA -
testcase_07 AC 14 ms
12,144 KB
testcase_08 AC 15 ms
12,144 KB
testcase_09 AC 13 ms
12,144 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 83 ms
12,968 KB
testcase_26 AC 83 ms
12,968 KB
testcase_27 AC 86 ms
12,968 KB
testcase_28 AC 316 ms
14,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>

#define RANGE_SIZE 450

int cmp_mo (const void *a, const void *b) {
  int *a_ = (int *)a;
  int *b_ = (int *)b;
  
  if (a_[0]/RANGE_SIZE < b_[0]/RANGE_SIZE) {
    return -1;
  }
  
  if (a_[0]/RANGE_SIZE > b_[0]/RANGE_SIZE) {
    return 1;
  }
  
  if (a_[1] < b_[1]) {
    return -1;
  }
  
  if (a_[1] > b_[1]) {
    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;
}

int main () {
  int t = 0;
  int nm[200000][3] = {};
  
  int res = 0;
  
  long long ans[200000] = {};
  long long mod_num = 998244353LL;
  
  long long fact[400001] = {};
  long long invf[400001] = {};
  
  long long inv2 = (mod_num+1LL)/2LL;
  
  res = scanf("%d", &t);
  for (int i = 0; i < t; i++) {
    res = scanf("%d", nm[i]);
    res = scanf("%d", nm[i]+1);
    nm[i][2] = i;
  }
  
  fact[0] = 1LL;
  for (int i = 0; i < 400000; i++) {
    fact[i+1] = fact[i];
    fact[i+1] *= (long long)(i+1);
    fact[i+1] %= mod_num;
  }
  
  invf[400000] = power_mod(fact[400000], mod_num-2LL, mod_num);
  for (int i = 400000; i > 0; i--) {
    invf[i-1] = invf[i];
    invf[i-1] *= (long long)i;
    invf[i-1] %= mod_num;
  }
  
  for (int i = 0; i < nm[0][1]; i++) {
    if (2*i >= nm[0][0]-1) {
      long long tmp = (fact[2*i]*invf[nm[0][0]-1])%mod_num;
      ans[nm[0][2]] += (tmp*invf[2*i-nm[0][0]+1])%mod_num;
    }
  }
  ans[nm[0][2]] %= mod_num;
  
  qsort(nm, t, sizeof(int)*3, cmp_mo);
  
  for (int i = 1; i < t; i++) {
    int pn = nm[i-1][0];
    int pm = nm[i-1][1];
    ans[nm[i][2]] = ans[nm[i-1][2]];
    while (pn != nm[i][0] || pm != nm[i][1]) {
      if (pn < nm[i][0]) {
        long long tmp = 0LL;
        if (2*pm > pn) {
          tmp = (fact[2*pm]*invf[pn+1])%mod_num;
          tmp = (tmp*invf[2*pm-pn-1])%mod_num;
        }
        ans[nm[i][2]] = (tmp+mod_num-ans[nm[i][2]])%mod_num;
        ans[nm[i][2]] *= inv2;
        ans[nm[i][2]] %= mod_num;
        pn++;
      } else if (pn > nm[i][0]) {
        long long tmp = 0LL;
        if (2*pm >= pn) {
          tmp = (fact[2*pm]*invf[pn])%mod_num;
          tmp = (tmp*invf[2*pm-pn])%mod_num;
        }
        ans[nm[i][2]] = (tmp+2LL*mod_num-2LL*ans[nm[i][2]])%mod_num;
        pn--;
      } else if (pm < nm[i][1]) {
        if (2*pm >= pn-1) {
          long long tmp = (fact[2*pm]*invf[pn-1])%mod_num;
          ans[nm[i][2]] += tmp*invf[2*pm-pn+1];
          ans[nm[i][2]] %= mod_num;
        }
        pm++;
      } else {
        if (2*pm-2 >= pn-1) {
          long long tmp = (fact[2*pm-2]*invf[pn-1])%mod_num;
          ans[nm[i][2]] += mod_num-(tmp*invf[2*pm-pn-1])%mod_num;
          ans[nm[i][2]] %= mod_num;
        }
        pm--;
      }
    }
  }
  
  for (int i = 0; i < t; i++) {
    printf("%lld\n", ans[i]);
  }
  
  return 0;
}
0