当前位置:网站首页>[7.16] code source - [array division] [disassembly] [select 2] [maximum common divisor]
[7.16] code source - [array division] [disassembly] [select 2] [maximum common divisor]
2022-07-23 15:38:00 【ZhgDgE】
#665. Array partition
The question : Given n ( n ≤ 100 ) n(n\leq 100) n(n≤100) It's an integer , Divide it into just k ( k ≤ 100 ) k(k\leq 100) k(k≤100) Subarray , Find the maximum value of sum operation after summing each sub array .
Answer key :( Dynamic programming ) Code source daily question Div1 Array partition
Ideas : At first I defined d p ( i , j ) dp(i,j) dp(i,j) For the former i i i Each element is divided into j j j The maximum value of the block , The transfer equation is d p ( i , j ) = max ( d p ( k , j − 1 ) & ( s u m ( k + 1 , i ) ) ) dp(i,j)=\max(dp(k,j-1)\&(sum(k+1,i))) dp(i,j)=max(dp(k,j−1)&(sum(k+1,i))) , But that doesn't work . Tell me in detail why not .
First , Each state is essentially a set , Each element in the set corresponds to a weight , According to the topic, we usually require the upper bound or lower bound of the weight of the set ( That is what is usually said max − min \max-\min max−min ), That is, the attributes of the set . If we adopt DP Find this attribute recursively , We must ensure that the attributes of the subsequent state and the attributes of the precursor state are monotonous . How do you understand that ?
We define an independent variable x x x , Its definition domain is the weight value domain of the precursor node ( Of course, there are upper and lower bounds ). According to the recursive equation , Weight value range of subsequent nodes y = f ( x ) y=f(x) y=f(x) . Only when f ( x ) f(x) f(x) It is incremental within the definition field , Only by maintaining the upper bound of the precursor ( Lower bound ), To get the upper bound of the subsequent range ( Lower bound ). If monotonic increase is not satisfied , Then the extreme value of the subsequent state does not necessarily lie on the upper bound of the precursor state ( Lower bound ) obtain . This recursive monotone property of dynamic programming is also important .
Back to the question , Because it's a bit operation , Increasing the value of the predecessor node does not necessarily increase the value of the successor node , Then this method will not work .
The solution to this problem is : Enumerate the binary bits of the answer from the high order , Then judge whether the sequence is divided k The scheme of parts makes the sum greater than or equal to ans, Then decide whether to keep the bit 1 The decision .DP Existence , y ≥ x y\geq x y≥x If and only if ( y & x ) = = x (y\&x)==x (y&x)==x , If there is a segmentation scheme , Every paragraph must meet s u m i & x = = x sum_i\&x==x sumi&x==x , Definition d p ( i , j ) dp(i,j) dp(i,j) Before presentation i Divided into k Whether there is a scheme to make the sum greater than or equal to x,DP The existence of transfer .
AC Code :http://oj.daimayuan.top/submission/290489
#611. Dismantle
The question : Multiple sets of data . Each group is given two numbers X,Y, Ask how many lengths are Y The product of the integer sequence of is X. T ≤ 1 0 5 , X , Y ≤ 1 0 6 T\leq 10^5,X,Y\leq 10^6 T≤105,X,Y≤106 .
Ideas : Split the prime factor and allocate it .
Optimization of the method of splitting qualitative factors ( O ( a ∼ b ) O(a\sim b) O(a∼b) Indicates that the pretreatment time is a a a, The query time is b b b):
- Preprocess to get all primes , Then use only prime numbers to sift , Time complexity O ( n ∼ n ln n ) O(n\sim\frac{\sqrt n}{\ln{\sqrt n}}) O(n∼lnnn)
- Pretreat all numbers with an Ehrlich sieve , Get the prime factor of each number ( The series is log n \log n logn), The time complexity is O ( n × log n ∼ log n ) O(n\times \log n\sim \log n) O(n×logn∼logn)
This problem uses the second optimization . We store all the prime factor preprocessing of a number , Then use it directly .
vector<int> primes[M];
for(int i = 2; i < M; i ++ ){
if(primes[i].size()) continue;
for(int j = i; j < M; j += i){
int t = j, cnt = 0;
while(t % i == 0) cnt ++ , t /= i;
primes[j].push_back(cnt);
}
}
AC Code :http://oj.daimayuan.top/submission/291303
#618. Selection 2
The question :

