UPC 补题 5052 Master of GCD

题目描述

Hakase has n numbers in a line. At fi rst, they are all equal to 1. Besides, Hakase is interested in primes. She will choose a continuous subsequence [l, r] and a prime parameter x each time and for every l≤i≤r, she will change ai into ai*x. To simplify the problem, x will be 2 or 3. After m operations, Hakase wants to know what is the greatest common divisor of all the numbers.

输入

The first line contains an integer T (1≤T≤10) representing the number of test cases.
For each test case, the fi rst line contains two integers n (1≤n≤100000) and m (1≤m≤100000),where n refers to the length of the whole sequence and m means there are m operations.
The following m lines, each line contains three integers li (1≤li≤n), ri (1≤ri≤n), xi (xi∈{2,3} ),which are referred above.

输出

For each test case, print an integer in one line, representing the greatest common divisor of the sequence. Due to the answer might be very large, print the answer modulo 998244353.

题意:有n个数,这n个数开始的时候全是1,下标依次从1到n,然后有m次区间赋值,每一次赋值都是将区间内的每一个值赋值为3或者2,然后需要你求出整个区间的gcd

分析:

这题最重要的就是去维护区间最小的gcd,怎么去维护呢,最先想到的方法就是用线段树去维护

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <set>
#include <stack>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
const int MOD=998244353;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define Abs(x) ((x)>=0?(x):-(x))
#define lson id<<1
#define rson id<<1|1
ll epow(ll bb,ll p){
    ll ans=1;
    while(p){
        if(p&1)
            ans=ans*bb%MOD;
        p>>=1;
        bb=bb*bb%MOD;
    }
    return ans;
}
int n,m;
ll lazy1[N<<2],lazy2[N<<2];//lazy1代表是标记乘2的区间,lazy2代表标记乘3的区间
ll cnt1[N<<2],cnt2[N<<2];//cnt1代表区间乘2的最小个数,cnt2代表区间乘3的最小的个数
void push_down(int id){
    if(lazy1[id]){
        lazy1[lson]+=lazy1[id];
        lazy1[rson]+=lazy1[id];
        cnt1[rson]+=lazy1[id];//这个地方需要注意,加的是lazy1
        cnt1[lson]+=lazy1[id];
        lazy1[id]=0;
    }
    if(lazy2[id]){
        lazy2[rson]+=lazy2[id];
        lazy2[lson]+=lazy2[id];
        cnt2[lson]+=lazy2[id];
        cnt2[rson]+=lazy2[id];
        lazy2[id]=0;
    }
}
void update(int id,int L,int R,int l,int r,ll val){
        if(L==l&&R==r){
            if(val==2){
                lazy1[id]++;
                cnt1[id]++;
            }
            else{
                lazy2[id]++;
                cnt2[id]++;
            }
            return ;
        }
        int mid=(L+R)>>1;
        push_down(id);
        if(r<=mid) update(lson,L,mid,l,r,val);
        else if(l>mid) update(rson,mid+1,R,l,r,val);
        else{
            update(lson,L,mid,l,mid,val);
            update(rson,mid+1,R,mid+1,r,val);
      }

    cnt1[id]=min(cnt1[lson],cnt1[rson]);
    cnt2[id]=min(cnt2[lson],cnt2[rson]);
}
int main()
{
//    #ifndef ONLINE_JUDGE
//    freopen("in.txt","r",stdin);
//    #endif // ONLINE_JUDGE
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        rep(i,0,N*4-10) lazy1[i]=lazy2[i]=0,cnt1[i]=cnt2[i]=0;
        for(int i=1;i<=m;i++){int u,v;ll w;
            scanf("%d%d%lld",&u,&v,&w);
            update(1,1,n,u,v,w);
        }
        printf("%lld\n",(epow(2,cnt1[1])%MOD*epow(3,cnt2[1])%MOD)%MOD);
    }
    return 0;
 }

当然,这题还是有别的办法去实现的,那就是去实现差分数组

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <set>
#include <stack>
using namespace std;
typedef long long ll;
const int N = 1e6+10;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
const int MOD=998244353;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define Abs(x) ((x)>=0?(x):-(x))
int n,m;
ll epow(ll b,ll p){
    ll ans=1;
    while(p){
        if(p&1)
            ans=ans*b%MOD;
        p>>=1;
        b=b*b%MOD;
    }
    return ans;
}
ll a[N];
ll d2[N],d3[N];
void update(int l,int r,int x){
     if(x==2)   {
        d2[l]++;
        d2[r+1]--;
     }else{
        d3[l]++;
        d3[r+1]--;
     }
}
void init(){
    rep(i,1,n) a[i]=1;
//    d3[1]=d2[1]=a[1];
//    rep(i,2,n) d2[i]=d3[i]=a[i]-a[i-1];
memset(d2,0,sizeof d2);
memset(d3,0,sizeof d3);
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        init();
        while(m--){int l,r, u;
            scanf("%d%d%d",&l,&r,&u);
            update(l,r,u);
        }
        ll min2=d2[1],min3=d3[1];
        ll pre2=d2[1],pre3=d3[1];
        rep(i,2,n){
            min2=min(min2,d2[i]+pre2),pre2+=d2[i];
            min3=min(min3,d3[i]+pre3),pre3+=d3[i];
        }
        printf("%lld\n",(epow(2,min2)%MOD*epow(3,min3)%MOD)%MOD);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/c___c18/article/details/89154524
UPC
gcd