来自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]的更多相关文章

  1. DDD创始人Eric Vans:要实现DDD原始意图,必须CQRS+Event Sourcing架构

    http://www.infoq.com/interviews/Technology-Influences-DDD# 要实现DDD(domain drive  design 领域驱动设计)原始意图,必 ...

  2. USACO 6.1 Postal Vans(一道神奇的dp)

    Postal Vans ACM South Pacific Region -- 2003 Tiring of their idyllic fields, the cows have moved to ...

  3. [外文理解] DDD创始人Eric Vans:要实现DDD原始意图,必须CQRS+Event Sourcing架构。

    原文:http://www.infoq.com/interviews/Technology-Influences-DDD# 要实现DDD(domain drive  design 领域驱动设计)原始意 ...

  4. USACO6.5-Closed Fences:计算几何

    Closed Fences A closed fence in the plane is a set of non-crossing, connected line segments with N c ...

  5. USACO6.4-Wisconsin Squares:搜索

    Wisconsin Squares It's spring in Wisconsin and time to move the yearling calves to the yearling past ...

  6. USACO6.4-Electric Fences:计算几何

    Electric Fences Kolstad & Schrijvers Farmer John has decided to construct electric fences. He ha ...

  7. 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 ...

  8. usaco6.1-Cow XOR:trie树

    Cow XOR Adrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All o ...

  9. codevs 3289 花匠

    题目:codevs 3289 花匠 链接:http://codevs.cn/problem/3289/ 这道题有点像最长上升序列,但这里不是上升,是最长"波浪"子序列.用动态规划可 ...

随机推荐

  1. java实现红包的分配算法

    个人推测,微信红包在发出的时候已经分配好金额.比如一个10元的红包发给甲乙丙三个人,其实在红包发出去的时候,已经确定了第一个会领取多少,第二个会领取多少金额. 而不是在领取的时候才计算的.下面贴出实现 ...

  2. Web Api HttpWebRequest 请求 Api 及 异常处理

    HttpWebRequest request = WebRequest.CreateHttp(url); request.Method = "post"; request.Head ...

  3. vue表单详解——小白速会

    一.基本用法 你可以用 v-model 指令在表单 <input> 及 <textarea> 元素上创建双向数据绑定. 但 v-model 本质上不过是语法糖.它负责监听用户的 ...

  4. Python内置函数(35)——next

    英文文档: next(iterator[, default]) Retrieve the next item from the iterator by calling its __next__() m ...

  5. Python-字符串及列表操作-Day2

    1.数据类型 1.1 变量引出数据类型 变量:用来记录状态变量值的变化就是状态的变化,程序运行的本质就是来处理一系列的变化 1.2 五大基本数据类型: 数字 字符串 列表 元组 字典 1.2.1 数字 ...

  6. GIT入门笔记(8)-- 查看历史提交记录/根据版本号回到过去或未来

    在Git中,用HEAD表示当前版本,也就是最新的提交版本, 上一个版本就是HEAD^, 上上一个版本就是HEAD^^, 往上100个版本写100个^比较容易数不过来,所以写成HEAD~100. Git ...

  7. 新概念英语(1-3)Sorry, sir

    Does the man get his umbrella back? A:My coat and my umbrella please. B:Here is my ticket. A:Thank y ...

  8. python基础——继承实现的原理

    python基础--继承实现的原理 1 继承顺序 class A(object): def test(self): print('from A') class B(A): def test(self) ...

  9. [转]linux查看日志文件内容命令

    linux查看日志文件内容命令tail.cat.tac.head.echo tail -f test.log你会看到屏幕不断有内容被打印出来. 这时候中断第一个进程Ctrl-C, ---------- ...

  10. jquery的attr()方法

    一.定义和用法 attr() 方法设置或返回被选元素的属性和值. 当该方法用于返回属性值,则返回第一个匹配元素的值. 当该方法用于设置属性值,则为匹配元素设置一个或多个属性/值对. 二.语法 返回属性 ...