結果
| 問題 |
No.802 だいたい等差数列
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-26 15:50:46 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 170 ms / 2,000 ms |
| コード長 | 2,175 bytes |
| コンパイル時間 | 241 ms |
| コンパイル使用メモリ | 82,204 KB |
| 実行使用メモリ | 101,368 KB |
| 最終ジャッジ日時 | 2025-03-26 15:51:49 |
| 合計ジャッジ時間 | 4,188 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 30 |
ソースコード
MOD = 10**9 + 7
def main():
import sys
input = sys.stdin.read
data = input().split()
N = int(data[0])
M = int(data[1])
D1 = int(data[2])
D2 = int(data[3])
n = N - 1
S_low = n * D1
S_high = min(n * D2, M - 1)
if S_low > S_high:
print(0)
return
T_max = S_high - S_low
c = D2 - D1
if c == 0:
print((M - S_low) % MOD)
return
max_fact = T_max + n + (n - 1) + 2
max_fact = min(max_fact, 2 * 10**6)
fact = [1] * (max_fact + 1)
for i in range(1, max_fact + 1):
fact[i] = fact[i - 1] * i % MOD
inv_fact = [1] * (max_fact + 1)
inv_fact[max_fact] = pow(fact[max_fact], MOD - 2, MOD)
for i in range(max_fact - 1, -1, -1):
inv_fact[i] = inv_fact[i + 1] * (i + 1) % MOD
def comb(a, b):
if a < 0 or b < 0 or a < b:
return 0
return fact[a] * inv_fact[b] % MOD * inv_fact[a - b] % MOD
sum_A = 0
max_k = (T_max + n) // (c + 1)
for k in range(0, max_k + 1):
rem = T_max + n - k * (c + 1)
if rem < 0:
continue
term_n_k = comb(n, k)
term_comb = comb(rem, n)
term = term_n_k * term_comb % MOD
if k % 2 == 1:
term = (-term) % MOD
sum_A = (sum_A + term) % MOD
sum_B = 0
e_max = min(c, T_max)
for e in range(0, e_max + 1):
current_T = T_max - e
if current_T < 0:
break
temp_sum = 0
max_k_current = (current_T + (n - 1)) // (c + 1)
for k in range(0, max_k_current + 1):
rem = current_T + (n - 1) - k * (c + 1)
if rem < 0:
continue
term_n_1_k = comb(n - 1, k)
term_comb = comb(rem, n - 1)
term = term_n_1_k * term_comb % MOD
if k % 2 == 1:
term = (-term) % MOD
temp_sum = (temp_sum + term) % MOD
sum_B = (sum_B + e * temp_sum) % MOD
sum_B = sum_B * n % MOD
answer = ((M - S_low) * sum_A - sum_B) % MOD
if answer < 0:
answer += MOD
print(answer)
if __name__ == "__main__":
main()
lam6er