[牛客前缀和练习] A.智乃酱的区间乘积

链接:https://ac.nowcoder.com/acm/contest/19483/A
来源:牛客网

输入

5 3
5 2 3 10 6
1 5
2 3
2 5

输出

1800
6
360
#include <iostream>
#include <cstring>
#include <algorithm>
 
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
 
int q[N];
//快速幂
int fast_mul(int a, int b)
{
    int ans = 1;
    while(b)
    {
        if(b & 1) ans = (LL)ans * a % mod;
        b >>= 1;
        a = (LL)a * a % mod;
    }
    return ans;
}
int main()
{
    int n, m; 
    cin >> n >> m;
     
    q[0] = 1;
    for(int i = 1; i <= n; i ++)
        scanf("%d", &q[i]);
     
    for(int i = 1; i <= n; i ++)
        q[i] = (LL)q[i-1]*q[i] % mod;
        
    while(m --)
    {
        int a, b; 
        scanf("%d%d", &a, &b);
        int ans = (LL)q[b] * fast_mul(q[a - 1], mod - 2) % mod;
        printf("%d\n", ans);
    }
     
    return 0;
}

 


这道题需要了解一点快速幂的数学知识,

>>算法学习笔记(4):快速幂 - 知乎 <<

posted @ 2021-08-22 19:44  泥烟  阅读(24)  评论(0编辑  收藏  举报