HDU6273 Master of GCD【差分数组】

Hakase has n numbers in a line. At fi rst, 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 fi rst 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
123456789
样例输出
6
2
12
提示
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,有T组查询,然后每组数据长度为 n ,且区间内的每一个数都是1,然后一共有 m 组数据,每组为L R X;这个[L ,R]闭区间内的数都乘以 X; 问你最后这一串数字的最大公约数,并且最后结果对 998244353取模;
解题思路:这道题的思路很不容易想,在一个区间乘2或者3,有点像差分,但是差分是和,这个是乘,等效一下,这个也是加,然后得到的数是乘2或者3的个数,然后再把这个数用快速幂便能得到答案,求得是最大公约数,现在可以先求得数组中乘2最少的次数,其他的每个数都是这个数的倍数,所以这个数用快速幂后便是最大公约数,同理3也是如此,最后这里个数相乘便是答案。
ac的代码如下:

#include<bits/stdc++.h>
using namespace std;
const int N=100010;
typedef long long LL;
const int M = 998244353;
int d[2][N];
LL qmi(int a,int b,int p){
 LL res=1%p;
 while(b){
  if(b&1)  res=res*a%p;
  a=a*(LL)a%p;
  b>>=1;
 }
 return res;
}
int main(){
 int t;
 scanf("%d",&t);
 while(t--){
  int n,m;
  memset(d,0,sizeof d);
  scanf("%d%d",&n,&m);
  while(m--){
   int l,r,x;
   scanf("%d%d%d",&l,&r,&x);
   if(x==2)   d[0][l]++,d[0][r+1]--;
   if(x==3)   d[1][l]++,d[1][r+1]--;
  }
  int min2=INT_MAX,min3=INT_MAX;
  for(int i=1;i<=n;i++){
   min2=min(min2,d[0][i]+=d[0][i-1]);
   min3=min(min3,d[1][i]+=d[1][i-1]);
  }
  printf("%lld\n",qmi(2,min2,M)*qmi(3,min3,M)%M);
 }
 return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值