当前位置:网站首页>【8.4】代码源 - 【数学】【历法】【删库】【不朴素的数列(Bonus)】
【8.4】代码源 - 【数学】【历法】【删库】【不朴素的数列(Bonus)】
2022-08-05 03:51:00 【ZhgDgE】
#47. 数学
题意:给定整数 n n n ,胖教授想将 1 ∼ n 1∼n 1∼n 这 n n n 个数字分成两组,每一组至少有一个数,并且使得两组数字的和的最大公约数最大,请输出最大的最大公约数。
思路:首先 1 ∼ n × ( n + 1 ) 2 1\sim \frac {n\times (n+1)} 2 1∼2n×(n+1) 的任意一个数都能被这些数表示,那么问题转化为有两个正整数满足 a + b = m = n × ( n + 1 ) 2 a+b=m= \frac {n\times (n+1)} 2 a+b=m=2n×(n+1) ,最大化 gcd ( a , b ) \gcd(a,b) gcd(a,b)
我们由辗转相除法的定义推一下式子 gcd ( a , b ) = gcd ( a , m − a ) = gcd ( a , m ) \gcd(a,b)=\gcd(a,m-a)=\gcd(a,m) gcd(a,b)=gcd(a,m−a)=gcd(a,m) , a a a 的取值为 a ∈ [ 1 , m − 1 ] a\in[1,m-1] a∈[1,m−1] ,那么问题转化为找到 m m m 的最大因子,根号枚举找一下最小质因子即可。
AC代码:http://oj.daimayuan.top/submission/317961
#913. 历法
题意:
题解:(数学) 代码源每日一题 Div1 历法
思路:和 这道题 挺像的。抽象一下:找到一对 x , y x,y x,y 满足 0 ≤ x , y < m i n ( m , d ) 0\leq x,y <min(m,d) 0≤x,y<min(m,d) 且 x × d + y = y × d + x ( m o d w ) x\times d+y=y\times d+x(\bmod w) x×d+y=y×d+x(modw) 。化简一下, ( y − x ) × ( d − 1 ) = 0 ( m o d w ) (y-x)\times(d-1)=0(\bmod w) (y−x)×(d−1)=0(modw) ,即 w ∣ ( y − x ) × ( d − 1 ) w|(y-x)\times(d-1) w∣(y−x)×(d−1) ,把 d − 1 d-1 d−1 移到左边 w gcd ( w , d − 1 ) ∣ y − x \frac w {\gcd(w,d-1)}|y-x gcd(w,d−1)w∣y−x ,即 y = x ( m o d w gcd ( w , d − 1 ) ) y=x(\bmod \frac w {\gcd(w,d-1)}) y=x(modgcd(w,d−1)w) 。我们把 [ 0 , m i n ( m , d ) ) [0,min(m,d)) [0,min(m,d)) 分为若干个同余系,每个同余系都有 C c n t 2 C_{cnt}^2 Ccnt2 个贡献。计算一下即可。
总结了一下:
有连续的 n n n 个整数,那么这些整数在模 p p p 意义下,按照同余系的大小分为两种:
- 有 n % p n\%p n%p 组同余系,大小为 ⌈ n p ⌉ \left \lceil \frac n p \right \rceil ⌈pn⌉
- 有 p − n % p p-n\%p p−n%p 组同余系,大小为 ⌊ n p ⌋ \left \lfloor \frac n p \right \rfloor ⌊pn⌋
AC代码:http://oj.daimayuan.top/submission/318549
#855. 删库
题意:
题解:(贪心/字典树) 代码源每日一题 Div1 删库
思路:字典树上 dfs + 贪心。开始的时候忘了还有字典树这东西了。
首先我们构建字典树。我们选择一个字符串并删去集合中对应的字符串,等价于在字典树上割去某子树,子树上的标记点 ≤ k \leq k ≤k 。
那么贪心思路就是:我们 dfs 完某个节点的儿子后,统计一下该子树上还有多少个标记点。如果标记点 ≤ k \leq k ≤k ,那我们的贪心策略就是直接回溯,不在此节点删除,因为我们回溯后可以和其他兄弟子树上的标记点结合,一起被删去,使得删除次数最小化;反之,我们必须要在此点删除某些儿子子树。我们依次删掉标记点个数最多的子树,使得回溯的标记点更少。
AC代码:http://oj.daimayuan.top/submission/318499
#883. 不朴素的数列(Bonus)
题意:给定长度为 n ( 1 ≤ n ≤ 1 0 6 ) n(1\leq n\leq 10^6) n(1≤n≤106) 的序列 a ( 0 ≤ a ≤ 1 0 6 ) a(0\leq a\leq 10^6) a(0≤a≤106) ,每次操作可以把一个数减一,与这个数相邻的数加一。问最少操作数使得序列 gcd ( a ) ≥ 2 \gcd(a)\geq 2 gcd(a)≥2 。
题解:(枚举/贪心) 代码源每日一题 Div1 不朴素的数列(Bonus)
思路:首先有一个定理:序列的 gcd \gcd gcd 等于序列差分的 gcd \gcd gcd ,即 gcd ( a 1 , a 2 , ⋯ , a n − 1 , a n ) = gcd ( a 1 , a 2 − a 1 , ⋯ , a i − a i − 1 , ⋯ , a n − a n − 1 ) \gcd(a_1,a_2,\cdots,a_{n-1},a_n)=\gcd(a_1,a_2-a_1,\cdots,a_i-a_{i-1},\cdots,a_n-a_{n-1}) gcd(a1,a2,⋯,an−1,an)=gcd(a1,a2−a1,⋯,ai−ai−1,⋯,an−an−1) 。同理,序列的 gcd \gcd gcd 等于序列前缀和的 gcd \gcd gcd 。
我们对序列的相邻数的操作等价于对前缀和序列单点加或减。我们枚举 s u m sum sum 的因子 x x x ,问题转化为操作多少次使得每个前缀都是 x x x 的倍数, ∑ i = 1 n − 1 min ( a i % p , p − a i % p ) \sum_{i=1}^{n-1}{\min(a_i\%p,p-a_i\%p)} ∑i=1n−1min(ai%p,p−ai%p) 。
题解则从一个经典问题类比过来。
给定两个序列 a , b a,b a,b ,也是类似的相邻数加减操作,问对 a a a 至少操作多少次使得与 b b b 完全相等。
定义 s t e p i step_i stepi 表示为前 i i i 个数操作为完全相等的最少次数,那么 s t e p i = s t e p i − 1 + ∣ s a i − s b i ∣ step_i=step_{i-1}+|sa_i-sb_i| stepi=stepi−1+∣sai−sbi∣ 。可以理解为,当前面 i i i 个数操作完之后,会从 a i a_i ai 这个数这里拿走或者送来若干数,我们为了使得 a i = b i a_i=b_i ai=bi ,需要把一些数拿来或者送走若干个数给 a i + 1 a_{i+1} ai+1 ,操作次数就是 ∣ s a i − s b i ∣ |sa_i-sb_i| ∣sai−sbi∣
边栏推荐
- 调用阿里云oss和sms服务
- 2022 Hangzhou Electric Multi-School 1st Game
- Redis1:Redis介绍、Redis基本特性、关系型数据库、非关系型数据库、数据库发展阶段
- DEJA_VU3D - Cesium功能集 之 056-智图Arcgis地图纠偏
- Burp installation and proxy settings
- Mathematics - Properties of Summation Symbols
- Thinking (88): Use protobuf custom options for multi-version management of data
- Redis key basic commands
- 2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer appears after successful startup of presto
- [论文笔记] MapReduce: Simplified Data Processing on Large Clusters
猜你喜欢

