当前位置:网站首页>P1896 [SCOI2005] 互不侵犯(状压dp)
P1896 [SCOI2005] 互不侵犯(状压dp)
2022-07-03 07:54:00 【eva_can(not)survive】
[SCOI2005] 互不侵犯 - 洛谷https://www.luogu.com.cn/problem/P1896初学状压dp,看了第一篇题解后的感觉学到了很多,首先矩阵的状压可以以行入手,记录在当前这一行的状态,首先我们看本题思考后有3个状态,所在行,当前行状态和国王数目。所以就要思考递推方程为f[i][j][k](i为当前行,j为此行状态,k为国王数目)+=f[i-1][s][k-t](s为上一行状态,t为这一行的国王数目)
所以我们要预先处理掉一行中可能的状态以及改状态下的国王数目,所以第二维可以优化为这一状态的编号,这样也同时解决了一行中的国王数目,(真的是看完那篇题解后学到了很多,之前还傻傻的以为就数组第二维直接放状态,这样很容易炸,orz大佬)
然后就要判断转移条件
s&j时 说明上下能攻击到
s<<1&j 左下右上攻击到
j<<1&s 左上右下攻击到
然后就可以愉悦的code了
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)
#define lowbit(x) ((x)&(-x))
using P = pair<int, int>;
int sit[2000], gs[2000];
int cnt = 0;
int n, yong;
ll f[10][2000][100];
void dfs(int he, int sum, int node) {
if (node >= n) {
sit[++cnt] = he;
gs[cnt] = sum;
return;
}
dfs(he, sum, node + 1);
dfs(he + (1 << node), sum + 1, node + 2);
}
int main() {
scanf("%d %d", &n, &yong);
dfs(0, 0, 0);
for (int i = 1; i <= cnt; i++)
f[1][i][gs[i]] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= cnt; j++) {
for (int k = 1; k <= cnt; k++) {
if (sit[j]&sit[k])
continue;
if ((sit[j] << 1)&sit[k])
continue;
if (sit[j] & (sit[k] << 1))
continue;
for (int s = yong; s >= gs[j]; s--)
f[i][j][s] += f[i - 1][k][s - gs[j]];
}
}
}
ll ans = 0;
for (int i = 1; i <= cnt; i++) {
ans += f[n][i][yong];
}
printf("%lld\n", ans);
return 0;
}边栏推荐
- LwIP learning socket (API)
- UA camouflage, get and post in requests carry parameters to obtain JSON format content
- register关键字
- HarmonyOS第三次培训笔记
- Redis查看客户端连接
- Research shows that breast cancer cells are more likely to enter the blood when patients sleep
- What to do after the browser enters the URL
- 什么是数据类型?数据类型有什么用?
- idea取消引用显示效果
- Pat class a 1028 list sorting
猜你喜欢

PAT甲级 1031 Hello World for U

Go language foundation ----- 15 ----- reflection

研究显示乳腺癌细胞更容易在患者睡觉时进入血液

什么是数据类型?数据类型有什么用?

密西根大学张阳教授受聘中国上海交通大学客座教授(图)

一个实习生的CnosDB之旅

HarmonyOS第三次培训笔记

Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does

UA camouflage, get and post in requests carry parameters to obtain JSON format content

C language learning notes (mind map)
随机推荐
【LeetCode】3. Merge Two Sorted Lists·合并两个有序链表
Go language foundation ----- 06 ----- anonymous fields, fields with the same name
优质博客——
【LeetCode】4. Best Time to Buy and Sell Stock·股票买卖最佳时机
研究显示乳腺癌细胞更容易在患者睡觉时进入血液
[MySQL 13] if you change your password for the first time after installing mysql, you can skip MySQL password verification to log in
Go language foundation ----- 15 ----- reflection
Pat grade a 1027 colors in Mars
PHP common sorting algorithm
MAE
idea取消引用顯示效果
What is a data type? What is the use of data types?
Pat class a 1030 travel plan
Iterm2 setting
Microsoft Security Response Center
go语言-循环语句
How does yarn link help developers debug NPM packages?
Redis view client connection
【cocos creator】获取资源uuid
Iterm2设置