Problem:D - Jewelry
题意:n*m的矩阵有不超过20个的珠宝,要用最少的灯照亮珠宝,灯只能照亮上左 或 下左 或 上右 或 下右 ,且不会照在边界和障碍物上
思路:珠宝最多20个,那么这样能照珠宝的灯不会超过80,找到这些灯,然后分组背包
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
using namespace std;
typedef pair<int,int>P;
vector<int>v[85];
map<P,int>mp;
char s[25][25];
int row,col;
int dp[(1<<21)];
bool in(int x,int y){
return x>=1 && x<=row && y>=1 && y<=col && s[x][y]!='*';
}
int main(){
while(scanf("%d%d",&row,&col)!=EOF){
memset(dp,0x3f,sizeof(dp)); int inf=dp[0];
for(int i=0;i<85;i++) v[i].clear(); mp.clear();
for(int i=1;i<=row;i++) scanf("%s",s[i]+1);
int ct=0;
for(int i=1;i<=row;i++)
for(int j=1;j<=col;j++){
if(s[i][j]=='M') mp[P(i,j)]=ct++;
}
int dot=0;
for(int i=1;i<=row;i++){
for(int j=1;j<=col;j++){
if(s[i][j]=='.'){
bool ok=false;
int now=0;
if(in(i-1,j) && in(i,j-1)){
if(s[i-1][j]=='M') ok=true,now|=(1<<mp[P(i-1,j)]);
if(s[i][j-1]=='M') ok=true,now|=(1<<mp[P(i,j-1)]);
}
if(now) v[dot].push_back(now);
now=0;
if(in(i-1,j) && in(i,j+1)){
if(s[i-1][j]=='M') ok=true,now|=(1<<mp[P(i-1,j)]);
if(s[i][j+1]=='M') ok=true,now|=(1<<mp[P(i,j+1)]);
}
if(now) v[dot].push_back(now);
now=0;
if(in(i+1,j) && in(i,j-1)){
if(s[i+1][j]=='M') ok=true,now|=(1<<mp[P(i+1,j)]);
if(s[i][j-1]=='M') ok=true,now|=(1<<mp[P(i,j-1)]);
}
if(now) v[dot].push_back(now);
now=0;
if(in(i+1,j) && in(i,j+1)){
if(s[i+1][j]=='M') ok=true,now|=(1<<mp[P(i+1,j)]);
if(s[i][j+1]=='M') ok=true,now|=(1<<mp[P(i,j+1)]);
}
if(now) v[dot].push_back(now);
if(ok) dot++;
}
}
}
dp[0]=0;
for(int i=0;i<dot;i++){
for(int j=(1<<ct)-1;j>=0;j--){
for(int k=0;k<v[i].size();k++){
int now=v[i][k];
if((j&now)==now){
dp[j]=min(dp[j],dp[(j&(~now))]+1);
}
}
}
}
if(dp[(1<<ct)-1]==inf) printf("-1\n");
else printf("%d\n",dp[(1<<ct)-1]);
}
return 0;
}
Problem:G - Alchemy I
题意:用m(<10^5)种符文标记n(<50)个节点,满足P(<=15)种约束保证 p[i]与q[i]表示符文不同 的方案数
思路:容斥原理+并查集
状态枚举得到所有状态约束条件的交集,奇减偶加
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
#include <set>
using namespace std;
typedef pair<int,int>P;
typedef long long LL;
const LL mod = 1000000007;
int n,m;
int p[16],q[16];
int par[51],rank[51];
set<int>s;
void init(){
for(int i=0;i<=n;i++) par[i]=i,rank[i]=0;
}
int find(int x){
if(par[x]==x) return x;
return par[x]=find(par[x]);
}
void unite(int x,int y){
x=find(x),y=find(y);
if(x==y) return ;
if(rank[x]<rank[y]) par[x]=y;
else{
par[y]=x;
if(rank[x]==rank[y]) rank[x]++;
}
}
bool same(int x,int y){
return find(x)==find(y);
}
LL Pow(LL a,LL b){
LL ans=1;
while(b){
if(b&1) b--,ans=(ans*a)%mod;
else b/=2,a=(a*a)%mod;
}
return ans;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
int P; scanf("%d",&P);
bool exist=true;
for(int i=0;i<P;i++){
scanf("%d%d",&p[i],&q[i]);
if(p[i]==q[i]) exist=false;
}
if(!exist){
printf("0\n");
continue;
}
LL ans=0;
for(int state=0;state<(1<<P);state++){
int flag=0,tmp=state;
init();
int index=0;
while(tmp){
if(tmp&1){
unite(p[index],q[index]);
flag++;
}
tmp/=2;
index++;
}
if(flag&1) flag=-1;
else flag=1;
s.clear();
for(int i=1;i<=n;i++){
s.insert(find(i));
}
ans=(ans+flag*Pow(m,s.size()))%mod;
if(ans<0) ans+=mod;
}
printf("%lld\n",ans);
}
return 0;
}