[學習筆記]狀壓dp

狀壓 \(dp\)


1、[SDOI2009]Bill的挑戰

\(f[i][j]\) 表示匹配到字符串的第 \(i\) 位狀態為 \(j\) 的方案數

那么方程就很明顯了,每次枚舉第 \(i\) 位的字母 \(alpha\) 然后 \(O(n)\) 判斷就好了

時間復雜度 \(O(26Tlen2^nn)\)

\(Code\ Below:\)

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int p=1e6+3;
int n,k,len,H[20],lg[1<<15],f[51][1<<15];
char s[16][51];inline int read(){register int x=0,f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}return (f==1)?x:-x;
}int main()
{register int T=read(),i,j,k,t,now,l,ans;H[0]=1;for(i=1;i<32768;i++) lg[i]=lg[i>>1]+(i&1);for(i=1;i<20;i++) H[i]=H[i-1]<<1;while(T--){n=read(),k=read();for(i=1;i<=n;i++) scanf("%s",s[i]+1);if(n<k){printf("0\n");continue;}len=strlen(s[1]+1);memset(f,0,sizeof(f));f[0][H[n]-1]=1;for(i=1;i<=len;i++)for(j=0;j<H[n];j++)if(f[i-1][j]&&lg[j]>=k){for(t=0;t<26;t++){now=0;for(l=1;l<=n;l++)if((j&H[l-1])&&(s[l][i]=='?'||s[l][i]==t+'a')) now|=H[l-1];f[i][now]=(f[i][now]+f[i-1][j])%p;}}ans=0;for(int i=0;i<H[n];i++) if(lg[i]==k) ans=(ans+f[len][i])%p;printf("%d\n",ans);}return 0;
}

2、[SDOI2009]學校食堂

狀壓 \(dp\) 好題!

首先 \(a\ or\ b - a\ and\ b = a\ xor\ b\)

\(f[i][j][k]\) 表示到第 \(i\) 個人狀態為 \(j\) 最后一個打飯的編號為 \(i+k\) 的方案數

那么就可以轉移了

if(j&1) chkmin(f[i+1][j>>1][k+7],f[i][j][k+8]);
else {int lim=inf;for(int l=0;l<=7;l++){if(!(j&(1<<l))){if(i+l>lim) break;chkmin(lim,i+l+B[i+l]);chkmin(f[i][j|(1<<l)][l+8],f[i][j][k+8]+(i+k?T[i+k]^T[i+l]:0));}}
}

\(Code\ Below:\)

#include <bits/stdc++.h>
using namespace std;
const int maxn=1000+10;
const int inf=0x3f3f3f3f;
int n,T[maxn],B[maxn],f[maxn][1<<8][16];inline int read(){register int x=0,f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}return (f==1)?x:-x;
}inline void chkmin(int &a,int b){a=a<b?a:b;}int main()
{int Case=read();while(Case--){n=read();for(int i=1;i<=n;i++)T[i]=read(),B[i]=read();memset(f,inf,sizeof(f));f[1][0][7]=0;for(int i=1;i<=n;i++)for(int j=0;j<256;j++)for(int k=-8;k<=7;k++){if(j&1) chkmin(f[i+1][j>>1][k+7],f[i][j][k+8]);else {int lim=inf;for(int l=0;l<=7;l++){if(!(j&(1<<l))){if(i+l>lim) break;chkmin(lim,i+l+B[i+l]);chkmin(f[i][j|(1<<l)][l+8],f[i][j][k+8]+(i+k?T[i+k]^T[i+l]:0));}}}   }int ans=inf;for(int i=0;i<=8;i++)chkmin(ans,f[n+1][0][i]);printf("%d\n",ans);}return 0;
}

3、[CQOI2018]解鎖屏幕

\(check\) 好麻煩啊

\(Code\ Below\):

