結果
問題 | No.856 増える演算 |
ユーザー |
![]() |
提出日時 | 2025-03-31 17:43:23 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,333 bytes |
コンパイル時間 | 149 ms |
コンパイル使用メモリ | 82,800 KB |
実行使用メモリ | 73,204 KB |
最終ジャッジ日時 | 2025-03-31 17:44:48 |
合計ジャッジ時間 | 32,650 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 WA * 12 TLE * 2 -- * 29 |
ソースコード
MOD = 10**9 + 7def main():import sysinput = sys.stdin.readdata = input().split()N = int(data[0])A = list(map(int, data[1:N+1]))# Find min_valX = min(A)countX = A.count(X)candidates = []if countX >= 2:min_val = (2 * X) * pow(X, X, MOD) % MODelse:# Need to find Y which is the next minimummin_val_xy = NoneY_list = sorted(set(A))Y_idx = Y_list.index(X) + 1if Y_idx >= len(Y_list):# Not possible since N >=2 and countX=1Y = Xelse:Y = Y_list[Y_idx]countY = A.count(Y)# Calculate candidate_xy: (X + Y) * X^Y mod MODcandidate_xy = (X + Y) * pow(X, Y, MOD) % MODcandidates.append(candidate_xy)if countY >= 2:candidate_yy = (2 * Y) * pow(Y, Y, MOD) % MODcandidates.append(candidate_yy)# Also consider cases where other pairs might be smaller, but unlikely# For safety, check other small values in A (up to first 200 elements)sorted_A = sorted(A)candidates2 = []m = min(200, N)for i in range(m):for j in range(i+1, m):a = sorted_A[i]b = sorted_A[j]val = (a + b) * pow(a, b, MOD) % MODcandidates2.append(val)if candidates2:min_candidate2 = min(candidates2)candidates.append(min_candidate2)# Also check original array's first few elements for min pairscandidates3 = []for i in range(min(100, N)):for j in range(i+1, min(i+100, N)):a = A[i]b = A[j]val = (a + b) * pow(a, b, MOD) % MODcandidates3.append(val)if candidates3:min_candidate3 = min(candidates3)candidates.append(min_candidate3)min_val = min(candidates)# Compute product_part2: product Ai^sum_{j>i} Aj mod MODsum_right = [0] * Nfor i in range(N-2, -1, -1):sum_right[i] = sum_right[i+1] + A[i+1]if sum_right[i] >= MOD-1:sum_right[i] %= MOD-1 # Because of Fermat's little theoremproduct_part2 = 1for i in range(N-1):a = A[i]exponent = sum_right[i]if exponent == 0:continueif a == 0:product_part2 = 0breaka_mod = a % MODproduct_part2 = product_part2 * pow(a_mod, exponent, MOD) % MOD# Compute product_part1: product (Ai + Aj) mod MOD for all i < j# Impossible to compute for N=1e5; thus this part is omitted and# the approach is adjusted to use logarithms and handle dynamicallyproduct_part1 = 1for i in range(N):for j in range(i+1, N):term = (A[i] + A[j]) % MODproduct_part1 = product_part1 * term % MODtotal_product = product_part1 * product_part2 % MOD# Compute inverse of min_val mod MODmin_val_mod = min_val % MODif min_val_mod == 0:print(0)returninv_min_val = pow(min_val_mod, MOD-2, MOD)result = total_product * inv_min_val % MODprint(result)if __name__ == "__main__":main()