zoukankan      html  css  js  c++  java
  • Master of GCD(线段树or差分)

    题目描述

    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.

    样例输入

    复制样例数据

    2
    5 3
    1 3 2
    3 5 2
    1 5 3
    6 3
    1 2 2
    5 6 2
    1 6 2
    

    样例输出

    6
    2
    

    提示

    For the first test case, after all operations, the numbers will be [6,6,12,6,6]. So the greatest common divisor is 6.

    差分

    #include <iostream>
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int mod = 998244353;
    const int N = 100005;
    int a[N],b[N];
    ll qpow(ll a,ll b){
        ll ans=1;
        while(b){
            if(b&1){
                ans=ans*a%mod;
            }
            a=a*a%mod;
            b/=2;
        }
        return ans;
    }
    int main()
    {
        int t;
        cin>>t;
        while(t--){
            memset(a,0,sizeof a);
            memset(b,0,sizeof b);
            int n,m;
            cin>>n>>m;
            while(m--){
                int x,y,z;
                scanf("%d%d%d",&x,&y,&z);
                if(z==2){
                    a[x]++;
                    a[y+1]--;
                }
                else {
                    b[x]++;
                    b[y+1]--;
                }
            }
            int s1=a[1],s2=b[1];
            for(int i=2;i<=n;i++){
                a[i]+=a[i-1];
                b[i]+=b[i-1];
                s1=min(s1,a[i]);
                s2=min(s2,b[i]);
            }
            ll ans=qpow(2,s1)%mod*qpow(3,s2)%mod;
            cout<<ans<<endl;
        }
        return 0;
    }

     线段树

    #include <iostream>
    #include <bits/stdc++.h>
    using namespace std;
    #define mod 998244353
    typedef long long ll;
    #define N 100005
    struct xx{
        int a,b;
    }sum[N*4];
    struct yy{
        int a,b;
    }lazy[N*4];
    ll gcd (ll a,ll b){
        if(b==0) return a;
        return gcd(b,a%b);
    }
    void build (int l,int r,int rt){
        if(l==r) {
            sum[rt].a=0;
            lazy[rt].a=0;
            sum[rt].b=0;
            lazy[rt].b=0;
            return;
        }
        int mid =(l+r)/2;
        build(l,mid,rt*2);
        build(mid+1,r,rt*2+1);
        sum[rt].a=sum[rt*2].a+sum[rt*2+1].a;
        sum[rt].b=sum[rt*2].b+sum[rt*2+1].b;
    }
    void updown1(int l1,int r1,int l2,int r2,int rt){
        if(lazy[rt].a!=0){
            sum[rt*2].a+=lazy[rt].a*(r1-l1+1);
            sum[rt*2+1].a+=lazy[rt].a*(r2-l2+1);
            lazy[rt*2].a+=lazy[rt].a;
            lazy[rt*2+1].a+=lazy[rt].a;
            lazy[rt].a=0;
        }
    }
    void updown2(int l1,int r1,int l2,int r2,int rt){
        if(lazy[rt].b!=0){
            sum[rt*2].b+=lazy[rt].b*(r1-l1+1);
            sum[rt*2+1].b+=lazy[rt].b*(r2-l2+1);
            lazy[rt*2].b+=lazy[rt].b;
            lazy[rt*2+1].b+=lazy[rt].b;
            lazy[rt].b=0;
        }
    }
    void update1(int l1,int r1,int x,int l,int r,int rt){
        if(l1<=l&&r1>=r){
            sum[rt].a*=(r1-l1+1);
            lazy[rt].a++;
            return ;
        }
        int mid=(l+r)/2;
        if(l1<=mid) update1(l1,r1,x,l,mid,rt*2);
        if(r1>mid) update1(l1,r1,x,mid+1,r,rt*2+1);
        sum[rt].a=sum[rt*2].a+sum[rt*2+1].a;
    }
    void update2(int l1,int r1,int x,int l,int r,int rt){
        if(l1<=l&&r1>=r){
            sum[rt].b*=(r1-l1+1);
            lazy[rt].b++;
            return ;
        }
        int mid=(l+r)/2;
        if(l1<=mid) update2(l1,r1,x,l,mid,rt*2);
        if(r1>mid) update2(l1,r1,x,mid+1,r,rt*2+1);
        sum[rt].b=sum[rt*2].b+sum[rt*2+1].b;
    }
    ll qurry1(int p,int l,int r,int rt){
        if(p==l&&p==r){
            return sum[rt].a;
        }
        int mid=(l+r)/2;
        updown1(l,mid,mid+1,r,rt);
        ll ans=0;
        if(p<=mid) ans+=qurry1(p,l,mid,rt*2);
        else if(p>mid) ans+=qurry1(p,mid+1,r,rt*2+1);
        return ans;
    }
    ll qurry2(int p,int l,int r,int rt){
        if(p==l&&p==r){
            return sum[rt].b;
        }
        int mid=(l+r)/2;
        updown2(l,mid,mid+1,r,rt);
        ll ans=0;
        if(p<=mid) ans+=qurry2(p,l,mid,rt*2);
        else if(p>mid) ans+=qurry2(p,mid+1,r,rt*2+1);
        return ans;
    }
    int main()
    {
     
        int t;
        cin>>t;
        while(t--){
            int n,m;
            scanf("%d%d",&n,&m);
            for(int i=1;i<=n;i++){
                sum[i].a=0;
                sum[i].b=0;
                lazy[i].a=0;
                lazy[i].b=0;
            }
            build(1,n,1);
            while(m--){
                int x,y,z;
                scanf("%d%d%d",&x,&y,&z);
                if(z==2)
                update1(x,y,z,1,n,1);
                else
                update2(x,y,z,1,n,1);
            }
            ll ans1=0x3f3f3f3f,ans2=0x3f3f3f3f;
            for(int i=1;i<=n;i++){
                ll t1=qurry1(i,1,n,1);
                ans1=min(t1,ans1);
                ll t2=qurry2(i,1,n,1);
                ans2=min(ans2,t2);
               // cout<<t1<<" "<<t2<<endl;
            }
            ll ans=1;
            while(ans1--){
                ans*=2;
                ans%=mod;
            }
            while(ans2--){
                ans*=3;
                ans%=mod;
            }
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    c语言简易文法
    词法分析实验报告
    词法分析程序
    scrapy-splash的安装和使用
    scrapy关于将数据保存进mysql数据库及问题解决(增删查改)
    关于scrapy的一些练习
    安装python框架scrapy
    ssm中遇到的一些问题及解决办法
    安装PIL和pytesseract,用Pycharm自动化测试,验证码登陆
    selenium+pycham自动化登陆qq邮箱发送邮件
  • 原文地址:https://www.cnblogs.com/skyleafcoder/p/12319511.html
Copyright © 2011-2022 走看看