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

题目大意:定义一个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

对每一次询问做出输出。

1、首先写一个找规律的,发现NicoNico-number是有三种组成的第一种是素数,另外一种是2的x次幂,第三种是6

2、那么能够建一个数组。直接标记某个数是不是NicoNico-number

3、使用线段树维护一段区间的NicoNico-number个数,然后能够进行对某一个数的改动,和对一个区间的查询。

对于另外一种操作。我们要知道对于一个数x取余操作。最多会运行log(x)次。由于每次取余至少数值会降低一半。所以对于每一个数来说最多会有log(x)次操作,之后会由于v大于当前值,而不用运行操作。既然取余的次数不多,那么就能够对区域操作进行暴力,维护一段区间的最大值,假设最大值小于v,那么这一段不用更新,否则就遍历的最低层进行取余。

4、对于n个数来说,查找到一个数须要log(n),一个数最多会被改动log(x)次,所以总的时间不会超过n*log(n)*log(x)。

#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <cmath>
#include <map>
#include <stack>
#include <algorithm>
using namespace std ;
#define LL __int64
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define root 1,n,1
#define int_rt int l,int r,int rt
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
const int mod = 1e9+7 ;
const double eqs = 1e-9 ;
int cl[400000] , num[400000] ;
int a[10000005] , check[10000005] ;
int tot ;
void init() {
memset(check,-1,sizeof(check)) ;
tot = 0 ;
for(int i = 2 ; i <= 10000000 ; i++) {
if( check[i] == -1 ){
a[tot++] = i ;
check[i] = 1 ;
}
for(int j = 0 ; j < tot ; j++) {
if( i*a[j] >= 10000000 ) break ;
check[i*a[j]] = 0 ;
if( i%a[j] == 0 ) break ;
}
}
check[0] = check[1] = check[6] = 1 ;
for(int i = 2 ; i <= 10000000 ; i *= 2)
check[i] = 1 ;
}
void push_up(int rt) {
cl[rt] = max(cl[rt<<1],cl[rt<<1|1]) ;
num[rt] = num[rt<<1]+num[rt<<1|1] ;
}
void create(int_rt) {
cl[rt] = num[rt] = 0 ;
if( l == r ) {
scanf("%d", &cl[rt]) ;
if( check[ cl[rt] ] == 1 ) num[rt] = 1 ;
return ;
}
create(lson) ;
create(rson) ;
push_up(rt) ;
}
void update1(int ll,int rr,int v,int_rt) {
if( ll > r || rr < l ) return ;
if( cl[rt] < v ) return ;
if( l == r ) {
cl[rt] %= v ;
if( check[ cl[rt] ] == 1 ) num[rt] = 1 ;
else num[rt] = 0 ;
return ;
}
update1(ll,rr,v,lson) ;
update1(ll,rr,v,rson) ;
push_up(rt) ;
}
void update2(int k,int x,int_rt) {
if( l == r && l == k ) {
cl[rt] = x ;
if( check[ cl[rt] ] == 1 ) num[rt] = 1 ;
else num[rt] = 0 ;
return ;
}
int mid = (l+r)/2 ;
if(k <= mid) update2(k,x,lson) ;
else update2(k,x,rson) ;
push_up(rt) ;
}
int query(int ll,int rr,int_rt) {
if( ll > r || rr < l ) return 0 ;
if( ll <= l && rr >= r ) return num[rt] ;
return query(ll,rr,lson) + query(ll,rr,rson) ;
}
int main() {
int n , m , i , k , l , r , v , x ;
init() ;
while( scanf("%d", &n) !=EOF ) {
create(root) ;
scanf("%d", &m) ;
while( m-- ) {
scanf("%d", &k) ;
if( k == 1 ) {
scanf("%d %d", &l, &r) ;
printf("%d\n", query(l,r,root)) ;
}
else if( k == 2 ) {
scanf("%d %d %d", &l, &r, &v) ;
update1(l,r,v,root) ;
}
else {
scanf("%d %d", &i, &x) ;
update2(i,x,root) ;
}
}
}
return 0 ;
}

