[线段树+欧拉函数+状态压缩] 区间修改 求区间积的欧拉函数 CF1114F

这是一个关于数组操作的问题,需要处理区间乘法和求区间内数的欧拉函数值模10^9+7。题目给出一个包含n个元素的数组,进行q次操作,包括区间乘法和求区间欧拉函数值。在处理过程中,需要理解并应用Euler's totient function的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

F. Please, another Queries on Array?

time limit per test

5.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array a1,a2,…,ana1,a2,…,an.

You need to perform qq queries of the following two types:

  1. "MULTIPLY l r x" — for every ii (l≤i≤rl≤i≤r) multiply aiai by xx.
  2. "TOTIENT l r" — print φ(∏i=lrai)φ(∏i=lrai) taken modulo 109+7109+7, where φφ denotes Euler's totient function.

The Euler's totient function of a positive integer nn (denoted as φ(n)φ(n)) is the number of integers xx (1≤x≤n1≤x≤n) such that gcd(n,x)=1gcd(n,x)=1.

Input

The first line contains two integers nn and qq (1≤n≤4⋅1051≤n≤4⋅105, 1≤q≤2⋅1051≤q≤2⋅105) — the number of elements in array aa and the number of queries.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤3001≤ai≤300) — the elements of array aa.

Then qq lines follow, describing queries in the format given in the statement.

  1. "MULTIPLY l r x" (1≤l≤r≤n1≤l≤r≤n, 1≤x≤3001≤x≤300) — denotes a multiplication query.
  2. "TOTIENT l r" (1≤l≤r≤n1≤l≤r≤n) — denotes a query on the value of Euler's totient function.

It is guaranteed that there is at least one "TOTIENT" query.

Output

For each "TOTIENT" query, print the answer to it.

Example

input

Copy

4 4
5 9 1 2
TOTIENT 3 3
TOTIENT 3 4
MULTIPLY 4 4 3
TOTIENT 4 4

output

Copy

1
1
2

Note

In the first example, φ(1)=1φ(1)=1 for the first query, φ(2)=1φ(2)=1 for the second query and φ(6)=2φ(6)=2 for the third one.

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int mn = 4e5 + 10;
const int mod = 1e9 + 7;

int pcnt, pm[100];
void shai()
{
	bool is[310];
    for (int i = 2; i <= 300; i++)
		is[i] = 1;
    for (int i = 2; i <= 300; i++)
	{
		if (!is[i])
			continue;
        pm[pcnt] = i;	// 300内从小到大质数表
        pcnt++;
        for (int j = 2; i * j <= 300; j++)
			is[i * j] = 0;
	}
}
ll yin[310], inv[310];
ll calzhi(int x)
{
	ll ans = 0;
    for (int i = 0; pm[i] <= x && i < pcnt; i++)
	{
		if (x % pm[i] == 0)	// x因子包含该质数
			ans += (1ll << i);
	}
	return ans;
}
ll ksm(ll x, ll y)
{
	ll ans = 1;
	while(y)
	{
		if (y & 1)
			ans = ans * x % mod;
		x = x * x % mod;
		y >>= 1;
	}
	return ans;
}

struct node
{
	int l, r;
	ll ji, zhi, lazy, lazy2;
} tr[8 * mn];

void pushup(int id)
{
	tr[id].ji = tr[2 * id].ji * tr[2 * id + 1].ji % mod;
    tr[id].zhi = tr[2 * id].zhi | tr[2 * id + 1].zhi;
}
void pushdown(int id)
{
    if (tr[id].lazy == 1 || tr[id].lazy2 == 0)
		return;
	
    tr[2 * id].lazy = tr[2 * id].lazy * tr[id].lazy % mod;
    tr[2 * id + 1].lazy = tr[2 * id + 1].lazy * tr[id].lazy % mod;
    tr[2 * id].ji = tr[2 * id].ji * ksm(tr[id].lazy, tr[2 * id].r - tr[2 * id].l + 1) % mod;
    tr[2 * id + 1].ji = tr[2 * id + 1].ji * ksm(tr[id].lazy, tr[2 * id + 1].r - tr[2 * id + 1].l + 1) % mod;
    tr[id].lazy = 1;
    
    tr[2 * id].lazy2 |= tr[id].lazy2;
    tr[2 * id + 1].lazy2 |= tr[id].lazy2;
    tr[2 * id].zhi |= tr[2 * id].lazy2;
    tr[2 * id + 1].zhi |= tr[2 * id + 1].lazy2;
	tr[id].lazy2 = 0;
}

