题目链接: [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}})
\]
参考代码
#include <bits/stdc++.h>

//#pragma GCC optimize("Ofast,unroll-loops")

#define isPbdsFile

#ifdef isPbdsFile

#include <bits/extc++.h>

#else

#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope> #endif using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; template <typename T>
int disc(T* a, int n)
{
return unique(a + 1, a + n + 1) - (a + 1);
} template <typename T>
T lowBit(T x)
{
return x & -x;
} template <typename T>
T Rand(T l, T r)
{
static mt19937 Rand(time(nullptr));
uniform_int_distribution<T> dis(l, r);
return dis(Rand);
} template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
return (a % b + b) % b;
} template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
a %= c;
T1 ans = 1;
for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
return modt(ans, c);
} template <typename T>
void read(T& x)
{
x = 0;
T sign = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')sign = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x *= sign;
} template <typename T, typename... U>
void read(T& x, U&... y)
{
read(x);
read(y...);
} template <typename T>
void write(T x)
{
if (typeid(x) == typeid(char))return;
if (x < 0)x = -x, putchar('-');
if (x > 9)write(x / 10);
putchar(x % 10 ^ 48);
} template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
write(x), putchar(c);
write(c, y...);
} template <typename T11, typename T22, typename T33>
struct T3
{
T11 one;
T22 tow;
T33 three; bool operator<(const T3 other) const
{
if (one == other.one)
{
if (tow == other.tow)return three < other.three;
return tow < other.tow;
}
return one < other.one;
} T3() { one = tow = three = 0; } T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
{
}
}; template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
if (x < y)x = y;
} template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
if (x > y)x = y;
} constexpr int N = 2e5 + 10;
constexpr ll MOD = 1ll << 32;
constexpr i128 INF = 1e18 + 10; struct Node
{
ll lcm;
ll sum;
} node[N << 2]; auto GCD(const ll x, const ll y) -> ll
{
if (!y)return x;
return GCD(y, x % y);
} auto LCM(const ll x, const ll y) -> ll
{
return min(static_cast<i128>(x) / GCD(x, y) * y, INF);
} inline void push_up(const int curr)
{
node[curr].sum = (node[ls(curr)].sum + node[rs(curr)].sum) % MOD;
node[curr].lcm = LCM(node[ls(curr)].lcm, node[rs(curr)].lcm), INF;
} inline void update(const int curr, const int l, const int r, const int s, const int e, const ll x)
{
if (l <= s and e <= r)
{
if (x % node[curr].lcm == 0)return;
if (s == e)
{
node[curr].lcm = GCD(node[curr].lcm, x);
node[curr].sum = node[curr].lcm;
return;
}
}
const int mid = (s + e) >> 1;
if (l <= mid)update(ls(curr), l, r, s, mid, x);
if (r > mid)update(rs(curr), l, r, mid + 1, e, x);
push_up(curr);
} inline ll query(const int curr, const int l, const int r, const int s, const int e)
{
if (l <= s and e <= r)return node[curr].sum;
ll ans = 0;
const int mid = (s + e) >> 1;
if (l <= mid)ans = (ans + query(ls(curr), l, r, s, mid)) % MOD;
if (r > mid)ans = (ans + query(rs(curr), l, r, mid + 1, e)) % MOD;
return ans;
} inline void build(const int curr, const int l, const int r)
{
if (l == r)
{
read(node[curr].sum);
node[curr].lcm = node[curr].sum;
return;
}
const int mid = (l + r) >> 1;
build(ls(curr), l, mid);
build(rs(curr), mid + 1, r);
push_up(curr);
} int n, q; inline void solve()
{
read(n, q);
build(1, 1, n);
while (q--)
{
int op;
read(op);
if (op == 1)
{
int l, r;
ll x;
read(l, r, x);
update(1, l, r, 1, n, x);
}
else
{
int l, r;
read(l, r);
write(endl, query(1, l, r, 1, n));
}
}
} signed int main()
{
Spider
//------------------------------------------------------
int test = 1;
// read(test);
// cin >> test;
forn(i, 1, test)solve();
// while (cin >> n, n)solve();
// while (cin >> test)solve();
}

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. 什么是 Serverless 架构?

    随着时间的推移,Serverless 架构变得越来越火热,凭借着极致弹性.按量付费.低成本运维等特性,在很多领域发挥着越来越重要的作用:机器学习领域在近些年也非常火热,并在越来越多的行业中得到应用. ...

  2. SpringCloud学习 系列一、 前言-为什么要学习微服务

    系列导航 SpringCloud学习 系列一. 前言-为什么要学习微服务 SpringCloud学习 系列二. 简介 SpringCloud学习 系列三. 创建一个没有使用springCloud的服务 ...

  3. 【rt-thread】构建自己的项目工程 -- 初始篇

    现以stm32f429igt6芯片的板子 & Keil5编译环境为例,记述构建适配自己板子的rt-thread工程的过程 1.拿到rt-thread源码,进入bsp/stm32/librari ...

  4. asp.net core 开启gzip压缩

    // 第一步: 配置gzip与br的压缩等级为最优 services.Configure<BrotliCompressionProviderOptions>(options => { ...

  5. MyBatis——第一个程序

    MyBatis1:初识 MyBatis第一个程序 流程:搭建环境–>导入MyBatis–>编写代码–>测试 1.创建一张User表. 关键字id.username.pwd 2.导入相 ...

  6. 百度网盘(百度云)SVIP超级会员共享账号每日更新(2023.11.23)

    一.百度网盘SVIP超级会员共享账号 可能很多人不懂这个共享账号是什么意思,小编在这里给大家做一下解答. 我们多知道百度网盘很大的用处就是类似U盘,不同的人把文件上传到百度网盘,别人可以直接下载,避免 ...

  7. SQLServer解决deadlock问题的一个场景

    SQLServer解决deadlock问题的一个场景 背景 公司产品出现过很多次dead lock 跟研发讨论了很久, 都没有具体的解决思路 但是这边知道了一个SQLServer数据库上面计划100% ...

  8. Jmeter之二_JSR223取样器,断言等添加失败的解决办法

    Jmeter之二_JSR223取样器,断言等添加失败的解决办法 背景 最近在学习jmeter 但是发现在进行JSR223的相关取样器以及断言处理时出现了错误: java.lang.NoClassDef ...

  9. vCenter 宕机后证书续期处理

    vCenter 宕机后证书续期处理 背景 最近护网, 我司被选中作为防守方 因为发现一个vCenter控制台出现异常访问, 被管理员强行断网. 因为是周六的事情,当时自己也没太在意,想着工作日添加网络 ...

  10. Redis7.0 编译安装以及简单创建Cluster测试服务器的方法

    背景 北京时间2022.4.27 晚上九点半左右, Redis 7.0.0 已经GA. 为了进行简单的学习, 这边进行了简单验证工作. 本次主要分为编译, 测试集群搭建,以及springboot进行简 ...