来自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. 使用Google 的 gson方式解析json

    gson支持解析的类型还是比较全面的,包括JavaBean,List<JavaBean>,List<String>,Map等,使用起来也是比较方便,下面根据代码示例给出总结: ...

  2. mui对话框事件

    mui.confirm('生成成功,是否跳转到订单页面?','',['跳转','取消'],function(e){ if(e.index==0){ //点击跳转 }else if(e.index==1 ...

  3. wamp的mysql设置用户名和密码

    wamp下修改mysql root用户的登录密码 感谢作者:http://www.3lian.com/edu/2014/02-25/131010.html               1.安装好wam ...

  4. Python模块configparser(操作配置文件ini)

    configparser模块提供对ini文件的增删改查方法. ini文件的数据格式: [name1] attribute1=value1 attribute2=value2 [name2] attri ...

  5. Ubuntu16.04建立本地更新源

    公司有多台Ubuntu机器,而且不能连接互联网,导致安装软件和更新都比较麻烦,需要建立一台本地更新源服务器. 1.安装apt-mirror工具 sudo apt-get install -y apt- ...

  6. Python学习之输入输出、数据类型

    #coding=utf-8 # 输入 print'100+200=',100+200 # 输入 # name = raw_input('tell me your name:') # print'hel ...

  7. AtCoder Beginner Contest 073

    D - joisino's travel Time Limit: 2 sec / Memory Limit: 256 MB Score : 400400 points Problem Statemen ...

  8. Spark测试代码

    测试代码: import org.apache.spark.{SparkConf, SparkContext} import org.apache.spark.sql.hive.HiveContext ...

  9. RTKLIB源码解析(一)——单点定位(pntpos.c)

    RTKLIB源码解析(一)--单点定位(pntpos.c) 标签: GNSS RTKLIB 单点定位 [TOC] pntpos int pntpos (const obsd_t *obs, int n ...

  10. 逻辑运算符、三元运算符、for循环、stack(栈),heap(堆),方法区,静态域

    Lesson One 2018-04-17 19:58:39 逻辑运算符(用于逻辑运算,左右两边都是 true 或 false) 逻辑与-& 和 短路与-&& 区别: & ...