题目链接: [Ynoi Easy Round 2023] TEST_69

首先GCD有比较良好的一些性质。我们观察到一次 \(GCD(a_i,x)\) 操作,会有以下两种变化。

  1. 如果 \(x \bmod a_i == 0\),那么很显然 \(\gcd(a_i,x)==a_i\),不会发生任何改变。
  2. 如果不是情况 \(1\),那么很显然 \(a_i\) 至少会变为原来的一半,因为容易知道 \(a_i\) 的最小因子不会小于 \(2\),所以由于公因子是成对出现的,另一个对应的公因子则是最大为 \(\frac{a_i}{2}\),所以最大因子显然不超过 \(\frac{a_i}{2}\)。不断减小,最终为 \(1\),这个过程至多进行 \(\log{a_i}\) 次。
考虑暴力一点的做法,为每个 \(a_i\) 进行反复的有效操作,容易发现,最终也至多执行 \(\sum_{i=1}^{n}{\log{a_i}}\) 次,很显然,如果每次操作我们都保证有效的,那么这个次数也是完全接受的。

瓶颈点:如何快速判断出无效的状态。这里有个技巧性质:第一种操作情况可以拓展到 \(LCM\) 上。

\[\text{如果有 } x \bmod lcm_{区间}==0 \text{,则这个区间内所有数都不会变化}
\]

很显然这个是对的,第一条其实可以翻译成如果 \(x\) 是一个数的倍数,那么这个操作不会发生变化,一堆数的公倍数,显然就是说的最小公倍数了。

算法框架

用线段树维护维护区间最小倍数,当遇到可以 \(x \bmod lcm_{区间} ==0\) 时,则停止往下递归,否则往下递归一直到单点修改。

