【清橙A1339】JZPLCM(顾昱洲) (树状数组)

Description

给定一长度为n的正整数序列a,有q次询问,每次询问一段区间内所有数的lcm(即最小公倍数)。由于答案可能很大,输出答案模1000000007。


Solution

我们将每个数的每个因数看做一件物品。
比如: ai=pkii a i = ∏ p i k i ,对于其中一个 pi p i ,我们拆成 ki k i 个物品,分别编号为 1ki 1 … k i
我们将所有询问离线并挂在右端点,从左向右依次处理,用树状数组维护,将当前物品加上,在每个物品的上一次出现的位置减去即可。


Code

/************************************************
 * Au: Hany01
 * Prob: b
 * Email: hany01dxx@gmail.com & hany01@foxmail.com
 * Inst: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define ALL(a) a.begin(), a.end()
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
    static int _, __; static char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 1e5 + 5, MOD = 998244353;

int n, m, a[maxn];
vector<PII> Q[maxn];
unordered_map<int, int> las;

inline int Pow(int a, int b) {
    static int Ans;
    for (Ans = 1; b; b >>= 1, a = (LL)a * a % MOD) if (b & 1) Ans = (LL)Ans * a % MOD;
    return Ans;
}

struct BIT {
    int c[maxn];
#define lb(x) ((x) & -(x))
    inline void init() { For(i, 1, n) c[i] = 1; }
    inline void update(int x, int dt) { for ( ; x <= n; x += lb(x)) c[x] = (LL)c[x] * dt % MOD; }
    inline int  query (int x) { int Ans = 1; for ( ; x; x -= lb(x)) Ans = (LL)Ans * c[x] % MOD; return Ans; }
    inline int  query (int x, int y) { return (LL)query(y) * Pow(query(x - 1), MOD - 2) % MOD; }
}bit;

inline void work(int pos, int a, int b) {
    int t = 1;
    For(i, 1, b) {
        t = (LL)t * a % MOD;
        if (las[t]) bit.update(las[t], Pow(a, MOD - 2));
        las[t] = pos;
    }
    bit.update(pos, t);
}

int main()
{
    freopen("b.in", "r", stdin);
    freopen("b.out", "w", stdout);

    static int l, r, t, ans[maxn], cnt;

    n = read(), m = read();
    For(i, 1, n) a[i] = read();
    For(i, 1, m) l = read(), r = read(), Q[r].push_back(mp(l, i));

    bit.init();
    For(i, 1, n) {
        t = a[i];
        for (register int j = 2; j * j <= t; ++ j) if (!(t % j)) {
            cnt = 0;
            do t /= j, ++ cnt; while (!(t % j));
            work(i, j, cnt);
        }
        if (t != 1) work(i, t, 1);
        rep(j, SZ(Q[i])) ans[Q[i][j].y] = bit.query(Q[i][j].x, i);
    }
    For(i, 1, m) printf("%d\n", ans[i]);

    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值