zoj3886--Nico Number(素数筛+线段树)的更多相关文章

  1. ZOJ 3886 Nico Number(筛素数+Love(线)Live(段)树)

    problemCode=3886">ZOJ 3886 题意: 定义一种NicoNico数x,x有下面特征: 全部不大于x且与x互质的数成等差数列,如x = 5 ,与5互素且不大于5的数 ...

  2. ACM Minimum Inversion Number 解题报告 -线段树

    C - Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  3. HDU_3071 Gcd & Lcm game 【素数分解 + 线段树 + 状压】

    一.题目  Gcd & Lcm game 二.分析 非常好的一题. 首先考虑比较暴力的做法,肯定要按区间进行处理,对于$lcm$和$gcd$可以用标准的公式进行求,但是求$lcm$的时候是肯定 ...

  4. FZU 2297 Number theory【线段树/单点更新/思维】

    Given a integers x = 1, you have to apply Q (Q ≤ 100000) operations: Multiply, Divide. Input First l ...

  5. Hdu P1394 Minimum Inversion Number | 权值线段树

    题目链接 题目翻译: 约定数字序列a1,a2,...,an的反转数是满足i<j和ai>aj的数对(ai,aj)的数量. 对于给定的数字序列a1,a2,...,an,如果我们将第1到m个数字 ...

  6. [POJ2104] K – th Number (可持久化线段树 主席树)

    题目背景 这是个非常经典的主席树入门题--静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输 ...

  7. ZOJ 3911Prime Query [素数处理 + 线段树]

    Time Limit: 5 Seconds Memory Limit: 196608 KBYou are given a simple task. Given a sequence A[i] with ...

  8. 埃氏筛+线段树——cf731F

    从2e5-1依次枚举每个数作为主显卡,然后分段求比它大的数的个数,这里的复杂度是调和级数ln2e5,即埃氏筛的复杂度.. #include<bits/stdc++.h> using nam ...

  9. (中等) POJ 2886 Who Gets the Most Candies? , 反素数+线段树。

    Description N children are sitting in a circle to play a game. The children are numbered from 1 to N ...

随机推荐

  1. 长脖子鹿省选模拟赛 [LnOI2019SP]快速多项式变换(FPT)

    本片题解设计两种解法 果然是签到题... 因为返回值问题T了好久... 第一眼:搜索大水题? 然后...竟然A了 #include<cstdio> #include<queue> ...

  2. css 继承性和层叠性

    css有两大特性:继承性和层叠性 继承性 面向对象语言都会存在继承的概念,在面向对象语言中,继承的特点:继承了父类的属性和方法.那么我们现在主要研究css,css就是在设置属性的.不会牵扯到方法的层面 ...

  3. java.util.Arrays

    package com.etc.Arrays; import java.util.Arrays; public class TestArraysClass { public static void m ...

  4. python--6、logging模块

    logging 可用的日志级别: debug 10 info 20 warning 30 error 40 critical 50 logging默认参数: 默认日志级别是warning. 默认情况日 ...

  5. 身份认证防止重放攻击的challenge-response方法

    或者叫询问-应答机制. 基于挑战/应答(Challenge/Response)方式的身份认证系统就是每次认证时认证服务器端都给客户端发送一个不同的"挑战"字串,客户端程序收到这个& ...

  6. Apex语言(三)原始数据类型

    1.原始数据类型(Primitive) 整数:Integer 双精度:Double 单精度:Decimal 长整型:Long 日期:Date 日期时间:Datetime 字符串:String ID:I ...

  7. Java 将要上传的文件上传至指定路径代码实现

    代码: /** * 上传文件到指定路径 * @param mFile 要上传的文件 * @param path 指定路径 */ public static void uploadFile(Multip ...

  8. 【剑指Offer】18、二叉树的镜像

      题目描述:   操作给定的二叉树,将其变换为原二叉树的镜像.   解题思路:   求一棵树的镜像的过程:先前序遍历这棵树的每个结点,如果遍历到的结点有子结点,就交换它的两个子结点.当交换完所有的非 ...

  9. 【JavaScript游戏开发】使用HTML5 canvas开发的网页版中国象棋项目

    //V1.0 : 实现棋子的布局,画布及游戏场景的初始化 //V2.0 : 实现棋子的颜色改变 //V3.0 :实现所有象棋的走棋规则 //V4.0 : 实现所有棋子的吃子功能 完整的项目源码已经开源 ...

  10. JS常见的四种设计模式

    1 工厂模式 简单的工厂模式可以理解为解决多个相似的问题; function CreatePerson(name,age,sex) { var obj = new Object(); obj.name ...