HDU 6273(树状数组+思维)

题面:

Problem J. Master of GCD

Hakase has n numbers ina line. At first, 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 forevery 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 divisorof all the numbers.

Input

The first line contains an integer T (1 ≤ T ≤10) representing the number of test cases.

For each test case, the first line contains two integers n (1 ≤ n ≤100000) and m (1≤ m ≤ 100000),where n refers to the length of the wholesequence and m means there are m operations.

The following m lines, each line contains three integers li (1 ≤ lin), ri (1 ≤ ri n), xi (xi ∈{2,3}), whichare referred above.

Output

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

Example

standard input

 

standard output

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

 

Explanation

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

    题目描述:给你一个大小为n的数列,最开始数列的数全都为1。有m次更新,每次输入l,r,c。代表将在数列区间l到r的数成上c。最后求出整个数列gcd。
    题目分析:乍一眼看过去,貌似就是一个裸的区间更新线段树维护区间的gcd。但是,很显然,直接维护gcd的操作太过于困难,因此我们退而求其次,因为每次进行的操作都是对2或3进行乘积,因此,我们可以在每次更新的过程中记录区间中2和3的个数,最后用快速幂乘起来算gcd。

    因为只需要记录个数,因此,我们可以使用树状数组去维护区间的值。
    统计好个数之后,还得注意一个优化:因为gcd的值事实上与2和3出现次数最多的数没有任何影响,只有出现次数最少的数才会对gcd的值有影响。因此我们只需要分别记录下2和3中出现次数最少的数,用快速幂算出他们所对应的值并相乘即是答案。

    具体可以看一下代码

#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
typedef long long ll;
const int mod=998244353;
const int INF=0x3f3f3f3f;
ll sum1[maxn];
ll sum2[maxn];
int n;
ll lowbit(int x){
    return x&(-x);
}
ll add(int x,ll *C,int k){
    for(int i=x;i<maxn;i+=lowbit(i)){
        C[i]+=k;
    }
}
ll Sum(int x,ll *C){
    ll ans=0;
    for(int i=x;i>0;i-=lowbit(i)) ans+=C[i];
    return ans;
}
ll powmod(ll a,ll n){
    ll ans=1;
    while(n){
        if(n&1) ans=(ans*a)%mod;
        a=(a*a)%mod;
        n>>=1;
    }
    return ans;
}
int main()
{
    ll t;
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>t;
    while(t--){
        ll n,m;
        cin>>n>>m;
        memset(sum1,0,sizeof(sum1));
        memset(sum2,0,sizeof(sum2));
        for(int i=0;i<m;i++){
            ll l,r,c;
            cin>>l>>r>>c;
            if(c==2){
                add(l,sum1,1);
                add(r+1,sum1,-1);
            }
            else{
                add(l,sum2,1);
                add(r+1,sum2,-1);
            }
        }
        ll minn1=Sum(1,sum1);
        ll minn2=Sum(1,sum2);
        for(int i=2;i<=n;i++){
            minn1=min(minn1,Sum(i,sum1));
            minn2=min(minn2,Sum(i,sum2));
        }
        minn1=powmod(2,minn1);
        minn2=powmod(3,minn2);
        cout<<(minn1*minn2)%mod<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Chen-Jr/p/11007301.html