HDU - 6273 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.
题意:给定数组a,a初始值都为1,在每个给定区间内所有数都乘2或3,找到数组a的最大公约数
分析:可以用树状数组或者线段树做,但他们的代码复杂度相对高,可以考虑到,本题中的最大公约数就是*2、*3次数最少的那个数。所以找出他们,然后利用快速幂求解
AC代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=1e6;
const ll mod=998244353;
const int inf=0x3f3f3f3f;
int n,m;
ll d[N],e[N];
ll f(ll a,ll b,ll c){
ll ans=1;
while(b){
if(b&1) ans=(ans*a)%c;
a=(a*a)%c;
b>>=1;
}
return ans%c;
}
int main(){
int t;
ll a,b,c;
scanf("%d",&t);
while(t--){
memset(d,0,sizeof(d));
memset(e,0,sizeof(e));
scanf("%d%d",&n,&m);
while(m--){
scanf("%lld%lld%lld",&a,&b,&c);
if(c==2){
d[a]++;
d[b+1]--;
}
else{
e[a]++;
e[b+1]--;
}
}
ll sum1=0,sum2=0,min1=inf,min2=inf;
for(int i=1;i<=n;i++){
sum1+=d[i];
min1=min(min1,sum1);
sum2+=e[i];
min2=min(min2,sum2);
}
ll ans=f(2,min1,mod);
ans=ans*f(3,min2,mod);
printf("%lld\n",ans%mod);
}
return 0;
}
本文详细解析了HDU-6273 Master of GCD问题,介绍了如何通过记录每个位置上*2、*3操作的次数,找出次数最少的位置,并利用快速幂求解最终的最大公约数。
379

被折叠的 条评论
为什么被折叠?



