【数论&线段树】【P4140】[清华集训2015]奇数国
Description
有一个长为 \(n\) 的序列,保证序列元素不超过 \(10^6\) 且其质因数集是前60个质数集合的子集。初始时全部都是 \(3\),有 \(m\) 次操作,要么要求支持单点修改,要么要求查询区间 \([l,~r]\) 的区间积 \(x\) 的欧拉函数值 \(\phi(x)\) 对一个质数取模的结果。
Limitation
\(1 \leq n,~m \leq 10^5\)
Solution
考虑一个公式
\]
证明上,考虑 \(\phi(p) = p - 1\),其中 \(p\) 是一个质数,那么对于 \(p^k\),由于它有且仅有 \(p\) 一个因数,于是 \(p^k\) 共有 \(\frac{p^k}{p} = p^{k - 1}\) 个因数,于是 \(\phi(p^k)~=~p^k - p^{k - 1}~=~p^{k-1} \times (p - 1)\)
由于欧拉函数是积性的,将每个质因子的欧拉函数乘起来即可得到上式。
于是考虑用线段树维护区间积,再状压维护区间每个质因数的出现情况,对于出现的质因数 \(p\),查询时直接将区间积乘上 \(p^{-1} \times (p - 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();
}
}
【数论&线段树】【P4140】[清华集训2015]奇数国的更多相关文章
- 「清华集训2015」V
「清华集训2015」V 题目大意: 你有一个序列,你需要支持区间加一个数并对 \(0\) 取 \(\max\),区间赋值,查询单点的值以及单点历史最大值. 解题思路: 观察发现,每一种修改操作都可以用 ...
- 清华集训2015 V
#164. [清华集训2015]V http://uoj.ac/problem/164 统计 描述 提交 自定义测试 Picks博士观察完金星凌日后,设计了一个复杂的电阻器.为了简化题目,题目中的常数 ...
- 2018.07.28 uoj#164. 【清华集训2015】V(线段树)
传送门 线段树好题. 要求支持的操作: 1.区间变成max(xi−a,0)" role="presentation" style="position: rela ...
- 【uoj#164】[清华集训2015]V 线段树维护历史最值
题目描述 给你一个长度为 $n$ 的序列,支持五种操作: $1\ l\ r\ x$ :将 $[l,r]$ 内的数加上 $x$ :$2\ l\ r\ x$ :将 $[l,r]$ 内的数减去 $x$ ,并 ...
- UOJ #164 [清华集训2015]V (线段树)
题目链接 http://uoj.ac/problem/164 题解 神仙线段树题. 首先赋值操作可以等价于减掉正无穷再加上\(x\). 假设某个位置从前到后的操作序列是: \(x_1,x_2,..., ...
- Bzoj 3813 奇数国 题解 数论+线段树+状压
3813: 奇数国 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 748 Solved: 425[Submit][Status][Discuss] ...
- 【bzoj3813】: 奇数国 数论-线段树-欧拉函数
[bzoj3813]: 奇数国 题意:给定一个序列,每个元素可以分解为最小的60个素数的形式.(x=p1^k1*p2^k2*......p60^k60)(p1=2,p2=3,…,p60=281) 支持 ...
- codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)(两种方法)
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 ...
- Codeforces 671C. Ultimate Weirdness of an Array(数论+线段树)
看见$a_i\leq 200000$和gcd,就大概知道是要枚举gcd也就是答案了... 因为答案是max,可以发现我们很容易算出<=i的答案,但是很难求出单个i的答案,所以我们可以运用差分的思 ...
随机推荐
- Laravel框架下路由的使用(源码解析)
本篇文章给大家带来的内容是关于Laravel框架下路由的使用(源码解析),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 前言 我的解析文章并非深层次多领域的解析攻略.但是参考着开发文 ...
- CSS属性相关知识
Css选择器 选择器的权重 在css中,哪个选择器的权重高,就走谁的样式. 标签选择器的权重是 1 Class选择器的权重是10 Id选择器的权重是100 行间样式的权重是1000 带有关键字 !im ...
- luogu P2495 [SDOI2011]消耗战 |虚树+LCA+dp
题目描述 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军胜利在望.已知 ...
- c语言课本及pta作业中运用到的程序思维
c语言课本运用到的程序思维 我个人觉得在写程序的时候,有很多题目会用到我们学过的解决一个程序或者一个问题的方法,把这些方法运用起来,将会使自己更加灵活地解决诸多问题,为今后打下良好地基础. (因为还没 ...
- Prometheus 监控Docker服务器及Granfanna可视化
Prometheus 监控Docker服务器及Granfanna可视化 cAdvisor(Container Advisor)用于收集正在运行的容器资源使用和性能信息. 使用Prometheus监控c ...
- POSIX 正则表达式 BRE与ERE的差异
BRE,标准正则表达式,basic regular expressions ERE,扩展正则表达式,Extended Regular Expressions POSIX 正则表达式 传统上,POSIX ...
- 阿里云 centos 无法执行moodle cron
在阿里云服务器安装moodle时,在执行cron计划任务时,报错sendmail: fatal: parameter inet_interfaces: no local interface found ...
- 微服务架构 ------ 插曲 Linux平台 Ubuntu的安装
1.一定要通过自定义安装 2.选择的硬件兼容性选择 14.x 这里介绍一下红框内的东西,是为了做虚拟存储使用的,也就是一批服务器对外展示位一个服务器,类似于服务器集群 3.选择稍后安装操作系统,如 ...
- 【开发笔记】- yml中出现特殊字符启动失败的问题
问题描述: yml配置中出现特殊字符,启动时抛出异常: Caused by: org.yaml.snakeyaml.constructor.ConstructorException: Can't co ...
- QML官方文档:Qt Quick Controls 1和2对比
Qt Quick Controls有1和2两个版本,在程序中会看到好多1和2版本混合使用的情况. 原文:https://doc.qt.io/qt-5/qtquickcontrols2-differen ...