ZOJ 3886 Nico Number

本文介绍了一个关于寻找特殊整数——NicoNico数的游戏模拟算法。该算法使用线段树进行区间修改和查询操作,以高效解决游戏中的各种操作需求。

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

Description

Kousaka Honoka and Minami Kotori are playing a game about a secret of Yazawa Nico.

When the game starts, Kousaka Honoka will give Minami Kotori an array A of N non-negative integers. There is a special kind of number in the array, which is called NicoNico-number. We call a integer x is a NicoNico-number, if all integers(no more than x) that is coprime with x could form an Arithmetic Sequence.

Then Minami Kotori will choose some consecutive part of the array A, wondering the number of NicoNico-number in this part. What's more, Kousaka Honoka sometimes modify the value of some consecutive elements in the array. Now it is your task to simulate this game!

Input

There are multiple test cases in input, you should process to the end of file.

For each case, the first line is an integer N, the number of elements in the array described above. Then second line contains N integers no greater than 107, the elements of the array initially.(1 <= N <= 100,000)

The third line is a integer T, the number of the operations of the game. Each line of the following T lines is in one of the following formats.(1 <= T <= 100,000)

"1 L R" : Minami Kotori will chooses the consecutive part of the array from the Lth to Rth element inclusive. (1 <= L <= R <= N)

"2 L R v" : Kousaka Honoka will change the value of the pth element A[p] in the array to A[p]%v for all L <= p <= R.(1 <= L <= R <= N, 1 <= v <= 107)

"3 p x" : Kousaka Honoka will change the value of the p th element A[p] to x.(1 <= p <= N, 1 <= x <= 107)

Output

Each time when Minami Kotori chooses some part of the array, you should output a line, the number of NicoNico-number in that part.

Sample Input

3
4 6 9
6
1 1 3
1 3 3
2 1 1 10
1 1 3
3 2 4
1 1 3

Sample Output

2
0
2
2
Hint

4 is a NicoNico-number because only 1 and 3 is coprime with 4 among the integers no greater than 4, and could form an Arithmetic Sequence {1,3}.


先确定有哪些niconico数,然后对于mod运算每次至少/2

线段树暴力更新即可。

#include<cstdio>
#include<vector>
#include<cmath>
#include<map>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL maxn = 400005;
int T, n, m, x, y, z, ans;
int f[maxn], sum[maxn], M[(int)1e7 + 7], p[(int)1e7 + 7];

inline void read(int &ret)
{
	char c;
	do {
		c = getchar();
	} while (c < '0' || c > '9');
	ret = c - '0';
	while ((c = getchar()) >= '0' && c <= '9')
		ret = ret * 10 + (c - '0');
}

void pre()
{
	int tot = 0;
	for (int i = 2; i <= 1e7; ++i)
	{
		if (!M[i]) p[tot++] = i;
		for (int j = 0; j < tot; ++j)
		{
			if (i*p[j]>1e7) break;
			M[i*p[j]] = 1;
			if (i%p[j] == 0) break;
		}
	}
	M[6] = 0;
	for (int i = 1; i <= 1e7; i <<= 1) M[i] = 0;
}

void build(int now, int l, int r)
{
	if (l == r)
	{
		scanf("%d", &f[now]);
		sum[now] = M[f[now]];
	}
	else
	{
		int mid = (l + r) >> 1;
		build(now + now, l, mid);
		build(now + now + 1, mid + 1, r);
		f[now] = max(f[now + now], f[now + now + 1]);
		sum[now] = sum[now + now] + sum[now + now + 1];
	}
}

void change(int now, int l, int r, int ll, int rr, int v)
{
	if (v > f[now]) return;
	if (l == r) { f[now] = f[now] % v; sum[now] = M[f[now]]; }
	else
	{
		int mid = (l + r) >> 1;
		if (ll <= mid) change(now + now, l, mid, ll, rr, v);
		if (rr > mid) change(now + now + 1, mid + 1, r, ll, rr, v);
		f[now] = max(f[now + now], f[now + now + 1]);
		sum[now] = sum[now + now] + sum[now + now + 1];
	}
}

void change(int now, int l, int r, int one, int v)
{
	if (l == r) { f[now] = v; sum[now] = M[f[now]]; }
	else
	{
		int mid = (l + r) >> 1;
		if (one <= mid) change(now + now, l, mid, one, v);
		if (one > mid) change(now + now + 1, mid + 1, r, one, v);
		f[now] = max(f[now + now], f[now + now + 1]);
		sum[now] = sum[now + now] + sum[now + now + 1];
	}
}

void get(int now, int l, int r, int ll, int rr)
{
	if (l >= ll&&r <= rr) ans += sum[now];
	else
	{
		int mid = (l + r) >> 1; 
		if (ll <= mid) get(now + now, l, mid, ll, rr);
		if (rr > mid) get(now + now + 1, mid + 1, r, ll, rr);
	}
}

int main()
{
	//read(T);
	pre();
	while (scanf("%d", &n) == 1)
	{
		build(1, 1, n);
		scanf("%d", &m);
		while (m--)
		{
			scanf("%d", &x);
			if (x == 2)
			{
				scanf("%d%d%d", &x, &y, &z);
				change(1, 1, n, x, y, z);
			}
			else if (x == 3) 
			{ 
				scanf("%d%d", &y, &z); 
				change(1, 1, n, y, z); 
			}
			else
			{
				ans = 0;
				scanf("%d%d", &y, &z);
				get(1, 1, n, y, z);
				printf("%d\n", z + 1 - y - ans);
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值