Gcd & Lcm game
Tired of playing too much computer games, alpc23 is planning to play a game on numbers. Because plus and subtraction is too easy for this gay, he wants to do some gcd and lcm operations in a number sequence. After playing it a few times, he has found it is also too boring. So he plan to do a more challenge job: he wants to change several numbers in this sequence and also work out the gcd or lcm of all the number in a subsequence of the whole sequence.
To be a friend of this gay, you have been invented by him to play this interesting game with him. Of course, you need to work out the answers faster than him to get a free lunch, He he…
There are multiple test cases.
For each test case.The first line is the length of sequence n, and the number of queries q. (1<=n, q<=100000) The second line has n numbers, they are the initial n numbers of the sequence a1,a2, …,an,
From the third line to the q+2 line are the description of the q operations. They are the one of the two forms:
L k1 k2 p; you need to work out the value after mod p of lcm of the subsequence from k1 to k2, inclusive. (1<=k1<=k2<=n)
G k1 k2 p; you need to work out the value after mod p of gcd of the subsequence from k1 to k2, inclusive. (1<=k1<=k2<=n)
C k v; the k-th number of the sequence has been changed to v.
You can assume that all the numbers before and after the replacement are positive and no larger than 100.
There are multiple test cases.
For each test case.The first line is the length of sequence n, and the number of queries q. (1<=n, q<=100000) The second line has n numbers, they are the initial n numbers of the sequence a1,a2, …,an,
From the third line to the q+2 line are the description of the q operations. They are the one of the two forms:
L k1 k2 p; you need to work out the value after mod p of lcm of the subsequence from k1 to k2, inclusive. (1<=k1<=k2<=n)
G k1 k2 p; you need to work out the value after mod p of gcd of the subsequence from k1 to k2, inclusive. (1<=k1<=k2<=n)
C k v; the k-th number of the sequence has been changed to v.
You can assume that all the numbers before and after the replacement are positive and no larger than 100.
6 4
1 2 4 5 6 3
L 2 5 17
G 4 6 4
C 4 9
G 4 6 4
9
1
3
一道还不错的题目,解法:线段树+位压缩
题意:
给定一个长度为n的序列m次操作,操作的种类一共有三种
- 查询
- L :查询一个区间的所有的数的最小公倍数
modp
- G :查询一个区间的所有的数的最大公约数
modp
- 修改
分析:
首先我们注意一下数据的范围,保证数据不超过100,那么很明显素因子特别少一共只有25个,我们可以用线段树维护一下对应素因子的最大值与最小值。更新的话就是单点更新。由于时间比较紧,我们需要把所有的数压到一个
int
中去。
对任意x<=100 其因子个数情况如下:
int prime[]={ 2, 3, 5, 7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
int dpos[]={28,25,23,21,20,19,18,17,16,15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
//max num 7 4 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
//bit 3 3 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
//tot bit 3+3+2+2+21*1=31
// 0000 0000 0000 0000 0000 0000 0000 0000
// | | | |
// 2 3 5 7
所以,可以用一个32位的int数字表示x对应的各因子数
#define _min(x,y) ((x)<(y)?(x):(y))
#define _max(x,y) ((x)>(y)?(x):(y))
inline int min(int x,int y){
return _min(x&0x70000000,y&0x70000000)|_min(x&0x0e000000,y&0x0e000000)|_min(x&0x01800000,y&0x01800000)|_min(x&0x00600000,y&0x00600000)|((x&0x001fffff)&(y&0x001fffff));
}
inline int max(int x,int y){
return _max(x&0x70000000,y&0x70000000)|_max(x&0x0e000000,y&0x0e000000)|_max(x&0x01800000,y&0x01800000)|_max(x&0x00600000,y&0x00600000)|((x&0x001fffff)|(y&0x001fffff));
}
自定义比较函数,即分别计算x与y的每个因子出现的最大与最小次数。
然后就是裸的单点更新,成段求最值的线段树了
003 | const int maxn=444444; |
004 | int prime[]={ 2, 3, 5, 7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}; |
005 | int dpos[]={28,25,23,21,20,19,18,17,16,15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; |
006 | int a[]={1,2,4,8,16,32,64}; |
007 | int b[]={1,3,9,27,81}; |
011 | #define lson l,mid,lrt |
012 | #define rson mid+1,r,rrt |
013 | #define mid ((l+r)>>1) |
017 | int MAX[maxn],MIN[maxn]; |
018 | inline int turn( int x){ |
020 | for ( int i=0;i<25&&x>1;i++){ |
021 | for (cnt=0;x%prime[i]==0;x/=prime[i]) cnt++; |
026 | inline int back( int x, int p) |
029 | int k=x>>dpos[0];y=y*a[k]%p;x^=k<<dpos[0]; |
030 | k=x>>dpos[1];y=y*b[k]%p;x^=k<<dpos[1]; |
031 | k=x>>dpos[2];y=y*c[k]%p;x^=k<<dpos[2]; |
032 | k=x>>dpos[3];y=y*d[k]%p;x^=k<<dpos[3]; |
033 | for ( int i=4;i<25;i++) |
034 | if (x&(1<<dpos[i])) y=y*prime[i]%p; |
037 | #define _min(x,y) ((x)<(y)?(x):(y)) |
038 | #define _max(x,y) ((x)>(y)?(x):(y)) |
039 | inline int min( int x, int y){ |
040 | return _min(x&0x70000000,y&0x70000000)|_min(x&0x0e000000,y&0x0e000000)|_min(x&0x01800000,y&0x01800000)|_min(x&0x00600000,y&0x00600000)|((x&0x001fffff)&(y&0x001fffff)); |
042 | inline int max( int x, int y){ |
043 | return _max(x&0x70000000,y&0x70000000)|_max(x&0x0e000000,y&0x0e000000)|_max(x&0x01800000,y&0x01800000)|_max(x&0x00600000,y&0x00600000)|((x&0x001fffff)|(y&0x001fffff)); |
047 | inline void pushup( int rt){ |
048 | MAX[rt]=max(MAX[lrt],MAX[rrt]); |
049 | MIN[rt]=min(MIN[lrt],MIN[rrt]); |
051 | void build( int l, int r, int rt){ |
053 | int x; scanf ( "%d" ,&x); |
054 | MIN[rt]=MAX[rt]=turn(x); |
057 | build(lson);build(rson); |
060 | void update( int k, int x, int l, int r, int rt){ |
062 | MAX[rt]=MIN[rt]=turn(x); |
065 | if (k<=mid) update(k,x,lson); |
066 | else update(k,x,rson); |
069 | int query_max( int s, int t, int l, int r, int rt){ |
070 | if (s<=l&&t>=r) return MAX[rt]; |
072 | if (s<=mid) ret=max(ret,query_max(s,t,lson)); |
073 | if (t>mid) ret=max(ret,query_max(s,t,rson)); |
076 | int query_min( int s, int t, int l, int r, int rt){ |
077 | if (s<=l&&t>=r) return MIN[rt]; |
079 | if (s<=mid) ret=min(ret,query_min(s,t,lson)); |
080 | if (t>mid) ret=min(ret,query_min(s,t,rson)); |
086 | while ( scanf ( "%d%d" ,&n,&q)!=EOF){ |
098 | scanf ( "%d%d%d" ,&k1,&k2,&p); |
099 | int x=query_max(k1,k2,1,n,1); |
100 | printf ( "%u\n" ,back(x,p)); |
104 | scanf ( "%d%d%d" ,&k1,&k2,&p); |
105 | int x=query_min(k1,k2,1,n,1); |
106 | printf ( "%u\n" ,back(x,p)); |
参考:http://blog.csdn.net/wxfwxf328/article/details/7479874