HDU - 6273 Master of GCD

本文详细解析了HDU-6273 Master of GCD问题,介绍了如何通过记录每个位置上*2、*3操作的次数,找出次数最少的位置,并利用快速幂求解最终的最大公约数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

  

posted on 2018-09-29 21:14 坤sir 阅读(...) 评论(...) 编辑 收藏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kunsir_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值