hdu.6273 Master of GC
题目描述:
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.
输入格式:
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.
输出格式:
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
题目大意:
Q在序列[l,r]区间内乘上一个数字2或3,要求我们输出整个序列的最大公约数。
比如序列(6,6,6,12,6,6),则最大公约数为6,然后输出6。
思路:
°2的最小出现次数*3的最小出现次数=最大公约数。
°在区间内进行修改元素,而不修改其他元素,可以利用差分数组来解决;
°建立两个数组,并且都初始化为0,a数组存放 乘2 的值,b数组存放 乘3 的值。如果在[l,r]区间内进行了乘法操作,则在这个区间内进行加1。
° 建立两个临时整型变量,存放2或3出现的次数,并建议一个变量进行最小值的比较。
AC代码
点击查看代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxN = 1e5+5;
const int MOD = 998244353;
LL Mod(LL a,LL b){
LL ans = 1;
while(b){
if(b&1) ans = ans * a % MOD;
b = b>>1;
a = a * a % MOD;
}
return ans;
}
int main(){
int T;
LL a[maxN],b[maxN];
int n,m;
scanf("%d",&T);//循环案例
while(T--){
int max2=999999,max3=999999;
int sum2=0,sum3=0;
scanf("%d%d",&n,&m); //最长序列长度 //操作次数
memset(a,0,sizeof(a));memset(b,0,sizeof(b));
while(m--){
int l,r,x;scanf("%d%d%d",&l,&r,&x);
if(x==2){
a[l]++; a[r+1]--;
}else if(x==3){
b[l]++; b[r+1]--;
}
}
for(int i = 1;i<=n;i++){
sum2+=a[i];
sum3+=b[i];
max2 = min(sum2,max2);//保留2或3出现的最小次数
max3 = min(sum3,max3);
}
LL tmp = Mod(2,max2) * Mod(3,max3)%MOD;
printf("%lld\n",tmp);
}
return 0;
}

浙公网安备 33010602011771号