#include <bits/stdc++.h>
#define res register int
using namespace std;
const int p=1e8+7;
int n,x[20],y[20],H[20],a[20][20],dp[1<<20][20],vis[1<<20][20],ans;
int head=1,tail=0,q[(1<<20)*20*2+10];int check(int k,int f,int j){if(k==f||k==j) return 0;if(x[f]==x[k]||x[f]==x[j]){if(x[f]==x[k]&&x[f]==x[j]&&(y[f]<=y[k])==(y[k]<=y[j])) return 1;if(y[f]==y[k]&&y[f]==y[j]&&(x[f]<=x[k])==(x[k]<=x[j])) return 1;return 0;}if((x[f]<=x[k])==(x[k]<=x[j])&&(y[f]<=y[k])==(y[k]<=y[j])&&(y[f]-y[k])*(x[f]-x[j])==(y[f]-y[j])*(x[f]-x[k])) return 1;return 0;
}void add(res &x,const res &y){x=x+y<p?x+y:x+y-p;
}int main()
{scanf("%d",&n);res i,j,f,st;H[0]=1;for(i=1;i<=20;i++) H[i]=H[i-1]<<1;for(i=0;i<n;i++) scanf("%d%d",&x[i],&y[i]);for(f=0;f<n;f++)for(j=0;j<n;j++){if(f==j) continue;for(i=0;i<n;i++) if(check(i,f,j)) a[f][j]+=1<<i;}for(i=0;i<n;i++) dp[H[i]][i]=vis[H[i]][i]=1,q[++tail]=H[i],q[++tail]=i;while(head<=tail){i=q[head++];f=q[head++];st=__builtin_popcount(i);if(st>=4) add(ans,dp[i][f]);for(j=0;j<n;j++){if(i&H[j]||!((a[f][j]&i)==a[f][j])) continue;add(dp[i|H[j]][j],dp[i][f]);if(!vis[i|H[j]][j]) vis[i|H[j]][j]=1,q[++tail]=i|H[j],q[++tail]=j;}}printf("%d\n",ans);return 0;
}

轉載于:https://www.cnblogs.com/owencodeisking/p/9978769.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278890.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278890.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278890.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

excel導入csv文件_如何將包含以0開頭的列的CSV文件導入Excel

excel導入csv文件Microsoft Excel will automatically convert data columns into the format that it thinks is best when opening comma-separated data files. For those of us that don’t want our data changed, we can change that behavior. Microsoft Excel將在打開…

MySQL之進化篇

MySQL之實用篇 MySQL之牛刀小試 子查詢是指出現在其他SQL語句內的SELECT子句. 例如: SELECT * FROM t1 WHERE column1 (SELECT column2 FROM t2) 其中 SELECT * FRIN t1 稱為outerQuery SELECT column2 FROM t2 稱為subQuery 注意:子查詢指嵌套在查詢內部,且必須始終出現在圓括…

android 9.0新ui,SystemUI分析(Android9.0)

8種機械鍵盤軸體對比本人程序員&#xff0c;要買一個寫代碼的鍵盤&#xff0c;請問紅軸和茶軸怎么選&#xff1f;一、SystemUI組成SystemUI是Android的系統界面&#xff0c;包括狀態欄statusbar、鎖屏keyboard、任務列表recents等等&#xff0c;都繼承于SystemUI這個類&#xf…

WMI技術介紹和應用——WMI概述

https://blog.csdn.net/breaksoftware/article/details/8424317轉載于:https://www.cnblogs.com/diyunpeng/p/9982885.html

解決App啟動時白屏的問題

第一次 03-25 11:02:34.431 6908-6908/com.newenergyjinfu.jytz D/App: before_onCreate: 239 03-25 11:02:34.513 6908-6908/com.newenergyjinfu.jytz D/App: after_initOkGo( initPicasso): 316 03-25 11:02:34.570 6908-6908/com.newenergyjinfu.jytz D/App: after_ J…

chromebook刷機_如何為不支持Chrome操作系統的網站欺騙Chromebook用戶代理

chromebook刷機Not all browsers handle websites the same, and if they don’t support your operating system or browser, you could be denied access. Luckily, you can spoof the user agent on Chrome OS to make it look like you use a completely different system.…

什么時候可以升級HarmonyOS,華為鴻蒙OS即將迎來升級 手機版本或仍需時間

原標題&#xff1a;華為鴻蒙OS即將迎來升級 手機版本或仍需時間在2019年的華為開發者大會上&#xff0c;華為消費者業務CEO余承東正式對外發布了HarmonyOS。時隔一年后&#xff0c;華為開發者大會2020即將拉開帷幕。此次大會&#xff0c;HarmonyOS無疑仍會是重頭戲之一&#xf…

Shell_mysql命令以及將數據導入Mysql數據庫

連接MYSQL數據庫 mysql -h${db_ip} -u${db_user} -p${db_pawd} -P${db_port} -D${db_name} -s -e "${sql}" db_ip&#xff1a;主機地址 db_user &#xff1a;數據庫用戶名 db_pwd&#xff1a;密碼 db_port&#xff1a;端口號 db_name&#xff1a;數據庫名稱 sql&…

