【数论&线段树】【P4140】[清华集训2015]奇数国
Description
有一个长为 的序列,保证序列元素不超过 且其质因数集是前60个质数集合的子集。初始时全部都是 ,有 次操作,要么要求支持单点修改,要么要求查询区间 的区间积 的欧拉函数值 对一个质数取模的结果。
Limitation
Solution
考虑一个公式
证明上,考虑 ,其中 是一个质数,那么对于 ,由于它有且仅有 一个因数,于是 共有 个因数,于是
由于欧拉函数是积性的,将每个质因子的欧拉函数乘起来即可得到上式。
于是考虑用线段树维护区间积,再状压维护区间每个质因数的出现情况,对于出现的质因数 ,查询时直接将区间积乘上 即可。
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();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2018-07-15 【神仙DP】【单调队列】【模拟题】区间覆盖