时间限制: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. 笔记-人老了-github

    其实GITHUB是很不错的,虽然之前的JD泄露那件事情,后果很严重. 但是作为个人使用很不错的. github使用入门: 1:申请一个帐号 2:github使用ssh推送的.(ssh走的是加密) 所以 ...

  2. UVa 11077 Find the Permutations(置换+递推)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35431 [思路] 置换+递推 将一个排列看作一个置换,分解为k个循 ...

  3. 折腾iPhone的生活——5s使用的各种小技巧

    电池: iPhone电池正常充电方法:随便充,想怎么充怎么充,想充就充,想停就停. 需要注意: 1.如果要闲置3个月以上,一定要冲到40%电以后再闲置 2.不要在-40~35度使用(应该是废话) 3. ...

  4. Cogs 1672. [SPOJ375 QTREE]难存的情缘 LCT,树链剖分,填坑计划

    题目:http://cojs.tk/cogs/problem/problem.php?pid=1672 1672. [SPOJ375 QTREE]难存的情缘 ★★★☆   输入文件:qtree.in  ...

  5. [IOS开发进阶与实战]第一天:CoreData的运行机制

    1.数据模型NSManagedObjectModel的建立 1.- (NSManagedObjectModel *)managedObjectModel { if (_managedObjectMod ...

  6. 遇到奇怪的C#/C/C++或者Java的bug可以去问问Coverity

    Coverity7月16号在博客Ask The Bug Guys中说以后遇到奇怪的C#/C/C++或者Java的bug可以给TheBugGuys@coverity.com发邮件.然后这些问题就会到一些 ...

  7. Adobe Dreamweaver CS6安装步骤

    dreamweaver cs6 下载地址: http://bbs.phonegap100.com/thread-135-1-1.html dreamweaver cs6 是世界顶级软件厂商adobe推 ...

  8. DateTime格式大全

    DateTime dt = DateTime.Now;//    Label1.Text = dt.ToString();//2005-11-5 13:21:25//    Label2.Text = ...

  9. [TypeScript] Understanding Generics with RxJS

    Libraries such as RxJS use generics heavily in their definition files to describe how types flow thr ...

  10. freemarker if..else.. 的使用

    FreeMarker是一款模板引擎,今天在做Pad端的时候正好用到,用法非常简单: 在xml配置页面的文件中,直接使用 <#if 1=1> //条件成立要显示的内容 </#if> ...