結果
問題 | No.1193 Penguin Sequence |
ユーザー | qumazaki |
提出日時 | 2020-09-16 03:07:19 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 747 ms / 2,000 ms |
コード長 | 2,835 bytes |
コンパイル時間 | 299 ms |
コンパイル使用メモリ | 82,712 KB |
実行使用メモリ | 156,180 KB |
最終ジャッジ日時 | 2024-06-22 02:58:30 |
合計ジャッジ時間 | 21,609 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 342 ms
147,828 KB |
testcase_01 | AC | 705 ms
155,732 KB |
testcase_02 | AC | 718 ms
153,440 KB |
testcase_03 | AC | 722 ms
153,432 KB |
testcase_04 | AC | 730 ms
156,180 KB |
testcase_05 | AC | 725 ms
153,516 KB |
testcase_06 | AC | 714 ms
156,176 KB |
testcase_07 | AC | 724 ms
155,716 KB |
testcase_08 | AC | 726 ms
153,624 KB |
testcase_09 | AC | 719 ms
153,564 KB |
testcase_10 | AC | 747 ms
155,672 KB |
testcase_11 | AC | 384 ms
109,812 KB |
testcase_12 | AC | 381 ms
110,240 KB |
testcase_13 | AC | 579 ms
128,208 KB |
testcase_14 | AC | 535 ms
121,416 KB |
testcase_15 | AC | 674 ms
153,568 KB |
testcase_16 | AC | 267 ms
137,572 KB |
testcase_17 | AC | 54 ms
64,504 KB |
testcase_18 | AC | 122 ms
85,240 KB |
testcase_19 | AC | 684 ms
155,528 KB |
testcase_20 | AC | 515 ms
119,592 KB |
testcase_21 | AC | 437 ms
113,348 KB |
testcase_22 | AC | 133 ms
86,124 KB |
testcase_23 | AC | 374 ms
108,868 KB |
testcase_24 | AC | 333 ms
103,648 KB |
testcase_25 | AC | 187 ms
91,772 KB |
testcase_26 | AC | 109 ms
83,052 KB |
testcase_27 | AC | 576 ms
122,844 KB |
testcase_28 | AC | 413 ms
110,540 KB |
testcase_29 | AC | 551 ms
123,396 KB |
testcase_30 | AC | 248 ms
98,576 KB |
testcase_31 | AC | 203 ms
94,380 KB |
testcase_32 | AC | 462 ms
115,708 KB |
testcase_33 | AC | 345 ms
106,356 KB |
testcase_34 | AC | 290 ms
101,208 KB |
testcase_35 | AC | 354 ms
106,216 KB |
testcase_36 | AC | 293 ms
101,060 KB |
testcase_37 | AC | 554 ms
121,500 KB |
testcase_38 | AC | 54 ms
63,984 KB |
testcase_39 | AC | 54 ms
63,500 KB |
testcase_40 | AC | 54 ms
63,832 KB |
ソースコード
class fenwick_tree(): def __init__(self, n:int, mod:int = 0): self.__mod = mod self.__n = n self.__data = [0] * self.__n def add(self, p:int, x:int): assert (0 <= p) & (p < self.__n) if(self.__mod == 0): self.__add_mod0(p,x) else: self.__add_mod(p,x) def __add_mod0(self, p:int, x:int): p+=1 while( p<= self.__n): self.__data[p-1] += x p += p & -p def __add_mod(self, p:int, x:int): p+=1 while( p<= self.__n): self.__data[p-1] += x self.__data[p-1] %= self.__mod p += p & -p def sum(self, l:int, r:int): assert (0 <= l) & (l <= r) & (r <= self.__n) if(self.__mod == 0): return self.__sum_mod0(r) - self.__sum_mod0(l) else: return self.__sum_mod(r) - self.__sum_mod(l) def __sum_mod0(self, r:int): s = 0 while(r > 0): s += self.__data[r-1] r -= r & -r return s def __sum_mod(self, r:int): s = 0 while(r > 0): s += self.__data[r-1] s %= self.__mod r -= r & -r return s n = int(input()) a = list(map(int,input().split())) mod = 998244353 ## nCkのmodを求める関数 # テーブルを作る(前処理) max_n = 2 * 10**5 + 100 fac, finv, inv = [0]*max_n, [0]*max_n, [0]*max_n def comInit(max_n): fac[0] = fac[1] = 1 finv[0] = finv[1] = 1 inv[1] = 1 for i in range(2,max_n): fac[i] = fac[i-1]* i% mod inv[i] = mod - inv[mod%i] * (mod // i) % mod finv[i] = finv[i-1] * inv[i] % mod comInit(max_n) # 二項係数の計算 def com(n,k): if(n < k): return 0 if( (n<0) | (k < 0)): return 0 return fac[n] * (finv[k] * finv[n-k] % mod) % mod inverse = 0 a_ind = [[ai,i] for i,ai in enumerate(a)] a_ind.sort(key= lambda x: -1*x[0]*10**6 - x[1]) d = {} bit = fenwick_tree(n) for ai,i in a_ind: inverse += bit.sum(0,i+1) bit.add(i,1) if(not ai in d): d[ai] = 1 else: d[ai] += 1 inverse %= mod inverse_all = n*(n-1)//2 for i in d.values(): inverse_all -= i*(i-1)//2 inverse_all %= mod combs = [1] * (n+1) comb_l = [1] * (n+1) comb_r = [1] * (n+2) one = [0] * (n+1) pair = [0] * (n+1) for i in range(1,n+1): combs[i] = com(n,i) comb_l[i] = (comb_l[i-1] * combs[i])%mod one[i] = com(n-1,i-1) pair[i] = com(n-2,i-2) for i in range(n,0,-1): comb_r[i] = (comb_r[i+1]*combs[i])%mod ans = 0 ones = 0 for i in range(1,n+1): ans += ones * inverse_all * comb_r[i+1] * one[i] ans %= mod ones = (ones * combs[i] + comb_l[i-1] * one[i])%mod # print(i,ans) ans += pair[i] * inverse * comb_l[i-1] * comb_r[i+1] ans %= mod # print(i,ans) print(ans)