Master of GCD(差分+快速幂)


问题 J: Master of GCD

问题 J: Master of GCD

时间限制: 1 Sec  内存限制: 128 MB
提交: 785  解决: 162
[提交] [状态] [命题人:admin]

题目描述

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
样例输出
6
2
提示

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

这里题意就是给定一个区间[l,r]把这个区间上所以的数都乘上x(这里x只能是2或3 ),然后让你求所有数的最小公约数,这里其实就是让你求2^(最小次数)*3^(最小次数)

不过这里如果直接每次都从l更新到r的话,会超时,所以就要用到差分数组

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#define mod 998244353
const int maxn=1e5+5;
typedef long long ll;
using namespace std;
long long Mod(long long a,long long b){
	long long ans=1;
	while(b){
		if(b&1)
		    ans=ans*a%mod;
		b=b>>1;
		a=a*a%mod;
	}
	return ans;
}
int n,m;
int vis2[200005],vis3[200005];
int main(){
	int t,l,r,x;
	scanf("%d",&t);
	while(t--){
		int max2=999999,max3=999999;
		int sum2=0,sum3=0;
		memset(vis2,0,sizeof(vis2));
		memset(vis3,0,sizeof(vis3)); 
		scanf("%d%d",&n,&m);
		for(int i=0;i<m;i++){
			scanf("%d%d%d",&l,&r,&x);
			if(x==2){
				vis2[l]++;//差分数组维护
				vis2[r+1]--;
			}
			else if(x==3){
				vis3[l]++;
				vis3[r+1]--;
			}
		}
		for(int i=1;i<=n;i++){
			sum2+=vis2[i];
			sum3+=vis3[i];
			max2=min(sum2,max2);
			max3=min(sum3,max3);
		}
		long long tmp=Mod(2,max2)*Mod(3,max3)%mod;
		printf("%lld\n",tmp);
	}
	return 0;
}

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值