ZOJ3886-Nico Number

Nico Number

Time Limit: 2 Seconds       Memory Limit: 262144 KB

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}.


Author:  ZHANG, Ruixiang
Source:  ZOJ Monthly, July 2015


题意:定义一个NicoNico-number,如果x是NicoNico-number,那么所有小于x的且与x互质的整数是一个等差数列,初始给出n个数字的数组,三种操作:

1 l r 问在[l,r]内有多少个NicoNico-number数

2 l r v 对于[l,r]内的数全部对v取余

3 k x 将第k个数换为x

解题思路:可以发现NicoNico-number是有三种组成的第一种是素数,第二种是2的x次幂,第三种是6,那么可以用一个数组,直接标记某个数是不是NicoNico-number,用线段树维护一段区间的NicoNico-number个数


#include <iostream>  
#include <cstdio>  
#include <string>  
#include <cstring>  
#include <cmath> 
#include <algorithm>
#include <queue>  
#include <vector>  
#include <set>  
#include <stack>
#include <map>  
#include <climits>  

using namespace std;

#define LL long long  
const int INF = 0x3f3f3f3f;

bool vis[10000009];
int sum[100009 << 2], ma[100009 << 2], a[100009], n, q, p, x, y, z;

void init()
{
	memset(vis, true, sizeof vis);
	for (int i = 2; i < 10000009; i++)
	{
		if (!vis[i]) continue;
		for (int j = i * 2; j < 10000009; j += i) vis[j] = false;
	}
	vis[6] = true;
	for (int i = 1; i < 10000009; i <<= 1) vis[i] = true;
}

void build(int k, int l, int r)
{
	if (l == r) { sum[k] = vis[a[l]], ma[k] = a[l]; return; }
	int mid = (l + r) >> 1;
	build(k << 1, l, mid), build(k << 1 | 1, mid + 1, r);
	sum[k] = sum[k << 1] + sum[k << 1 | 1];
	ma[k] = max(ma[k << 1], ma[k << 1 | 1]);
}

int query(int k, int l, int r, int ll, int rr)
{
	if (l >= ll&&r <= rr) return sum[k];
	int ans = 0, mid = (l + r) >> 1;
	if (ll <= mid) ans += query(k << 1, l, mid, ll, rr);
	if (rr > mid) ans += query(k << 1 | 1, mid + 1, r, ll, rr);
	return ans;
}

void update(int k, int l, int r, int ll, int rr, int val)
{
	if (l >= ll&&r <= rr&&ma[k] < val) return;
	if (l == r) { ma[k] %= val, sum[k] = vis[ma[k]]; return; }
	int mid = (l + r) >> 1;
	if (ll <= mid) update(k << 1, l, mid, ll, rr, val);
	if (rr > mid) update(k << 1 | 1, mid + 1, r, ll, rr, val);
	sum[k] = sum[k << 1] + sum[k << 1 | 1];
	ma[k] = max(ma[k << 1], ma[k << 1 | 1]);
}

void update(int k, int l, int r, int p, int val)
{
	if (l == r) { sum[k] = vis[val], ma[k] = val; return; }
	int mid = (l + r) >> 1;
	if (p <= mid) update(k << 1, l, mid, p, val);
	else update(k << 1 | 1, mid + 1, r, p, val);
	sum[k] = sum[k << 1] + sum[k << 1 | 1];
	ma[k] = max(ma[k << 1], ma[k << 1 | 1]);
}

int main()
{
	init();
	while (~scanf("%d", &n))
	{
		for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
		build(1, 1, n);
		scanf("%d", &q);
		while (q--)
		{
			scanf("%d%d%d", &p, &x, &y);
			if (p == 1) printf("%d\n", query(1, 1, n, x, y));
			else if (p == 2)
			{
				if (p == 2) scanf("%d", &z);
				update(1, 1, n, x, y, z);
			}
			else update(1, 1, n, x, y);
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值