Given integers . There are 3 types of operations:
- 1 l r v: Multiply by ;
- 2 l r k: Change each element in to ;
- 3 l r: Query the multiplication of modulo 1,000,000,007.
For each query, please output the answer.
InputThere are multiple test cases. The first line of the input is an integer (), indicating the number of test cases. For each test case:
The first line contains two integers () and (), indicating the number of the given integers and the number of operations.
The second line contains integers (), indicating the given integers.
The first integer on each of the following lines will be (), indicating the type of operation.
- If equals 1, then three integers (, ) follow, indicating the first type of operation.
- If equals 2, then three integers (, ) follow, indicating the second type of operation.
- If equals 3, then two integers () follow, indicating an query.
It's guaranteed that neither the sum of nor the sum of over all test cases will exceed .
<h4< dd="">OutputFor each query, output one line containing one integer, indicating the answer.
<h4< dd="">Sample Input1 5 5 1 2 1 2 1 3 2 4 1 1 5 2 3 2 4 2 1 1 4 3 1 1<h4< dd="">Sample Output
4 32 16
题意:给N个数,操作1是区间内每个数乘上v,操作2是区间内每个数变k次幂,查询区间数的乘积。
思路:两个延迟数组,分别记录系数和指数,显然操作1就是乘上pow(v, size),所以size可以提出来最后再算,对于操作2的指数就是直接乘起来了,根据费马小定理指数相乘需要对(mod-1)取模。然后要处理pushdown怎么写,父节点只有操作2会对儿子产生影响,操作1可以分离开来直接传到儿子的系数数组相乘,用纸模拟下就知道了。
# include <bits/stdc++.h>
# define lson l, mid, id<<1
# define rson mid+1, r, id<<1|1
using namespace std;
typedef long long LL;
const LL mod = 1e9+7;
const int maxn = 1e5+30;
LL sum[maxn*3], xi[maxn*3], zi[maxn*3];
template <class T>
inline void scan(T &ret)
{
char c;
ret = 0;
while ((c = getchar()) < '0' || c > '9');
while (c >= '0' && c <= '9') ret = ret * 10 + (c - '0'), c = getchar();
}
template <class T>
void out(T x)
{
if(x/10) out(x/10);
putchar('0'+x%10);
}
LL qmod(LL a, LL b, LL c)
{
LL res = 1;
for(;b;b>>=1)
{
if(b&1) res = res*a%c;
a=a*a%c;
}
return res;
}
void build(int l, int r, int id)
{
xi[id] = zi[id] = 1LL;
if(l==r)
{
scanf("%lld",&sum[id]);
return;
}
int mid=l+r>>1;
build(lson);
build(rson);
sum[id]=sum[id<<1]*sum[id<<1|1]%mod;
}
void pushdown(int id, int len)
{
zi[id<<1] = zi[id<<1]*zi[id]%(mod-1);
zi[id<<1|1] = zi[id<<1|1]*zi[id]%(mod-1);
xi[id<<1]=qmod(xi[id<<1], zi[id], mod)*xi[id]%mod;
xi[id<<1|1]=qmod(xi[id<<1|1], zi[id], mod)*xi[id]%mod;
sum[id<<1] = qmod(sum[id<<1], zi[id], mod)*qmod(xi[id], len+1>>1, mod)%mod;
sum[id<<1|1] = qmod(sum[id<<1|1], zi[id], mod)*qmod(xi[id], len>>1, mod)%mod;
xi[id] = zi[id] = 1LL;
}
void update(int l, int r, int id, int L, int R, LL x, LL y)
{
if(L<=l && R>=r)
{
zi[id] = zi[id]*x%(mod-1);//Fermat's little theorem
xi[id] = qmod(xi[id], x, mod)*y%mod;
sum[id] = qmod(sum[id], x, mod)*qmod(y, r-l+1, mod)%mod;
return;
}
pushdown(id, r-l+1);
int mid = l+r>>1;
if(L<=mid) update(lson, L, R, x, y);
if(R>mid) update(rson, L, R, x, y);
sum[id]=sum[id<<1]*sum[id<<1|1]%mod;
}
LL query(int l, int r, int id, int L, int R)
{
if(L<=l && R>=r) return sum[id];
pushdown(id, r-l+1);
int mid = l+r>>1;
LL res = 1LL;
if(L<=mid) res = res*query(lson, L, R)%mod;
if(R>mid) res = res*query(rson, L, R)%mod;
return res;
}
int main()
{
int T, l ,r, n, m;
LL x;
scan(T);
while(T--)
{
scan(n);scan(m);
build(1, n, 1);
while(m--)
{
int op;
scan(op);
if(op == 1)
{
scan(l);scan(r);scan(x);
update(1, n, 1, l, r, 1, x);
}
else if(op == 2)
{
scan(l);scan(r);scan(x);
update(1, n, 1, l, r, x, 1);
}
else
{
scan(l);scan(r);
out(query(1, n, 1, l, r));
putchar('\n');
}
}
}
return 0;
}