Sum over Subsets DP 入门
source link: https://blog.woshiluo.com/2073.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Sum over Subsets DP 入门
为什么没在标题里写 SOS DP ?因为这 SOS 这个名字我第一次看的时候怎么看怎么觉得不正经,所以写全称让他看着正经一些。
个人感觉这个东西更像是一个套路而不是 DP 类型。
1 什么是 SOS DP
Sum over Subsets DP,即求子集和的 DP。更具体的说,用于在 O(k⋅2k) 的时间求出以下式子:
fmask=∑i⊆maskai
一种非常朴素的方式是直接对于每个子集枚举其子集,复杂度是 O(4n)。这个复杂度显然是无法接受的。
定义 S(k,i)={x|x⊆k∧k⊕x<2i+1}。
S(k,i)={S(k,i−1)k&2i=0S(k,i−1)∪S(k⊕2i,i–1)otherwise
注意到现在已经可以递推 S 来得到我们想要的结果了。
但是更广为人知的是这个滚动数组的写法:
for( int st = 0; st < ( 1 << K ); st ++ )
f[i] = a[i];
for( int j = 0; j < K; j ++ ) {
for( int st = 0; st < ( 1 << K ); st ++ ) {
if( st & ( 1 << j ) )
f[i] += f[ i ^ ( 1 << j ) ];
}
}
class
local_offer
Recommend
-
21
README.md subfont
-
6
04 Jul 2020, 14:53 Like many ARM CPUs, the one in the Pinebook Pro has a “big.LITTLE” architecture, where some cores are more pow...
-
5
Partition an array of non-negative integers into two subsets such that average of both the subsets is equal Related Articles ...
-
10
Sum of subsets of all the subsets of an array Related Articles ...
-
9
Number of subsets with a given OR valueNumber of subsets with a given OR value Given an array arr[] of length N, the ta...
-
8
Sum of subsets of all the subsets of an array Related Articles ...
-
7
Given an array arr[] of length N and an integer X, the task is to find the number of subsets with sum equal to X. Examples: Input: arr[] = {1, 2, 3, 3},...
-
4
“Could you give us a checklist for accessibility, please”, is a frequently asked question to accessibility consultants. Checklists, while convenient, reduce WCAG to a subset. To maximise accessibility, we likely need a superset. Th...
-
3
In this coding problem, we need to find the power-set of given input without duplicates. Introduction In this article, we discuss the subsets of a given input. This is one of the most popular questions asked in codin...
-
6
vinayakawanti's blog Minimum n...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK