# 【数论&线段树】【P4140】[清华集训2015]奇数国

## Limitation

$1 \leq n,~m \leq 10^5$

## Solution

$\phi(x) = \prod_{i = 1}^{60} p_i^{c_i - 1} \times (p_i - 1)$

## Code

#include <cstdio>
#include <algorithm>

const int maxn = 100005;
const int MOD = 19961993;

int n = 100000, q;
const int prm[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281};
const int inv[] = {9980997, 6653998, 11977196, 8555140, 5444180, 1535538, 10568114, 14708837, 3471651, 11701858, 17386252, 1618540, 16066970, 2321162, 18263100, 16948862, 12518538, 15380552, 10725847, 1686929, 13399146, 17182475, 12025297, 15924736, 13582387, 395287, 6395590, 15857658, 16299242, 6359573, 3300802, 18742940, 6702567, 10914471, 16210746, 11765678, 5340151, 18247466, 7769638, 8077107, 11932588, 6506948, 1985748, 6619521, 5877135, 4413707, 9744480, 10115270, 14597757, 16475182, 18334191, 5011379, 18885205, 7555336, 621385, 11309266, 12170137, 12006660, 18304499, 11153142};

struct Tree {
int l, r;
ll v, oc;
Tree *ls, *rs;

Tree() : v(3), oc(2), ls(NULL), rs(NULL) {}

inline void pushup() {
this->v = this->ls->v * this->rs->v % MOD;
this->oc = this->ls->oc | this->rs->oc;
}

inline bool inrange(const int l, const int r) { return (this->l >= l) && (this->r <= r); }
inline bool outofrange(const int l, const int r) { return (this->l > r) || (this->r < l); }
};
Tree *rot;

void build(Tree *const u, const int l, const int r);
void update(Tree *const u, const int p, const int v);
std::pair<ll, ll> query(Tree *const u, const int l, const int r);

int main() {
freopen("1.in", "r", stdin);
qr(q);
build(rot = new Tree, 1, n);
int a, b, c;
while (q--) {
a = b = c = 0; qr(a); qr(b); qr(c);
if (a == 0) {
auto _ret = query(rot, b, c);
for (int i = 0; i < 60; ++i) if (_ret.second & (1ll << i)) {
_ret.first = _ret.first * inv[i] % MOD * (prm[i] - 1) % MOD;
}
qw(_ret.first, '\n', true);
} else {
update(rot, b, c);
}
}
return 0;
}

void build(Tree *const u, const int l, const int r) {
if ((u->l = l) == (u->r = r)) return;
int mid = (l + r) >> 1;
build(u->ls = new Tree, l, mid);
build(u->rs = new Tree, mid + 1, r);
u->pushup();
}

std::pair<ll, ll> query(Tree *const u, const int l, const int r) {
if (u->inrange(l, r)) {
return std::make_pair(u->v, u->oc);
} else if (u->outofrange(l, r)) {
return std::make_pair(1ll, 0ll);
} else {
auto lr = query(u->ls, l, r), rr = query(u->rs, l, r);
return std::make_pair(lr.first * rr.first % MOD, lr.second | rr.second);
}
}

void update(Tree *const u, const int p, const int v) {
if (u->outofrange(p, p)) {
return;
} else if (u->l == u->r) {
u->v = v; u->oc = 0;
for (int i = 0; i < 60; ++i) if (!(v % prm[i])) {
u->oc |= 1ll << i;
}
} else {
update(u->ls, p, v); update(u->rs, p, v);
u->pushup();
}
}

posted @ 2019-07-15 09:02  一扶苏一  阅读(186)  评论(0编辑  收藏  举报