[CF Gym 100827F] Knights [2014-2015 ACM-ICPC Pacific Northwest Regional Contest F]
来源:互联网 发布:linux用ubuntu 编辑:程序博客网 时间:2024/05/29 03:47
题意
在M*N棋盘上放置任意数量的马,1≤M≤4,1≤N≤109
题解
矩阵快速幂。以两列(M*2)的放置情况作为状态,求出状态间转移的01矩阵,再快速幂求答案。
代码
/****************************************\* Author : ztx* Title : F - Knights* ALG :* CMT :* Time :\****************************************/#include <cstdio>#define Rep(i,l,r) for(i=(l);i<=(r);i++)#define rep(i,l,r) for(i=(l);i< (r);i++)#define Rev(i,r,l) for(i=(r);i>=(l);i--)#define rev(i,r,l) for(i=(r);i> (l);i--)typedef long long ll ;typedef double lf ;int CH , NEG ;template <typename TP>inline void read(TP& ret) { ret = NEG = 0 ; while (CH=getchar() , CH<'!') ; if (CH == '-') NEG = true , CH = getchar() ; while (ret = ret*10+CH-'0' , CH=getchar() , CH>'!') ; if (NEG) ret = -ret ;}template <typename TP>inline void readc(TP& ret) { while (ret=getchar() , ret<'!') ; while (CH=getchar() , CH>'!') ;}template <typename TP>inline void reads(TP *ret) { ret[0]=0;while (CH=getchar() , CH<'!') ; while (ret[++ret[0]]=CH,CH=getchar(),CH>'!') ; ret[ret[0]+1]=0;}#define maxs 260LL /// 2^(2*4)=256#define mod 1000000009LLstruct Mat { int n , m ; int a[maxs][maxs] ; inline void init(int i,int j) { n = i , m = j ; Rep (i,1,n) Rep (j,1,m) a[i][j] = 0 ; } Mat operator * (const Mat&B) const { Mat ret ; ret.init(n,B.m) ; int i , j , k ; Rep (i,1,n) Rep (j,1,B.m) Rep (k,1,m) ret.a[i][j] = ((ll)a[i][k]*B.a[k][j]+ret.a[i][j])%mod ; return ret ; } Mat operator ^ (const int&b) const { /// p > 0 Mat ret=*this , x=*this ; for (int p=b-1;p>0;p>>=1,x=x*x) if (p&1) ret=ret*x ; return ret ; } inline void print() { printf("<Mat %d,%d>:\n",n,m) ; int i , j ; Rep (i,1,n) { Rep (j,1,m) { printf("%d ",a[i][j]) ; } puts("") ; } }} A , B ;int n , m , sta ;inline void dfs(int a,int b,int c,int dep) { if (dep > m) { A.a[a*(1<<m)+b+1][b*(1<<m)+c+1] = 1 ; // 0->255 ---> 1->256 return ; } int aa , bb , cc , sta ; rep (sta,0,8) { aa = a<<1 , bb = b<<1 , cc = c<<1 ; if ((sta&1) && ((aa&2)||(bb&4))) continue ; if ((sta&2) && ((aa&4)||(cc&4))) continue ; if ((sta&4) && ((bb&4)||(cc&2))) continue ; if (sta&1) cc ++ ; if (sta&2) bb ++ ; if (sta&4) aa ++ ; dfs(aa,bb,cc,dep+1) ; }}int main() {int T , i ;ll ans ;// #define READ #ifdef READ freopen(".in" ,"r",stdin ) ; freopen(".out","w",stdout) ; #endif for (scanf("%d", &T) ; T -- ; ) { read(m) , read(n) ; sta = 1 << (m << 1) ; A.init(sta,sta) , B.init(1,sta) ; B.a[1][1] = 1 ; dfs(0,0,0,1) ; A = A^n ; B = B*A ; ans = 0 ; Rep (i,1,sta) ans += B.a[1][i] , ans %= mod ; printf("%I64d\n", ans) ; } #ifdef READ fclose(stdin) ; fclose(stdout) ; #else getchar() ; getchar() ; #endif return 0 ;}
0 0
- [CF Gym 100827F] Knights [2014-2015 ACM-ICPC Pacific Northwest Regional Contest F]
- [CF Gym 100827C] Containment [2014-2015 ACM-ICPC Pacific Northwest Regional Contest C]
- [CF Gym 100827E] Hill Number [2014-2015 ACM-ICPC Pacific Northwest Regional Contest E]
- [CF Gym 100827K] Towers [2014-2015 ACM-ICPC Pacific Northwest Regional Contest K]
- Codeforce Gym 100819P : 2015-2016 ACM-ICPC Pacific Northwest Regional Contest - P 仔细看题啊!
- Codeforce Gym 100819L : 2015-2016 ACM-ICPC Pacific Northwest Regional Contest - L 这是一道阅读理解(微笑)
- Codeforces GYM 100548 F - Color 2014-2015 ACM-ICPC, Asia Xian Regional Contest
- 2016-2017 ACM-ICPC Pacific Northwest Regional Contest (Div. 2)
- 2016-2017 ACM-ICPC Pacific Northwest Regional Contest
- 2015-2016 ACM-ICPC Pacific Northwest Regional Contest Div.2 全部题目题解
- 2015-2016 ACM-ICPC Pacific Northwest Regional Contest Div.2( Problem V Gears)
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest F Color
- 【2015-2016 ACM-ICPC Pacific Northwest Regional Contest (Div 1)C】【排序 模拟】Classy 课程难度排序超多关键字
- 【2015-2016 ACM-ICPC Pacific Northwest Regional Contest (Div 1)D】【水题】Triangle 两个三角形是否可以恰好拼成矩形
- 2016-2017 ACM-ICPC Pacific Northwest Regional Contest (Div. 2)【solved:12 / 13】
- 2017/9/23周测(CF2016-2017 ACM-ICPC Pacific Northwest Regional Contest (Div. 2))
- 2016-2017 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) 【solved:9 / 12】
- 【codeforces】2014-2015 ACM-ICPC, Central Europe Regional Contest (CERC 14) F Vocabulary【dp】
- 设计模式10_门面模式
- Mac 升级到El Capitan或者Yosemite出现用户级目录404 NOT FOUND的解决方案
- leetcode题解日练--2016.7.27
- Java第三天
- 剑指offer——丑数
- [CF Gym 100827F] Knights [2014-2015 ACM-ICPC Pacific Northwest Regional Contest F]
- 物联网开发的生命周期模型
- 关于新导入java项目后提示:Configure build path
- linux简单常用命令及个人总结
- 决策树
- iOS --- 自定义navigationBar的两种常见方式
- Http协议备忘录
- 目前为止,在图像缩放项目遇到的一些坑
- 【HOJ1867】经理的烦恼-区间求和+判素数