Answer key :( greedy ) Code source daily question Div1 Selection 2
Ideas : First of all find out m m m . If there is one, the length is m m m The subsequence of satisfies the meaning of the question , Then there must be a length of m m m The subinterval of satisfies the meaning of the question , Because we fix the right endpoint , Move other numbers to the right , The average will only get bigger . In this way, we can find m m m .
Find out m m m after , For each substring that meets the meaning of the topic , Only move the left endpoint to the left , This ensures that the left endpoint moves as far to the left as possible . Still two points .
AC Code :http://oj.daimayuan.top/submission/290739
#131. greatest common divisor
The question : You have a ring , It's on the ring n n n A positive integer . You can cut the ring into sections , Each paragraph contains one or more numbers .
For a segmentation scheme , The degree of beauty is the maximum common divisor of each number and , You want to maximize the beauty of the segmentation program , about k = 1 , 2 , ⋯ , n k=1,2,\cdots,n k=1,2,⋯,n Output the answer .
Answer key :( mathematics / enumeration / Problems on the ring ) Code source daily question Div1 greatest common divisor
Ideas : We divide the rings into k k k Share , that g c d ∣ s i , i ∈ [ 1 , k ] gcd|s_i,i\in [1,k] gcd∣si,i∈[1,k] , namely g c d ∣ ∑ i = 1 k s i = s u m gcd|\sum_{i=1}^k{s_i}=sum gcd∣∑i=1ksi=sum . Let's enumerate s u m sum sum The factor of is g c d gcd gcd . The order of magnitude of the factor can be considered as expectation log n \log n logn , The worst n \sqrt n n Of .
about g c d gcd gcd , We can find out how many segments can be divided at most , Because segments can be merged , The suffix and the maximum are enough . If it is a normal sequence , Then directly find how many numbers in the prefix sum are m o d g c d \bmod gcd modgcd In the sense of 0 0 0 . If it's a ring , We ask how many numbers are in the prefix and m o d g c d \bmod gcd modgcd In the sense of equal .
边栏推荐
- Blazor quickly realizes Minesweeper
- Clickhouse, let the query fly!!!
- 800V高压快充落地进程加快均胜电子获5亿欧元项目定点
- Redis的过期策略以及内存淘汰机制,Key过期了为什么没释放内存
- VSCode 更新後與tab相關快捷鍵無法使用
- 种种迹象表明,Apple将有望支持AV1
- Deep understanding of L1 and L2 regularization
- Find the source code of the thesis
- Simulation of synchronization performance of BOC modulation and demodulation based on MATLAB, output tracking curve and identification curve under different lead lag code distance
- 什么是真正的 HTAP ?(二)挑战篇
猜你喜欢
[email protected]]#颜色"/>修改ssh命令行[[email protected]]#颜色

The official redis visualization tool is coming. The needle doesn't poke

Jsd-2204 session management filter day19

C语言经典例题-两个分数相加

Linux: analysis of the basic use of vim editor

Skills to learn before going to primary school
![[ctfhub] the data of JWT header and payload are transmitted in clear text. If sensitive information is contained in it, sensitive information will be leaked. Try to find the flag. Format is flag{}](/img/d0/133d628a304f5c6b5f0d514c9fe222.jpg)
[ctfhub] the data of JWT header and payload are transmitted in clear text. If sensitive information is contained in it, sensitive information will be leaked. Try to find the flag. Format is flag{}

Batch deletion with RPM -e --nodeps

颜值爆表 Redis官方可视化工具来啦,针不戳

Guangzhou held a competition for quality and safety supervisors of agricultural products in the town and street
随机推荐
Redis bloom filter
C# 计算某个字符在字符串中出现的次数
Unreal中通过FMonitoredProcess启动其他独立程序
Can multithreading optimize program performance?
RTA is a new way to accurately launch advertisements?
OPNsense – 多功能高可靠易使用的防火墙(二)
C语言经典例题-逆序打印输入的两位数
C语言书籍推荐
Idea starts multiple projects at once
What is the difference between server hosting and virtual host
bgp选路原则
(Zset)Redis底层是如何用跳表进行存储的
深入理解CAS (自旋锁)
查找论文源代码
Redis布隆过滤器
Skills to learn before going to primary school
VSCode 更新后与tab相关快捷键无法使用
STL map operation
Matlab simulation of solving multi-objective optimal value based on PSO optimization
Simulink simulation of ESP three-phase SVPWM controller