题解 | #雾粉与数论# 我和Mathematica,ChatGPT合力砍下牛客练习赛126的B题
雾粉与数论
https://ac.nowcoder.com/acm/contest/84527/B
我和Mathematica,ChatGPT合力砍下牛客练习赛126的B题。我们仨个真厉害。
题目描述
结果对1e9+7取模。
代码
# Clear[n]
# f[n_] := Sum[GCD[i*(i - 1)/2, i*(i + 1)/2], {i, 2, n}]
# f /@ Range[1, 30, 1]
# FindSequenceFunction[%5713, n]
# ```
# \frac{1}{16} (-1)^n \left(6 (-1)^n n^2+6 (-1)^n n-2 n-15 (-1)^n-1\right)
# ```
# rewrite it in python code.
# refine my code.
n = int(input())
mod = 1000000007
# Calculate components of the expression
sign = (-1) ** n
n_squared = n ** 2
# Define the expression with precomputed components
# The (1/16) factor will be handled by multiplying by the modular inverse of 16 mod 1000000007
expression = sign * (6 * sign * n_squared + 6 * sign * n - 2 * n - 15 * sign - 1)
# Modular inverse of 16 mod 1000000007
mod_inverse_16 = pow(16, mod - 2, mod)
# Compute the result with integer arithmetic and apply the modular inverse
result = (expression * mod_inverse_16) % mod
print(result)