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

样例输出

6
2

题意:给定一个区间[1,n],每次可以选择其中一个子区间进行乘2或者乘三操作,求区间内的数的最大公因子是多少

若果每次直接对区间进行操作则时间复杂度太高,会超时TT,所以就用a来记录成2操作的区间a[l]开始a[r+1]记录结束(在[l,r+1]后项加前项即可得每个点进行了几次乘二操作,b数组记录乘三操作),因为区间内的初始值全都是1所以最大公因数和操作有关,t1记录最小进行乘2的次数,t2记录最小乘三操作的次数,最大公因子就2^t2 *3^t3

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
typedef long long ll;

using namespace std;

const int maxn = 100000+2;
const ll mod = 998244353;
int a[maxn];
int b[maxn];


ll PowMod(ll a,ll b){
    ll result = 1;
    ll base = a%mod;
    while(b){
        if(b%2 == 1)
            result = (result*base)%mod;
        base = (base*base)%mod;
        b /= 2;
    }
    return result;
}

int main()
{
    int t;
    cin>>t;
    while(t--){
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        ll n,ope;
        cin>>n>>ope;
        for(int i = 0;i < ope;i++){
            ll l,r,mul;
            cin>>l>>r>>mul;
            if(mul == 2){
                a[l]++;
                a[r+1]--;
            }
            else{
                b[l]++;
                b[r+1]--;
            }
        }
        int t1 = a[1];
        int t2 = b[1];
        for(int i = 2;i<=n;i++){
            a[i] += a[i-1];
            t1 = min(t1,a[i]);
            b[i] += b[i-1];
            t2 = min(t2,b[i]);
        }
        ll t1_Pow = PowMod(2,(ll)t1);
        ll t2_Pow = PowMod(3,(ll)t2);
        ll result = (t1_Pow*t2_Pow)%mod;
        cout<<result<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值