[usaco6.1.1Postal Vans]
来自FallDream的博客,未经允许,请勿转载,谢谢。
给你一个4*n的棋盘,问从(1,1)出发恰好经过所有格子一次的走法数量.(n<=1000)
插头dp,用f[i][j][k]表示转移到第i行第j列,插头的状态是k的方案数,套上高精度。
假设要转移到格子(i,j) 前一个格子的右插头是p,上一个格子的下插头是q,
p和q都没有插头时,现在这个格子显然只能有向右和向下的插头,插头状态变成()
p和q只有一个有插头时,一个插头只能与这个插头对接,另一个插头可右可下,分别转移
pq都有插头的时候,显然只能和这两个插头对接,考虑维护联通性。
如果两个插头是(),那么会形成一个环,只有在最后一个格子才能转移。
如果两个插头是((或者)),这两个联通块被合并,把这两个插头去掉,然后把他们匹配的两个相同的插头改成()即可。
如果这两个插头是)(,那么直接去掉这两个插头即可。
插头的状态可以与处理,总共只有21种,复杂度O(4*n*21*高精度)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<iomanip>
#define MN 2200
#define mod 1000000000
using namespace std;
inline int read()
{
int x = , f = ; char ch = getchar();
while(ch < '' || ch > ''){ if(ch == '-') f = -; ch = getchar();}
while(ch >= '' && ch <= ''){x = x * + ch - '';ch = getchar();}
return x * f;
} struct Hpc
{
int len,s[];
void init(int x){memset(s,,sizeof(int)*(len+));s[]=x;len=(x>);}
void operator += (Hpc y)
{
len=max(len,y.len)+;
for(int i=;i<=len;++i)
{
s[i]+=y.s[i];
if(s[i]>=mod)
{
s[i+]+=s[i]/mod;
s[i]%=mod;
}
}
if(!s[len]) --len;
}
void print()
{
printf("%d",s[len]);
for(int i=len-;i>;--i)
cout<<setw()<<setfill('')<<s[i];
}
}f[][<<],ans;
int n,tot=,s[MN+],c[MN+][],q[],top; int main()
{
n=read();f[][].init();
for(int i=;i<<<(<<)+;++i)
{
s[++tot]=i;top=;
for(int j=;j<=;++j)
{
int x=i>>(j<<);
if((x&)==) {top=-;break;}
if((x&)==) q[++top]=j;
if((x&)==)
{
if(!top) {top=-;break;}
else c[tot][q[top]]=j,c[tot][j]=q[top],--top;
}
}
if(top) --tot;
}
for(int i=;i<=n;++i)
{
for(int j=;j<=tot;++j)
{
if(s[j]&) f[][s[j]].init();
else f[][s[j]]=f[][s[j]>>];
}
for(int j=;j<=;++j)
{
int x=(j-)<<;
memset(f[j],,sizeof(f[j]));
for(int k=;k<=tot;++k)
{ int p=(s[k]>>x)&;
int q=(s[k]>>(x+))&;
if(!p&&!q) f[j][s[k]|(<<x)]+=f[j-][s[k]];
else if(p&&q)
{
if(p==&&q==)
{
if(i==n&&j==&&s[k]==(<<x)) ans+=f[j-][s[k]];
}
else if(p==&&q==)
f[j][s[k]^(<<x)^(<<(c[k][j]<<))]+=f[j-][s[k]];
else if(p==&&q==) f[j][s[k]^(<<x)]+=f[j-][s[k]];
else if(p==&&q==) f[j][s[k]^(<<x)^(<<(c[k][j-]<<))]+=f[j-][s[k]]; }
else
{
f[j][s[k]]+=f[j-][s[k]];
f[j][(s[k]^(p<<x)^(q<<x+))|(p<<x+)|(q<<x)]+=f[j-][s[k]];
}
}
}
}
ans+=ans;ans.print();
return ;
}
[usaco6.1.1Postal Vans]的更多相关文章
- DDD创始人Eric Vans:要实现DDD原始意图,必须CQRS+Event Sourcing架构
http://www.infoq.com/interviews/Technology-Influences-DDD# 要实现DDD(domain drive design 领域驱动设计)原始意图,必 ...
- USACO 6.1 Postal Vans(一道神奇的dp)
Postal Vans ACM South Pacific Region -- 2003 Tiring of their idyllic fields, the cows have moved to ...
- [外文理解] DDD创始人Eric Vans:要实现DDD原始意图,必须CQRS+Event Sourcing架构。
原文:http://www.infoq.com/interviews/Technology-Influences-DDD# 要实现DDD(domain drive design 领域驱动设计)原始意 ...
- USACO6.5-Closed Fences:计算几何
Closed Fences A closed fence in the plane is a set of non-crossing, connected line segments with N c ...
- USACO6.4-Wisconsin Squares:搜索
Wisconsin Squares It's spring in Wisconsin and time to move the yearling calves to the yearling past ...
- USACO6.4-Electric Fences:计算几何
Electric Fences Kolstad & Schrijvers Farmer John has decided to construct electric fences. He ha ...
- USACO6.4-The Primes
The Primes IOI'94 In the square below, each row, each column and the two diagonals can be read as a ...
- usaco6.1-Cow XOR:trie树
Cow XOR Adrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All o ...
- codevs 3289 花匠
题目:codevs 3289 花匠 链接:http://codevs.cn/problem/3289/ 这道题有点像最长上升序列,但这里不是上升,是最长"波浪"子序列.用动态规划可 ...
随机推荐
- 局域网下访问其他计算机搭建的django网页
1.修改工程目录下的setting.py 文件 ALLOWED_HOSTS = ['*'] #*表示允许访问的ip 如果是添加* 则允许所有同局域网环境的主机访问 2.在完成的django工程下运行以 ...
- 【iOS】swift-Binary operator '|' cannot be applied to two UIViewAutoresizing operands
let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568)) addSubview(view) view.autoresi ...
- Apache的配置httpd.conf文件配置
(1) 基本配置: ServerRoot "/mnt/software/apache2" #你的apache软件安装的位置.其它指定的目录如果没有指定绝对路径,则目录是相对于该目录 ...
- idea导入本地maven项目
首先把项目关闭File->Close Project 否则会将项目导入到当前项目中 回到主界面,点击Import Project 一定要选择项目的pom文件 默认设置,继续往下走 默认配置,下一 ...
- JavaScript 动态显示当前时间
代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...
- vue+mint-ui的微博页面(支持评论@添加表情等)
github地址 https://github.com/KyrieZbw/Sneakers (如果觉得不错就给个小星星) 预览地址 页面展示 技术栈 vue2 + vuex + vue-router ...
- GIT入门笔记(13)- GUI GIT
- Spring Security入门(3-9)Spring Security登录成功以后
- SendMessage 遇到的神坑
场景 两个进程A和B,需要从A中设置B中的文本框的内容 过程 x.x.x.x. 成功获取了B中的内容,惊喜,离成功更近异步 xxxx ***** ....... x.x.x.x. 大约查找了几百个网页 ...
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) A. Trip For Meal
http://codeforces.com/contest/876/problem/A 题意: 一个人一天要吃n次蜂蜜,他有3个朋友,他第一次总是在一个固定的朋友家吃蜂蜜,如果说没有吃到n次,那么他就 ...