当前位置:网站首页>【 8.4 】 source code - [math] [calendar] [delete library 】 【 is not a simple sequence (Bonus) 】
【 8.4 】 source code - [math] [calendar] [delete library 】 【 is not a simple sequence (Bonus) 】
2022-08-05 03:59:00 【ZhgDgE】
#47. 数学
题意:给定整数 n n n ,Fat professor wants to 1 ∼ n 1∼n 1∼n 这 n n n 个数字分成两组,Each group has at least one number,And make the greatest common divisor of the sum of the two sets of numbers the largest,Please output the largest greatest common divisor.
思路:首先 1 ∼ n × ( n + 1 ) 2 1\sim \frac {n\times (n+1)} 2 1∼2n×(n+1) Any number of can be represented by these numbers,Then the problem translates to having two positive integers satisfy 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)
Let's push the formula from the definition of tossing and dividing 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] ,Then the problem turns into finding m m m 的最大因子,Radical enumeration to find the smallest prime factor.
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)) into several congruent systems,Every congruence has it C c n t 2 C_{cnt}^2 Ccnt2 个贡献.计算一下即可.
总结了一下:
有连续的 n n n 个整数,Then these integers are modulo p p p 意义下,According to the size of the congruence system, it is divided into two types:
- 有 n % p n\%p n%p group congruence,大小为 ⌈ n p ⌉ \left \lceil \frac n p \right \rceil ⌈pn⌉
- 有 p − n % p p-n\%p p−n%p group congruence,大小为 ⌊ n p ⌋ \left \lfloor \frac n p \right \rfloor ⌊pn⌋
AC代码:http://oj.daimayuan.top/submission/318549
#855. 删库
题意:
题解:(贪心/字典树) 代码源每日一题 Div1 删库
思路:字典树上 dfs + 贪心.At the beginning, I forgot about the dictionary tree.
First we build the dictionary tree.We select a string and delete the corresponding string from the set,Equivalent to cutting a subtree from the dictionary tree,Marker points on the subtree ≤ k \leq k ≤k .
那么贪心思路就是:我们 dfs After finishing the son of a node,Count how many markers there are in the subtree.如果标记点 ≤ k \leq k ≤k ,Then our greedy strategy is to backtrack directly,Do not delete at this node,Because we can combine with markers on other sibling subtrees after backtracking,deleted together,minimize the number of deletions;反之,We must delete some child subtrees at this point.We sequentially delete the subtree with the largest number of markers,Makes backtracking with fewer markers.
AC代码:http://oj.daimayuan.top/submission/318499
#883. Uncomplicated numbers(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) ,Each operation can decrement a number by one,Add one to the number adjacent to this number.Ask the least number of operands to make the sequence gcd ( a ) ≥ 2 \gcd(a)\geq 2 gcd(a)≥2 .
题解:(枚举/贪心) 代码源每日一题 Div1 Uncomplicated numbers(Bonus)
思路:First there is a theorem:序列的 gcd \gcd gcd Equal to serial difference 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 is equal to the sequence prefix sum gcd \gcd gcd .
Our operations on adjacent numbers of the sequence are equivalent to single-point addition or subtraction of the prefix and the sequence.我们枚举 s u m sum sum 的因子 x x x ,The question turns into how many times to operate so that each prefix is 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) .
The problem solution is analogized from a classic problem.
给定两个序列 a , b a,b a,b ,It is also similar to the addition and subtraction of adjacent numbers,问对 a a a At least how many times the operation makes and b b b 完全相等.
定义 s t e p i step_i stepi 表示为前 i i i The minimum number of operations to be exactly equal,那么 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 After the number operation is completed,会从 a i a_i ai Take this number here or send some numbers,We in order to make a i = b i a_i=b_i ai=bi ,Need to bring some numbers or send some numbers away a i + 1 a_{i+1} ai+1 ,The number of operations is ∣ s a i − s b i ∣ |sa_i-sb_i| ∣sai−sbi∣
边栏推荐
- 重载运算符
- How to wrap markdown - md file
- iMedicalLIS listener (2)
- Industry Status?Why do Internet companies prefer to spend 20k to recruit people rather than raise their salary to retain old employees~
- Mathematics - Properties of Summation Symbols
- [Paper Notes] MapReduce: Simplified Data Processing on Large Clusters
- pyqt5 + socket 实现客户端A经socket服务器中转后主动向客户端B发送文件
- 队列题目:最近的请求次数
- UE4 通过互动(键盘按键)开门
- iMedicalLIS监听程序(2)
猜你喜欢
36-Jenkins-Job Migration
Confessing the era of digital transformation, Speed Cloud engraves a new starting point for value
测试薪资这么高?刚毕业就20K
Hard power or soft power, which is more important to testers?
程序开发的一些常规套路(一)
[MRCTF2020]PYWebsite
用Unity发布APP到Hololens2无坑教程
UE4 在游戏运行时更改变量 (通过鼠标滑轮来更改第一人称角色的最大行走速度)
[论文笔记] MapReduce: Simplified Data Processing on Large Clusters
Web3.0 Dapps——通往未来金融世界的道路
随机推荐
[SWPU2019]Web1
Redis1:Redis介绍、Redis基本特性、关系型数据库、非关系型数据库、数据库发展阶段
bytebuffer put flip compact clear 方法演示
The most comprehensive exam questions for software testing engineers in 2022
Event parse tree Drain3 usage and explanation
token、jwt、oauth2、session解析
[极客大挑战 2019]FinalSQL
rpc-remote procedure call demo
UE4 通过互动(键盘按键)开门
[MRCTF2020]PYWebsite
YYGH-13-客服中心
银行数据采集,数据补录与指标管理3大问题如何解决?
leetcode-每日一题1403. 非递增顺序的最小子序列(贪心)
Ffmpeg - sources analysis
2022.8.4-----leetcode.1403
Swing有几种常用的事件处理方式?如何监听事件?
[TA-Frost Wolf_may-"Hundred Talents Project"] Graphics 4.3 Real-time Shadow Introduction
iMedicalLIS listener (2)
Web3.0 Dapps——通往未来金融世界的道路
Detailed and comprehensive postman interface testing practical tutorial