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 ≤ li≤ n), 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.
#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;
}