cocos android-1,cocos2dx在windows下開發,編譯到android上(1)

轉自&#xff1a;http://www.2cto.com/kf/201205/130697.html下面我給大家介紹下&#xff0c;用vs2010開發cocos2dx&#xff0c;然后如何使其編譯到android上。步驟如下&#xff1a;1、必要條件&#xff0c;你的eclipse能把代碼編譯到安卓手機或虛擬機上&#xff0c;如果這一步…

中藥ppi網絡圖太雜亂_太雜亂了嗎? 這是您的iPhone,iPad,Android或臺式機的15張簡約壁紙...

中藥ppi網絡圖太雜亂Busy wallpaper images don’t work very well on your iPhone, iPad, or any device where you need to have lots of icons on the screen. Here’s a set of minimalistic wallpaper images that won’t clutter up your desktop. 繁忙的墻紙圖像在iPhon…

算法61---兩個字符串的最小ASCII刪除和【動態規劃】

一、題目&#xff1a; 給定兩個字符串s1, s2&#xff0c;找到使兩個字符串相等所需刪除字符的ASCII值的最小和。 示例 1: 輸入: s1 "sea", s2 "eat" 輸出: 231 解釋: 在 "sea" 中刪除 "s" 并將 "s" 的值(115)加入總和。 在…

android設置時間widget,【Android】時間與日期Widget(DatePicker 與 TimePicker)

public class Activity01 extends Activity{TextViewm_TextView;//聲明dataPickerDatePickerm_DatePicker;//聲明TimePickerTimePickerm_TimePicker;Button m_dpButton;Button m_tpButton;//java中的Calendar類Calendar c;/** Called when the activity is first created. */Ov…

初學者java學習計劃_初學者:計劃在Windows 7 Media Center中錄制直播電視的時間

初學者java學習計劃If you’re a new user to Windows 7 Media Center you know it can act as a DVR and pause or record Live TV. You can set up a schedule for it to record your favorite TV programs as well. 如果您是Windows 7 Media Center的新用戶&#xff0c;則知…

雙數據源配置

從此抄錄&#xff1a;https://blog.csdn.net/ll535299/article/details/78203634 1、先配置兩個數據源&#xff0c;附上主要代碼&#xff0c;給自己回憶&#xff0c;詳解見開頭鏈接 <!-- 配置數據源 --> <bean id"szDS" class"com.alibaba.druid.pool.…

如何在Office 2007中查看關于對話框和版本信息

One of our favorite readers wrote in today asking how to tell if his Word 2007 installation was running Service Pack 1, since he couldn’t find the About dialog, which got me thinking… I bet most people don’t know where it is! 我們最喜歡的一位讀者今天寫信…

windows全局熱鍵_在Windows中創建快捷方式或熱鍵以清除剪貼板

windows全局熱鍵Have you ever copied something to the clipboard that you don’t want to leave there in case somebody else is going to use your computer? Sure, you can copy something else to the clipboard real quick, but can’t you just make a shortcut or h…

android+notepad教程,Android Sample學習——NotePad

android.view.Menu專場Interface for managing the items in a menu.By default, every Activity supports an options menu of actions or options. You can add items to this menu and handle clicks on your additions. The easiest way of adding menu items is inflating…

Windows應用程序開發

Windows窗體應用程序開發&#xff1a;WinForm、桌面應用程序&#xff0c;有可執行文件(.exe)即安裝包。是一種C/S&#xff08;客戶機/服務器&#xff09;架構應用程序 1.Windows窗體應用程序&#xff0c;用可視化的窗體和控件生成豐富界面的&#xff0c;可交互操作的應用程序。…

獲取outlook 會議_如何僅在Microsoft Outlook中僅獲取您關注的電子郵件的通知

獲取outlook 會議Some emails are more important than others. Rather than getting alerts every time an email arrives, configure Microsoft Outlook to only alert you when the important stuff hits your inbox, rather than any old email that can wait until you ch…

jq html 多一個引號,為什么jQuery模板會為某些字符串添加雙引號

背景我正在使用jQuery模板,ASP.Net MVC Razor視圖和Twitter.問題使用帶有一些字符串的jQuery模板會自動導致這些字符串被包含在“細節我創建了一個如下所示的jQuery模板&#xff1a;before ${text.parseUserName().parseHashTag()} after${created_at}${prettyDate(created_at)…