思路:首先看到子数组,多次查询,首先要想到前缀和。不过要注意两点:1.的值不包括0,因此不用担心后续除到0的情况;2.
乘积之后的值可能会非常大,如果不提前取mod的话,肯定会超时。因此我们处理一个带mod的前缀和数组pre,后续查询就采用pre[r] // pre[l - 1]然后取mod得到最终结果
对于有除法的取模,我们需要用到小费马定理。具体来说,要求mod是质数,此时有。很常用的公式,记住即可。然后对于Python来说,pow内置函数的功能类似于快速幂,直接用即可;对于其他语言来说,要手搓一个qpow出来才行
代码:
import sys
input = lambda: sys.stdin.readline().strip()
import math
inf = 10 ** 18
def I():
return input()
def II():
return int(input())
def MII():
return map(int, input().split())
def GMI():
return map(lambda x: int(x) - 1, input().split())
def LI():
return input().split()
def LII():
return list(map(int, input().split()))
def LFI():
return list(map(float, input().split()))
fmax = lambda x, y: x if x > y else y
fmin = lambda x, y: x if x < y else y
isqrt = lambda x: int(math.sqrt(x))
'''
'''
def solve():
n, q = MII()
a = LII()
mod = 10 ** 9 + 7
pre = [1] * (n + 1)
for i in range(n):
pre[i + 1] = (pre[i] * a[i]) % mod
out = []
for _ in range(q):
l, r = MII()
out.append((pre[r] * pow(pre[l - 1], mod - 2, mod)) % mod)
print(' '.join(map(str, out)))
t = 1
# t = II()
for _ in range(t):
solve()

京公网安备 11010502036488号