Shell script: for loop and the while loop
![[Filter tracking] based on matlab unscented Kalman filter inertial navigation + DVL combined navigation [including Matlab source code 2019]](/img/c9/fff226b6d33a773b59a0314a99a788.png)
[Filter tracking] based on matlab unscented Kalman filter inertial navigation + DVL combined navigation [including Matlab source code 2019]

Web3.0 Dapps——通往未来金融世界的道路

2022高处安装、维护、拆除考试题模拟考试题库及在线模拟考试

炎炎夏日教你利用小米智能家居配件+树莓派4接入Apple HomeKit

UE4 第一人称角色模板 添加蹲伏功能

UE4 在游戏运行时更改变量 (通过鼠标滑轮来更改第一人称角色的最大行走速度)

21 Days Learning Challenge (2) Use of Graphical Device Trees

Walter talked little knowledge | "remote passthrough" that something

Kubernetes 网络入门
随机推荐
Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals
从企业的视角来看,数据中台到底意味着什么?
How do newcomers get started and learn software testing?
Call Alibaba Cloud oss and sms services
ffmpeg -sources分析
结构体初解
七夕节代码表白
Walter talked little knowledge | "remote passthrough" that something
Android 面试题——如何徒手写一个非阻塞线程安全队列 ConcurrentLinkedQueue?
Burp installation and proxy settings
数组常用方法总结
Spark Basics [Introduction, Getting Started with WordCount Cases]
高项 02 信息系统项目管理基础
Acid (ACID) Base (BASE) Principles for Database Design
UE4 更改组件变量 (以修改第一人称角色模板的最大行走速度和跳跃高度为例)
36-Jenkins-Job迁移
Detailed and comprehensive postman interface testing practical tutorial
21 Days Learning Challenge (2) Use of Graphical Device Trees
Web3.0 Dapps - the road to the future financial world
ASP.NET application--Hello World