状压dp
//Poj 3254
Corn Fields
Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't be planted. Canny FJ knows that the cows dislike eating close to each other, so when choosing which squares to plant, he avoids choosing squares that are adjacent; no two chosen squares share an edge. He has not yet made the final choice as to which squares to plant. Being a very open-minded man, Farmer John wants to consider all possible options for how to choose the squares for planting. He is so open-minded that he considers choosing no squares as a valid option! Please help Farmer John determine the number of ways he can choose the squares to plant. Input Line 1: Two space-separated integers: M and N
Lines 2..M+1: Line i+1 describes row i of the pasture with N space-separated integers indicating whether a square is fertile (1 for fertile, 0 for infertile) Output Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.
Sample Input 2 3 1 1 1 0 1 0 Sample Output 9 Hint Number the squares as follows:
1 2 3 There are four ways to plant only on one squares (1, 2, 3, or 4), three ways to plant on two squares (13, 14, or 34), 1 way to plant on three squares (134), and one way to plant on no squares. 4+3+1+1=9. Source |
[Submit] [Go Back] [Status] [Discuss]
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <utility> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <stack> 10 using namespace std; 11 #define max(x,y) x>=y?x:y 12 #define lowbit(x) x&(-x) 13 #define ll long long 14 const int mod=1e8; 15 int m,n,cnt; 16 int r[20],dp[20][400],a[20]; 17 int state[400]; 18 bool check1(int x) 19 { 20 return !(x&(x<<1));//x的二进制有没有相邻的1 21 } 22 bool check2(int x,int k) 23 { 24 return !((x&(a[k])));//x和第k+1行的逆有没有重合 25 /* 26 如 k+1行为 0 1 0 1 27 1 0 1 0 28 x : 1 0 0 1 虽然x满足没有相邻的1,但是他的第一个1的位置为0(不能放) 29 */ 30 } 31 void init() 32 { 33 cnt=0; 34 for(int i=0;i<(1<<n);i++){ 35 if(check1(i)) state[cnt++]=i;//找出=所有的可行状态 36 } 37 } 38 int main() 39 { 40 scanf("%d%d",&m,&n); 41 init(); 42 int x; 43 for(int i=0;i<m;i++){ 44 for(int j=0;j<n;j++){ 45 scanf("%d",&x); 46 if(!x){ 47 a[i]+=(1<<(n-j-1));//逆 48 } 49 } 50 51 } 52 for(int i=0;i<cnt;i++){ 53 if(check2(state[i],0)) dp[0][i]=1;//初始化 54 } 55 for(int i=1;i<m;i++){//要从1开始 56 for(int j=0;j<cnt;j++){ 57 if(!check2(state[j],i)) continue; 58 for(int k=0;k<cnt;k++){ 59 if(!check2(state[k],i-1)) continue; 60 if(state[j]&state[k]) continue;//上下不能有相邻的1 61 dp[i][j]=(dp[i][j]+dp[i-1][k])%mod; 62 } 63 } 64 } 65 int ans=0; 66 for(int i=0;i<cnt;i++){ 67 ans=(ans+dp[m-1][i])%mod; 68 } 69 printf("%d\n",ans); 70 return 0; 71 }
ACM-ICPC 2018 南京赛区网络预赛 E. AC Challenge
Dlsj is competing in a contest with n (0 < n \le 20)n(0<n≤20) problems. And he knows the answer of all of these problems.
However, he can submit ii-th problem if and only if he has submitted (and passed, of course) s_isi problems, the p_{i, 1}pi,1-th, p_{i, 2}pi,2-th, ......, p_{i, s_i}pi,si-th problem before.(0 < p_{i, j} \le n,0 < j \le s_i,0 < i \le n)(0<pi,j≤n,0<j≤si,0<i≤n) After the submit of a problem, he has to wait for one minute, or cooling down time to submit another problem. As soon as the cooling down phase ended, he will submit his solution (and get "Accepted" of course) for the next problem he selected to solve or he will say that the contest is too easy and leave the arena.
"I wonder if I can leave the contest arena when the problems are too easy for me."
"No problem."
—— CCF NOI Problem set
If he submits and passes the ii-th problem on tt-th minute(or the tt-th problem he solve is problem ii), he can get t \times a_i + b_it×ai+bi points. (|a_i|, |b_i| \le 10^9)(∣ai∣,∣bi∣≤109).
Your task is to calculate the maximum number of points he can get in the contest.
Input
The first line of input contains an integer, nn, which is the number of problems.
Then follows nn lines, the ii-th line contains s_i + 3si+3 integers, a_i,b_i,s_i,p_1,p_2,...,p_{s_i}ai,bi,si,p1,p2,...,psias described in the description above.
Output
Output one line with one integer, the maximum number of points he can get in the contest.
Hint
In the first sample.
On the first minute, Dlsj submitted the first problem, and get 1 \times 5 + 6 = 111×5+6=11 points.
On the second minute, Dlsj submitted the second problem, and get 2 \times 4 + 5 = 132×4+5=13 points.
On the third minute, Dlsj submitted the third problem, and get 3 \times 3 + 4 = 133×3+4=13 points.
On the forth minute, Dlsj submitted the forth problem, and get 4 \times 2 + 3 = 114×2+3=11 points.
On the fifth minute, Dlsj submitted the fifth problem, and get 5 \times 1 + 2 = 75×1+2=7 points.
So he can get 11+13+13+11+7=5511+13+13+11+7=55 points in total.
In the second sample, you should note that he doesn't have to solve all the problems.
样例输入1
5 5 6 0 4 5 1 1 3 4 1 2 2 3 1 3 1 2 1 4
样例输出1
55
样例输入2
1 -100 0 0
样例输出2
0
题目来源
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <utility> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <stack> 10 using namespace std; 11 #define max(x,y) x>=y?x:y 12 #define lowbit(x) x&(-x) 13 #define ll long long 14 const ll inf=9e18; 15 #define N 50 16 #define M 1<<21 17 int n,k,pre[N]; 18 ll a[N],b[N],dp[M];//dp的大小 19 int main() 20 { 21 scanf("%d",&n); 22 for(int i=1;i<=n;i++){ 23 scanf("%lld%lld%d",&a[i],&b[i],&k); 24 int x; 25 for(int j=0;j<k;j++) 26 { 27 scanf("%d",&x); 28 pre[i]+=(1<<(x-1)); 29 //在提交第i个之前要完成的 30 //如 提交第1个之前要完成的 2 3 31 //那么 pre[1]=110 32 } 33 } 34 int cnt=(1<<n); 35 for(int i=1;i<cnt;i++ ) dp[i]=-inf; 36 dp[0]=0ll; 37 ll ans=0;//题意最小0 38 //dp[i] :按照i的方法进行可以获得的最大价值 39 for(int i=1;i<cnt;i++)//枚举所有情况 40 //0 1 1 就包含了1 2|2 1 41 { 42 for(int j=0;j<n;j++) 43 44 { 45 if(i>>j&1){ 46 47 int u=i-(1<<j);//提交第j+1个之前已经做的 48 if((u&pre[j+1])==pre[j+1]){//把提交第j+1个之前要做的都做了。要加(),==高于& 49 50 ll val=dp[u]+__builtin_popcount(i)*a[j+1]+b[j+1]; 51 dp[i]=max(dp[i],val); 52 ans=max(ans,dp[i]); 53 } 54 } 55 } 56 } 57 printf("%lld\n",ans); 58 return 0; 59 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现