HDU-6273 Master of GCD

Master of GCD

Hakase has n numbers in a 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 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.

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 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.

Output

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.

 Example

standard input standard output

2

5 3

1 3 2

3 5 2

扫描二维码关注公众号,回复: 1013008 查看本文章

1 5 3                            6

6 3 

1 2 2

5 6 2

1 6 2                            2

Explanation

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

题意:t组数据,每组n,m。然后输出l,r,c(c为2或3)  l-r区间乘以c,然后求1~n的最大公约数。

思路:如果没更新区间1~n的最大公约数是1,其实我们只需要计算1~n 2和3的都有个数,然后乘起来就是最大公约数,数据太大要用LL,然后%998244353.

我们只需要维护一个树状数组,每次更新2,3的区间个数,然后再搞个快速幂就行。

反思:没开LL,wa了两次。(傻逼

#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
const int maxn = 100005;
const int Mod = 998244353;
ll sum2[maxn],sum3[maxn];
ll lowbit(ll k)
{
    return k&(-k);
}
ll quickpow(ll a,ll b)
{
    ll ans=1;
    a=a%Mod;
    while(b!=0)
    {
        if(b&1) ans=(ans*a)%Mod;
        b>>=1;
        a=(a*a)%Mod;
    }
    return ans;
}
void updata(ll k,ll *a,ll c)
{
    while(k<maxn)
    {
        a[k]+=c;
        k+=lowbit(k);
    }
}
ll query(ll k,ll *a)
{
    ll ans=0;
    while(k>0)
    {
        ans+=a[k];
        k-=lowbit(k);
    }
    return ans;
}
int main()
{
    ll t,n,m,l,r,c;
    scanf("%lld",&t);
    while(t--)
    {
        memset(sum2,0,sizeof(sum2));
        memset(sum3,0,sizeof(sum3));
        ll ans=0;
        scanf("%lld %lld",&n,&m);
        while(m--)
        {
            scanf("%lld %lld %lld",&l,&r,&c);
            if(c==2)
            {
                updata(l,sum2,1);
                updata(r+1,sum2,-1);
            }
            else
            {
                updata(l,sum3,1);
                updata(r+1,sum3,-1);
            }
        }
        ll min2=query(1,sum2);
        ll min3=query(1,sum3);
        for(int i=2; i<=n; i++)
        {
            min2=min(min2,query(i,sum2));
            min3=min(min3,query(i,sum3));
        }
        printf("%lld\n",(quickpow(2,min2)*quickpow(3,min3))%Mod);
    }
}

PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~

猜你喜欢

转载自www.cnblogs.com/MengX/p/9084932.html