时间限制:0.5s

空间限制:6M

题意:

给出长n(n<=10^100)和宽m(m<=5)的地面,铺上黑色和白色的地板,使得没有任意一个2*2大小的地面铺同种颜色的方案数是多少.


Solution:

状态压缩,一个数字的对应为01分别代表铺白色和黑色地板,对于每一列有(1<<m)种状态.

我们可以构造一个矩阵,mat[i][j]代表,第一列是状态是i,第二列是j的方案数,显然mat[i][j]不是0就是1,而且很容易判断构造.

然后我们只要对这个矩阵进行快速幂运算,幂为(n-1),当然不要忘记取模,最后把mat所有元素加起来就是我们想要的答案了.

由于n比较大,所以要做大数的减一,和除以二的运算.

code

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
struct Mat {
int mat[][];
} mx;
int pow[];
int n, m, mod, len;
Mat operator * (Mat a, Mat b) {
Mat c;
memset (c.mat, , sizeof c.mat);
for (int k = ; k < ( << m); k++)
for (int i = ; i < ( << m); i++)
for (int j = ; j < ( << m); j++)
(c.mat[i][j] += (a.mat[i][k] * b.mat[k][j]) % mod) %= mod;
return c; }
inline int div2() {
int ans[] = {};
int i, res = ;
for(i = ; i < len; ++i) {
ans[i] = (pow[i]+res*)/;
res = (pow[i]+res*)%;
}
if(ans[] == ) len--;
for(i = +(ans[] == ); i < len+(ans[] == ); i++)
pow[i-(ans[] == )] = ans[i];
return res;
}
Mat operator ^ (Mat a, int pow[]) {
Mat c;
for (int i = ; i < ( << m); i++)
for (int j = ; j < ( << m); j++)
c.mat[i][j] = (i == j);
while (len) {
if (div2() )
c = c * a;
a = a * a;
}
return c;
}
string s;
int main() {
ios::sync_with_stdio ();
while(cin >> s >> m >> mod){
for (int i = ; i < s.size(); ++i)
pow[i] = s[i] - '';
len = s.size();
for (int i = len - ; i >= ; i--) {
if (pow[i]) {
--pow[i];
break;
}
else
pow[i] = ;
}
if (pow[] == ) {
for (int i = ; i < len - ; i++) pow[i] = ;
pow[--len]=;
}
for (int i = ; i < ( << m); i++)
for (int j = ; j < ( << m); j++) {
mx.mat[i][j] = ;
for (int k = , tem = i ^ j; k < m - ; k++)
if ( (tem & << k) == && (tem & << k + ) ==
&&( ( (i & << k) > && (i & << k + ) > ) || ( ( (i & << k) == && (i & << k + ) == ) ) ) ) {
mx.mat[i][j] = ;
break;
}
}
mx = mx ^ pow;
int ans = ;
for (int i = ; i < ( << m); i++)
for (int j = ; j < ( << m); j++) {
ans += mx.mat[i][j];
while (ans >= mod) ans -= mod;
}
cout << ans << endl;
}
}

SGU 197.Nice Patterns Strike Back的更多相关文章

  1. ZOJ2317-Nice Patterns Strike Back:矩阵快速幂,高精度

    Nice Patterns Strike Back Time Limit: 20000/10000MS (Java/Others)Memory Limit: 128000/64000KB (Java/ ...

  2. zoj 2317 Nice Patterns Strike Back(矩阵乘法)

    problemId=1317">http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=1317 给出一个n*m的矩阵( ...

  3. SGU 分类

    http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...

  4. ASC #1

    开始套题训练,第一套ASC题目,记住不放过每一题,多独立思考. Problem A ZOJ 2313 Chinese Girls' Amusement 循环节 题意:给定n,为圆环长度,求k < ...

  5. sentence patterns

    第四部分     推理题 1.世界上每个角落的每个人都有立场,都有背景,都有推理性,能推理出一个人语言的真意,才成就了真正的推理能力: 2.换言之,如果你能通过一个人的说话推理出其身份职业,你的推理能 ...

  6. sgu Theodore Roosevelt【判断点是否在凸多边形内模板】

    链接: http://acm.sgu.ru/problem.php?contest=0&problem=253 http://acm.hust.edu.cn/vjudge/contest/vi ...

  7. Design Patterns Simplified - Part 3 (Simple Factory)【设计模式简述--第三部分(简单工厂)】

    原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part3-factory/ Design ...

  8. BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]

    1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1017  Solved: ...

  9. [LeetCode] Android Unlock Patterns 安卓解锁模式

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

随机推荐

  1. 在代码中创建Drawable资源

    如何在代码中创建圆环: 先看效果图 代码; import android.graphics.drawable.GradientDrawable; GradientDrawable circle = n ...

  2. Linux学习笔记12——Unix中的进程

    通过调用fork和exec函数都能创建新的进程,但两者有着本质的区别:fork函数拷贝了父进程的内存映像,而exec函数用用新的映像来覆盖调用进程的进程映像的功能. 一  fork函数 #includ ...

  3. Windbg扩展的一些参考文章

    Windbg脚本和扩展工具开篇http://www.cnblogs.com/pugang/archive/2012/11/30/2796617.html WinDbg简单扩展DLL http://ww ...

  4. Calendar - SGU 115(日期判断)

    这年的开始的第一天是星期 1 代码如下: =============================================================================== ...

  5. Conversion between json and object

    public static string ObjToJson<T>(T obj) { DataContractJsonSerializer serializer = new DataCon ...

  6. [置顶] 【VB.NET2010】在空间上显示提示气泡框的方法

    在VB6中,有ToolTip这个属性,可以设置鼠标悬浮在控件上的时候显示的图像. 而,在VB.NET中,实现这个功能需要使用一个类,ToolTip类. 代码如下(为了立即起效,建议将这些代码放在For ...

  7. delphi TColorDialog

    TColorDialog 预览          实现过程 动态创建和使用颜色对话框 function ShowColorDlg:TColor;begin  with TColorDialog.Cre ...

  8. 重复记录(duplicate records)相关运营数据

    MySQL 中查找反复数据,删除反复数据 创建表和測试数据 /* 表结构 */ DROPTABLEIFEXISTS `t1`; CREATETABLEIFNOTEXISTS `t1`( `id` IN ...

  9. SDL 实现透明悬浮窗

    最近一直想用SDL实现弹幕功能,但是一直没法实现悬浮窗和透明背景功能. 在一个老外的博客上发现了思路:EthioProgrammer: Applying transparency using win3 ...

  10. block没那么难(二):block和变量的内存管理

    本系列博文总结自<Pro Multithreading and Memory Management for iOS and OS X with ARC> 了解了 block的实现,我们接着 ...