当前位置:网站首页>LeetCode 526. A graceful arrangement***
LeetCode 526. A graceful arrangement***
2022-06-09 20:33:00 【Evening rain forest bell】
Specific ideas :
DP Memory search ;
The difficulty lies in two places :
- DP Ideas ;
- How to use bits to calculate ;
Utilization bit bit To express those positions , Don't care who it is ;
enumeration 1~n position , In this case, it means to consider the i How much profit can a bit make ,dp[i][state];
among state Represents the status value ;
Enumerate all States , But notice , Because now enumerate to i position , Fill in the total i Number , therefore state Whether it is necessary to determine how many have been used , Whether it is i individual , That is, the statistics of each state bit 1 The number of ;
Check the legal status 1~n Enumeration , First of all, the numbers you fill in should meet the requirements of the topic , Secondly, for this number , It must be in state Indicates that... Has been used ;
Add it up ;
Specific code :
1. to flash back :
class Solution {
public:
int countArrangement(int n) {
vector<bool>vis(n,false);
int ret=0;
dfs(vis,n,1,ret);
return ret;
}
void dfs(vector<bool>& vis,int& n,int cnt,int& ret){
if(cnt==n+1){
ret++;
return;
}
for(int i=1;i<=n;i++){
if(vis[i])
continue;
if(i%cnt!=0&&cnt%i!=0)
continue;
vis[i]=true;
dfs(vis,n,cnt+1,ret);
vis[i]=false;
}
}
};
2. state DP:
class Solution {
public:
int countArrangement(int n) {
int mask=1<<n;
vector<vector<int>>dp(n+1,vector<int>(mask,0));
dp[0][0]=1;
for(int i=1;i<=n;i++){
// Before enumeration i A place ;
for(int j=0;j<mask;j++){
int num = __builtin_popcount(j);
if(num!=i)
continue;
for(int k=1;k<=n;k++){
if((1<<(k-1)&j)!=0&&(k%i==0||i%k==0)){
dp[i][j]+=dp[i-1][(~(1<<(k-1)))&j];
}
}
}
}
return dp[n][mask-1];
}
};
边栏推荐
- C#Random的作用
- 博文推荐|BookKeeper - Apache Pulsar 高可用 / 强一致 / 低延迟的存储实现
- Changshu science and technology applet SQL injection
- Jerry's IO_ Key how to use double keys? [chapter]
- UNION ALL UNION FULL JOIN
- C language to realize computer automatic shutdown program -- it can be used to spoof roommate's computer
- 谁是最后大赢家?华为云IoT创新应用开发大赛圆满落幕!
- 杰理之图传确定工程没有开CONFIG_NO_SDRAM_ENABLE宏,工程使用sdram【篇】
- UTM to latitude and longitude
- Knowledge points of string in C #
猜你喜欢

The processor of this virtual machine supports different functions than the processor of the virtual machine that holds the state of the virtual machine

目标分割之--Unet对多类别数据集的语义分割

Lammps中常用的势函数和晶体库资源收集

下载最新的全球疫情历史数据,下载到当前日前两天

95後大廠程序員删庫被判刑9個月

Changshu science and technology applet SQL injection

Potential functions commonly used in lammps and collection of crystal library resources

College community management system

Safety net interview (Miscellaneous)

TypeScript 变量声明
随机推荐
95后大厂程序员删库被判刑9个月
Jerry's application of camera [chapter]
FPGA入门实验-基于状态机实现多按键控制变速流水灯和跳变灯
Knowledge points of string in C #
Discussion on mobx
Exit full screen with native JS
【mysql】主从复制原理、搭建
【RK2206】4. Mqtt example
Download the latest global epidemic history data to the two days before the current day
Target Segmentation -- semantic segmentation of multi category dataset by Unet
查询sql
杰理之使用网络图传则更换lwip库和wifi库【篇】
Personal blog system (with source code)
C reverse sort
Inheritance relationship in C #
常用正则表达式
C#中委托与事件之间的一些应用
Clickhouse data insert, update and delete SQL
The HMI Software memory is abnormal, resulting in a crash exit bug
Modelarts second training notes