void build(int l, int r, int id)
{
    tr[id].l = l, tr[id].r = r, tr[id].lazy = 1, tr[id].lazy2 = 0;
    if (l == r)
	{
        int t;
        scanf("%d", &t);
        tr[id].ji = t;
        tr[id].zhi = yin[t];
        return;
	}
    int mid = (l + r) >> 1;
    build(l, mid, 2 * id);
    build(mid + 1, r, 2 * id + 1);
    pushup(id);
}
void change(int id, int L, int R, int x, ll xzhi)
{
    int l = tr[id].l, r = tr[id].r;
    if (L <= l && r <= R)
	{
        tr[id].ji = tr[id].ji * ksm(x, tr[id].r - tr[id].l + 1) % mod;
        tr[id].zhi = tr[id].zhi | xzhi;
        tr[id].lazy = tr[id].lazy * x % mod;
        tr[id].lazy2 |= xzhi;
		return;
	}
	pushdown(id);
    int mid = (l + r) >> 1;
    if (R <= mid)
		change(2 * id, L, R, x, xzhi);
	else if (mid +1 <= L)
		change(2 * id + 1, L, R, x, xzhi);
    else 
    {
    	change(2 * id, L, mid, x, xzhi);
    	change(2 * id + 1, mid + 1, R, x, xzhi);
    }
	
	pushup(id);
}
ll anszhi, ansji;
void query(int id, int L, int R)
{
	int l = tr[id].l, r = tr[id].r;
	if (L <= l && r <= R)
	{
		ansji = ansji * tr[id].ji % mod;
        anszhi |= tr[id].zhi;
		return;
	}
	pushdown(id);
    int mid = (l + r) >> 1;
    if (R <= mid)
		query(2 * id, L, R);
	else if (mid + 1 <= L)
		query(2 * id + 1, L, R);
	else 
	{
		query(2 * id, L, mid);
		query(2 * id + 1, mid + 1, R);
	}
}

int main()
{
	shai();
	for (int i = 1; i <= 300; i++)
	{
		yin[i] = calzhi(i);	// i 包含的质数 
        // 300以内素数62个,是否含有 = 0/1, 压缩进一个long long
		inv[i] = ksm(i, mod - 2);	// 求 i 的逆元
	}

	int n, q;
	scanf("%d %d", &n, &q);
	build(1, n, 1);

	char ch[50];
	int l, r, x;
	while (q--)
	{
		scanf("%s", ch);
		if (ch[0] == 'M')
		{
			scanf("%d %d %d", &l, &r, &x);
			change(1, l, r, x, yin[x]);
			// 区间 * x; 区间包含的质数 | x包含的质数
		}
		else if (ch[0] == 'T')
		{
			anszhi = 0, ansji = 1;
			scanf("%d %d", &l, &r);
            query(1, l, r);
            
            ll ans = ansji;
            for (int i = 0; i < pcnt; i++)
			{
				if ((anszhi >> i) & 1)
				ans = ans * (pm[i] - 1) % mod * inv[pm[i]] % mod;
				/// n的欧拉函数值 = n * (1 - 1 / p1) * (1 - 1 / p2) * ... * (1 - 1 / pn) 
				/// 1 - 1 / p1  ==  (p1 - 1) / p1  ==  (p1 - 1) * inv[p1]
			}
			printf("%lld\n", ans);
		}
	}
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值