HDU - 3071 Gcd & Lcm game (线段树 + 素数拆分 + 状压)

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… 

Input

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. 

Output

For each of the first or second operation, you need to output the answer in each line.

Sample Input

6 4
1 2 4 5 6 3
L 2 5 17
G 4 6 4
C 4 9
G 4 6 4

Sample Output

9
1
3

题意:一开始给定给一个数组。有三种操作:1. 查询 k1, k2 的LCM 模 p。 2. 查询k1, k2之间的 GCD 模 p。3. 将第 k 个位置的值改成 v 。对于每种查询操作,输出答案。

思路:

蒟蒻我的看着这个题,难。。瞅大佬博客好久才理解,然后默默的边写边看题解代码QAQ

仔细想一想,会发现是不能直接维护LCM和GCD的,就算是100以内。 如 100以内的所有素数的LCM,已经爆了long long.

这里采用质因子状态压缩来求GCD和LCM。

100 以内的 素数只有25个,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.

故可以求得 一个数最多有 6个因子为 2 、4个因子为3、2个因子为5、2个因子为7剩下的都最多1个因子。所以我们压缩的时候因子2需要 3位 (2^3 = 8 > 6),因子 3 需要 3位,因子 5 需要 2位,因子 7 需要 2位,其他的都只需要一位

最后刚好31位二进制。

 

然后对于求GCD的问题,就是状压后每一对应的因子取最小次数,然后所有的乘起来。

LCM就是状压后每一对应的因子取最大次数,然后所有的乘起来。


对于 GCD函数和LCM函数中的数1879048192等,就是用于求对应因子出现的次数的,转换为2进制看可能会更明显。

剩下的就是压缩和还原的问题,具体见函数,然后手动模拟一下,就大功告成了

 

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define _max(x,y) ((x) > (y) ? (x) : (y))
#define _min(x,y) ((x) > (y) ? (y) : (x))
#define LL long long

using namespace std;

const int maxn = 1e5 + 10;
int lcm[maxn << 2],gcd[maxn << 2];
int n,m;
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 Move[] = {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};
int a[4][7] = {{1,2,4,8,16,32,64},{1,3,9,27,81},{1,5,25},{1,7,49}};

int yasuo(int x){
    int y = 0;
    for(int i = 0;i < 25 && x > 1;++ i){
        int tmp = 0;
        while(x % prime[i] == 0){
            ++ tmp;
            x /= prime[i];
        }
        y |= tmp << Move[i];
    }
    return y;
}

int rollback(int x,int p){
    int tmp = x;
    LL ans = 1;
    for(int i = 0;i < 25;++ i){
        tmp = tmp >> Move[i];
        ans = ans * (i < 4 ? a[i][tmp] : (tmp ? prime[i] :1)) % p;
        tmp = tmp << Move[i];
        tmp ^= x;
        x = tmp;
    }
    return (int)ans;
}

int GCD(int x,int y){
    return _min(x & 1879048192,y & 1879048192) | _min(x & 234881024,y & 234881024)
     | _min(x & 25165824,y & 25165824) | _min(x & 6291456,y & 6291456) | ((x & 2097151) & (y & 2097151));
}

int LCM(int x,int y){
    return _max(x & 1879048192,y & 1879048192) | _max(x & 234881024,y & 234881024)
     | _max(x & 25165824,y & 25165824) | _max(x & 6291456,y & 6291456) | ((x & 2097151) | (y & 2097151));
}

void build(int l,int r,int i){
    if(l == r){
        int x; scanf("%d",&x);
        gcd[i] = lcm[i] = yasuo(x);
        return ;
    }
    int mid = (l + r) >> 1;
    build(l,mid,i << 1);
    build(mid + 1,r,i << 1 | 1);
    gcd[i] = GCD(gcd[i << 1],gcd[i << 1 | 1]);
    lcm[i] = LCM(lcm[i << 1],lcm[i << 1 | 1]);
}

void update(int l,int r,int pos,int val,int i){
    if(l == r){
        gcd[i] = lcm[i] = yasuo(val);
        return ;
    }
    int mid = (l + r) >> 1;
    if(pos <= mid) update(l,mid,pos,val,i << 1);
    if(pos > mid) update(mid + 1,r,pos,val,i << 1 | 1);
    gcd[i] = GCD(gcd[i << 1],gcd[i << 1 | 1]);
    lcm[i] = LCM(lcm[i << 1],lcm[i << 1 | 1]);
}

int query(int l,int r,int ql,int qr,int i,int flag){  ///flag == 1 查询lcm
    if(ql <= l && r <= qr){
        return flag ? lcm[i] : gcd[i];
    }
    int mid = (l + r) >> 1;
    int ans = flag ? 0 : 0x7fffffff;
    if(ql <= mid) flag ? ans = LCM(ans,query(l,mid,ql,qr,i << 1,flag)) :
                         ans = GCD(ans,query(l,mid,ql,qr,i << 1,flag));
    if(qr > mid)  flag ? ans = LCM(ans,query(mid + 1,r,ql,qr,i << 1 | 1,flag)) :
                         ans = GCD(ans,query(mid + 1,r,ql,qr,i << 1 | 1,flag));
    return ans;
}

int main() {
    while(~scanf("%d%d",&n,&m)){
        build(1,n,1);
        while(m --){
            char ch; scanf(" %c",&ch);
            int a,b; scanf("%d%d",&a,&b);
            if(ch == 'L' || ch == 'G'){
                int c; scanf("%d",&c);
//                debug(query(1,n,a,b,1,ch == 'L'));
                printf("%d\n",rollback(query(1,n,a,b,1,ch == 'L'),c));
            }
            else {
                update(1,n,a,b,1);
            }
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值