解题思路:
先创建全部次方的数组(二维数组存放),再进行前缀和运算。最后按题目要求进行区间和运算。
题解:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 10;//数组不能开太大,会报错
const ll p = 1e9 + 7;
ll a[6][N], prefix[6][N];//数组a前面6表示开六个长度为N的数组,且6还表示是几次方。
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m;cin >> n >> m;
for(int i = 1; i <= n; i++)cin >> a[1][i];
for(int i = 2; i <= 5; i++)//表示基于原数组,创立i次方的数组
{
for(int j = 1; j <= n; j++)
a[i][j] = a[i - 1][j] * a[1][j] % p;
//运算过程中取余,避免数据溢出。不会对最终结果造成影响。一般像这种要求对结果取余的都会溢出
}
for(int i = 1; i <= 5; i++)//前缀和模板
{
for(int j = 1; j <= n; j++)
{
prefix[i][j] = (prefix[i][j - 1] + a[i][j]) % p;
}
}
while(m--)//区间和
{
int l, r, k;cin >> l >> r >> k;
cout << (prefix[k][r] - prefix[k][l - 1] + p) % p << '\n';
}
return 0;
}