\[\text{最终复杂度为建树+查询与修改:} O(n\log{V_{max}}+m\log{n}\log{V_{max}})
\]
参考代码
  1. #include <bits/stdc++.h>
  2. //#pragma GCC optimize("Ofast,unroll-loops")
  3. #define isPbdsFile
  4. #ifdef isPbdsFile
  5. #include <bits/extc++.h>
  6. #else
  7. #include <ext/pb_ds/priority_queue.hpp>
  8. #include <ext/pb_ds/hash_policy.hpp>
  9. #include <ext/pb_ds/tree_policy.hpp>
  10. #include <ext/pb_ds/trie_policy.hpp>
  11. #include <ext/pb_ds/tag_and_trait.hpp>
  12. #include <ext/pb_ds/hash_policy.hpp>
  13. #include <ext/pb_ds/list_update_policy.hpp>
  14. #include <ext/pb_ds/assoc_container.hpp>
  15. #include <ext/pb_ds/exception.hpp>
  16. #include <ext/rope>
  17. #endif
  18. using namespace std;
  19. using namespace __gnu_cxx;
  20. using namespace __gnu_pbds;
  21. typedef long long ll;
  22. typedef long double ld;
  23. typedef pair<int, int> pii;
  24. typedef pair<ll, ll> pll;
  25. typedef tuple<int, int, int> tii;
  26. typedef tuple<ll, ll, ll> tll;
  27. typedef unsigned int ui;
  28. typedef unsigned long long ull;
  29. typedef __int128 i128;
  30. #define hash1 unordered_map
  31. #define hash2 gp_hash_table
  32. #define hash3 cc_hash_table
  33. #define stdHeap std::priority_queue
  34. #define pbdsHeap __gnu_pbds::priority_queue
  35. #define sortArr(a, n) sort(a+1,a+n+1)
  36. #define all(v) v.begin(),v.end()
  37. #define yes cout<<"YES"
  38. #define no cout<<"NO"
  39. #define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
  40. #define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
  41. #define forn(i, a, b) for(int i = a; i <= b; i++)
  42. #define forv(i, a, b) for(int i=a;i>=b;i--)
  43. #define ls(x) (x<<1)
  44. #define rs(x) (x<<1|1)
  45. #define endl '\n'
  46. //用于Miller-Rabin
  47. [[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
  48. template <typename T>
  49. int disc(T* a, int n)
  50. {
  51. return unique(a + 1, a + n + 1) - (a + 1);
  52. }
  53. template <typename T>
  54. T lowBit(T x)
  55. {
  56. return x & -x;
  57. }
  58. template <typename T>
  59. T Rand(T l, T r)
  60. {
  61. static mt19937 Rand(time(nullptr));
  62. uniform_int_distribution<T> dis(l, r);
  63. return dis(Rand);
  64. }
  65. template <typename T1, typename T2>
  66. T1 modt(T1 a, T2 b)
  67. {
  68. return (a % b + b) % b;
  69. }
  70. template <typename T1, typename T2, typename T3>
  71. T1 qPow(T1 a, T2 b, T3 c)
  72. {
  73. a %= c;
  74. T1 ans = 1;
  75. for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
  76. return modt(ans, c);
  77. }
  78. template <typename T>
  79. void read(T& x)
  80. {
  81. x = 0;
  82. T sign = 1;
  83. char ch = getchar();
  84. while (!isdigit(ch))
  85. {
  86. if (ch == '-')sign = -1;
  87. ch = getchar();
  88. }
  89. while (isdigit(ch))
  90. {
  91. x = (x << 3) + (x << 1) + (ch ^ 48);
  92. ch = getchar();
  93. }
  94. x *= sign;
  95. }
  96. template <typename T, typename... U>
  97. void read(T& x, U&... y)
  98. {
  99. read(x);
  100. read(y...);
  101. }
  102. template <typename T>
  103. void write(T x)
  104. {
  105. if (typeid(x) == typeid(char))return;
  106. if (x < 0)x = -x, putchar('-');
  107. if (x > 9)write(x / 10);
  108. putchar(x % 10 ^ 48);
  109. }
  110. template <typename C, typename T, typename... U>
  111. void write(C c, T x, U... y)
  112. {
  113. write(x), putchar(c);
  114. write(c, y...);
  115. }
  116. template <typename T11, typename T22, typename T33>
  117. struct T3
  118. {
  119. T11 one;
  120. T22 tow;
  121. T33 three;
  122. bool operator<(const T3 other) const
  123. {
  124. if (one == other.one)
  125. {
  126. if (tow == other.tow)return three < other.three;
  127. return tow < other.tow;
  128. }
  129. return one < other.one;
  130. }
  131. T3() { one = tow = three = 0; }
  132. T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
  133. {
  134. }
  135. };
  136. template <typename T1, typename T2>
  137. void uMax(T1& x, T2 y)
  138. {
  139. if (x < y)x = y;
  140. }
  141. template <typename T1, typename T2>
  142. void uMin(T1& x, T2 y)
  143. {
  144. if (x > y)x = y;
  145. }
  146. constexpr int N = 2e5 + 10;
  147. constexpr ll MOD = 1ll << 32;
  148. constexpr i128 INF = 1e18 + 10;
  149. struct Node
  150. {
  151. ll lcm;
  152. ll sum;
  153. } node[N << 2];
  154. auto GCD(const ll x, const ll y) -> ll
  155. {
  156. if (!y)return x;
  157. return GCD(y, x % y);
  158. }
  159. auto LCM(const ll x, const ll y) -> ll
  160. {
  161. return min(static_cast<i128>(x) / GCD(x, y) * y, INF);
  162. }
  163. inline void push_up(const int curr)
  164. {
  165. node[curr].sum = (node[ls(curr)].sum + node[rs(curr)].sum) % MOD;
  166. node[curr].lcm = LCM(node[ls(curr)].lcm, node[rs(curr)].lcm), INF;
  167. }
  168. inline void update(const int curr, const int l, const int r, const int s, const int e, const ll x)
  169. {
  170. if (l <= s and e <= r)
  171. {
  172. if (x % node[curr].lcm == 0)return;
  173. if (s == e)
  174. {
  175. node[curr].lcm = GCD(node[curr].lcm, x);
  176. node[curr].sum = node[curr].lcm;
  177. return;
  178. }
  179. }
  180. const int mid = (s + e) >> 1;
  181. if (l <= mid)update(ls(curr), l, r, s, mid, x);
  182. if (r > mid)update(rs(curr), l, r, mid + 1, e, x);
  183. push_up(curr);
  184. }
  185. inline ll query(const int curr, const int l, const int r, const int s, const int e)
  186. {
  187. if (l <= s and e <= r)return node[curr].sum;
  188. ll ans = 0;
  189. const int mid = (s + e) >> 1;
  190. if (l <= mid)ans = (ans + query(ls(curr), l, r, s, mid)) % MOD;
  191. if (r > mid)ans = (ans + query(rs(curr), l, r, mid + 1, e)) % MOD;
  192. return ans;
  193. }
  194. inline void build(const int curr, const int l, const int r)
  195. {
  196. if (l == r)
  197. {
  198. read(node[curr].sum);
  199. node[curr].lcm = node[curr].sum;
  200. return;
  201. }
  202. const int mid = (l + r) >> 1;
  203. build(ls(curr), l, mid);
  204. build(rs(curr), mid + 1, r);
  205. push_up(curr);
  206. }
  207. int n, q;
  208. inline void solve()
  209. {
  210. read(n, q);
  211. build(1, 1, n);
  212. while (q--)
  213. {
  214. int op;
  215. read(op);
  216. if (op == 1)
  217. {
  218. int l, r;
  219. ll x;
  220. read(l, r, x);
  221. update(1, l, r, 1, n, x);
  222. }
  223. else
  224. {
  225. int l, r;
  226. read(l, r);
  227. write(endl, query(1, l, r, 1, n));
  228. }
  229. }
  230. }
  231. signed int main()
  232. {
  233. Spider
  234. //------------------------------------------------------
  235. int test = 1;
  236. // read(test);
  237. // cin >> test;
  238. forn(i, 1, test)solve();
  239. // while (cin >> n, n)solve();
  240. // while (cin >> test)solve();
  241. }

P9989 [Ynoi Easy Round 2023] TEST_69 题解的更多相关文章

  1. Codeforces Round #539 Div1 题解

    Codeforces Round #539 Div1 题解 听说这场很适合上分QwQ 然而太晚了QaQ A. Sasha and a Bit of Relax 翻译 有一个长度为\(n\)的数组,问有 ...

  2. UOJ Easy Round#7

    UOJ Easy Round#7 传送门:http://uoj.ac/contest/35 题解:http://matthew99.blog.uoj.ac/blog/2085 #1 题意: 在一个(2 ...

  3. Codeforces Round #543 Div1题解(并不全)

    Codeforces Round #543 Div1题解 Codeforces A. Diana and Liana 给定一个长度为\(m\)的序列,你可以从中删去不超过\(m-n*k\)个元素,剩下 ...

  4. Codeforces Round #545 Div1 题解

    Codeforces Round #545 Div1 题解 来写题解啦QwQ 本来想上红的,结果没做出D.... A. Skyscrapers CF1137A 题意 给定一个\(n*m\)的网格,每个 ...

  5. CF Educational Round 78 (Div2)题解报告A~E

    CF Educational Round 78 (Div2)题解报告A~E A:Two Rival Students​ 依题意模拟即可 #include<bits/stdc++.h> us ...

  6. Educational Codeforces Round 64 部分题解

    Educational Codeforces Round 64 部分题解 不更了不更了 CF1156D 0-1-Tree 有一棵树,边权都是0或1.定义点对\(x,y(x\neq y)\)合法当且仅当 ...

  7. CF Round #580(div2)题解报告

    CF Round #580(div2)题解报告 T1 T2 水题,不管 T3 构造题,证明大约感性理解一下 我们想既然存在解 \(|a[n + i] - a[i]| = 1\) 这是必须要满足的 既然 ...

  8. Educational Codeforces Round 64部分题解

    Educational Codeforces Round 64部分题解 A 题目大意:给定三角形(高等于低的等腰),正方形,圆,在满足其高,边长,半径最大(保证在上一个图形的内部)的前提下. 判断交点 ...

  9. 【题解】Comet OJ Round 70 简要题解

    [题解]Comet OJ Round 70 简要题解 A 将放在地上的书按照从小到大排序后,问题的本质就变成了合并两个序列使得字典序最小.可以直接模拟归并排序.直接用循环和std::merge实现这个 ...

  10. Codeforces Round div2 #541 题解

    codeforces Round #541 abstract: I构造题可能代码简单证明很难 II拓扑排序 III并查集 启发式排序,带链表 IV dp 处理字符串递推问题 V 数据结构巧用:于二叉树 ...

随机推荐

  1. 为什么很多候选人投出去的简历石沉大海(面向Java方向)

    我最近在帮上海某培训学校里的毕业生做面试辅导,普遍发现很多候选人不是没能力,或者说能力没有差到没有面试机会的程度,但这些同学投出去的简历大多石沉大海,即使有回应,也大多是些外包外派公司或者小公司. 而 ...

  2. 负载均衡--rpc服务端

    1. dubbo负载均衡的作用? 其出发点,自然也就是普通的负载均衡器的出发点了. 将负载均衡功能实现在rpc客户端侧,以便能够随时适应外部的环境变化,更好地发挥硬件作用. 而且客户端的负载均衡天然地 ...

  3. C#设计模式15——观察者模式的写法

    是什么: 观察者模式是一种设计模式,它定义了对象之间的一种一对多的依赖关系,使得当一个对象状态发生改变时,它的所有依赖者都能够得到相应的通知并作出相应的反应.观察者模式也被称为发布-订阅模式. 为什么 ...

  4. 通过dockerfile构建微服务的镜像发布

    本文为博主原创,未经允许不得转载: 目录: 1. dockerfile 的文件使用讲解 2. dockerfile 常用指令 3. 通过dockerfile 进行微服务发布 1. dockerfile ...

  5. 使用requests爬虫遇到的一个奇葩的问题:UnicodeEncodeError: 'latin-1' codec can't encode character

    每一位成功的程序员,背后也许都站着无数的秃头的男人--为其提供各种开发工具&代码库,当然也包括-- 各种玄学bug-- 玄学的开端 最近在用Python做一个爬虫项目的时候遇到一个很奇怪的问题 ...

  6. JDK21更新特性详解

    有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步,认准https://blog.zysicyj.top 首发博客地址 文章更新计划 文章更新计划 | 430: | String T ...

  7. [转帖]CentOS-7-x86_64-Everything-2009 rpm包列表(CentOS7.9)

    CentOS-7-x86_64-Everything-2009 rpm包列表(CentOS7.9) 共10073个文件 复制389-ds-base-1.3.10.2-6.el7.x86_64.rpm ...

  8. [转帖]s3对象存储挂载到本地文件夹

    https://www.zhangzhuo.ltd/articles/2021/10/22/1634888049032.html 一.s3fs工具 s3fs-fuse 是一个采用 c++ 开发的开源应 ...

  9. [转帖]5、kafka监控工具Kafka-Eagle介绍及使用

    https://zhuanlan.zhihu.com/p/628039102   # Apache Kafka系列文章 1.kafka(2.12-3.0.0)介绍.部署及验证.基准测试 2.java调 ...

  10. Redis scan等命令的学习与研究

    Redis scan等命令的学习与研究 摘要 背景跟前几天说的一个问题类似. 为了验证自己的设想, 所以晚上继续写脚本进行了一轮次的验证. 不过上次讨论时,打击好像都没听懂我说的 所以这次准备从基础开 ...