Kiki & Little Kiki 2

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3078    Accepted Submission(s): 1642

Problem Description
There are n lights in a circle numbered from 1 to n. The left of light 1 is light n, and the left of light k (1< k<= n) is the light k-1.At time of 0, some of them turn on, and others turn off. 
Change the state of light i (if it's on, turn off it; if it is not on, turn on it) at t+1 second (t >= 0), if the left of light i is on !!! Given the initiation state, please find all lights’ state after M second. (2<= n <= 100, 1<= M<= 10^8)

 
Input
The input contains one or more data sets. The first line of each data set is an integer m indicate the time, the second line will be a string T, only contains '0' and '1' , and its length n will not exceed 100. It means all lights in the circle from 1 to n.
If the ith character of T is '1', it means the light i is on, otherwise the light is off.

 
Output
For each data set, output all lights' state at m seconds in one line. It only contains character '0' and '1.
 
Sample Input
1
0101111
10
100000001
 
Sample Output
1111000
001000010
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1757 1588 2604 2256 2294 
 
题意:给你n个灯泡,每个灯泡根据其左边的灯泡的开关(1代表开,0代表关)来决定自身是否要发生改变,开就发生改变,关则不变,1号灯泡的左边是n号灯泡,问经过m秒后n个灯泡是开还是关
 
分析:因为m可以很大,所以这里得用到矩阵快速幂做题。
这是另外一种形式的快速幂。首先当然是先找递推式:f(n) = ( f(n-1) + f(n) ) % 2,得到1代表开,0代表关
然后我们就可以写出矩阵式,不难得到:
| f(1)  f(2)  ...  f(n) |     | f(1)  f(2)  ...  f(n) |
| 0       0    ...    0  |  x  | 0       0    ...    0  |
| ...     ...    ...   ...  |     | ...     ...    ...   ...  |
| 0       0    ...    0  |     | 0       0    ...    0  |
然后根据矩阵式套矩阵快速幂模板就行
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 110; //注意空间的大小,开多了会炸程序,尤其在程序中有多个矩阵的时候,最好开到刚刚符合题目要求
const int mod = 2;
typedef long long ll;
struct matrix {
ll a[maxn][maxn];
};
matrix base, ans;
ll n;
string s;
matrix multip( matrix x, matrix y ) {
matrix tmp;
for( ll i = 0; i < s.length(); i ++ ) {
for( ll j = 0; j < s.length(); j ++ ) {
tmp.a[i][j] = 0;
for( ll k = 0; k < s.length(); k ++ ) {
tmp.a[i][j] = ( tmp.a[i][j] + x.a[i][k] * y.a[k][j] + mod ) % mod;
}
}
}
return tmp;
}
void f( ll x ) {
while( x ) {
if( x&1 ) {
ans = multip( ans, base );
}
base = multip( base, base );
x /= 2;
}
}
int main() {
while( cin >> n >> s ) {
memset( ans.a, 0, sizeof(ans.a) );
memset( base.a, 0, sizeof(base.a) );
for( ll i = 0; i < s.length(); i ++ ) {
ans.a[0][i] = s[i]-'0';
}
for( ll i = 0; i < s.length(); i ++ ) {
if( i != s.length()-1 ) {
base.a[i][i] = base.a[i][i+1] = 1;
} else {
base.a[i][i] = base.a[i][0] = 1;
}
}
f(n);
for( ll i = 0; i < s.length(); i ++ ) {
cout << ans.a[0][i];
}
cout << endl;
}
return 0;
}

  

HDU2276 Kiki & Little Kiki 2 矩阵快速幂的更多相关文章

  1. HDU 2276 Kiki & Little Kiki 2( 矩阵快速幂 + 循环同构矩阵 )

    蒟蒻的我还需深入学习 链接:传送门 题意:给出一个长度为 n,n 不超过100的 01 串 s ,每当一个数字左侧为 1 时( 0的左侧是 n-1 ),这个数字就会发生改变,整个串改变一次需要 1s ...

  2. 矩阵快速幂之Kiki & Little Kiki 2

    题意是:给出一串01串,每一秒,每个位置得灯会根据左边那个灯得状态进行改变,(第一个得左边为最后一个)如果左边为1,那么自己就会改变状态,左边为0则不用,问n秒改01串的状态 ///// 首先,我们发 ...

  3. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  4. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  5. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  6. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  7. HDU5950(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...

  8. 51nod 1126 矩阵快速幂 水

    有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...

  9. hdu2604(递推,矩阵快速幂)

    题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...

随机推荐

  1. input属性设置type="number"之后, 仍可输入e, E, -, + 的解决办法

    <el-input v-model="scope.row.variables.leaderbuweiscores.score" @keyup.native="cha ...

  2. maven 下载 安装 环境配置

    电脑系统:win10  64位   idea 2019    Java 1.8 1.链接地址,我一般都找官网 http://maven.apache.org/download.cgi 截图:注意mav ...

  3. 【React踩坑记四】React项目中引入并使用js-xlsx上传插件(结合antdesign的上传组件)

    最近有一个前端上传并解析excel/csv表格数据的需求. 于是在github上找到一个14K star的前端解析插件 github传送门 官方也有,奈何实在太过于浅薄.于是做了以下整理,避免道友们少 ...

  4. 数据结构之队列java版

    //java由于泛型的擦除,用起来十分不方便 abstract class BaseQueue<T>{ abstract boolean enQueue(T x); abstract T ...

  5. gRPC的简单使用

    目录 前言 gRPC的简单介绍 基本用法 服务的定义 服务端代码编写 客户端代码编写 运行效果 服务治理(注册与发现) .NET Core 2.x 和 .NET Core 3.0的细微区别 扩展阅读 ...

  6. IPC机制1

    1.Android IPC简介 Inter-Process Communication的缩写就是IPC,含义是进程间通信或是跨进程间通信,是指两个进程进行交换数据的过程. 进程是什么? 进程在pc上就 ...

  7. 2019最新最全Java开发面试常见问题答案总结

    2019最新最全Java开发面试常见问题答案总结 马上准备9月份出去面试Java开发,自己学习丢西瓜捡芝麻,学了的都忘了,所以有机会自己做个学习笔记,摘录自各个博文以及总结. 1.JAVA面向对象的特 ...

  8. Java源码之ConcurrentHashMap

    ⑴背景 ConcurrentHashMap是线程安全高效的HashMap.而HashMap在多线程情况下强行使用HashMap的put方法可能会导致程序死循环,使CPU使用率达到100%.(http: ...

  9. 性能测试学习第二天-----loadrunner常用函数大全及设置项

    常用函数大全: 1,C语言参数转web参数 lr_save_string("aaa","param"):将字符串“aaa”或者一个字符串变量,转变成LR的参数{ ...

  10. C++通过宏定义判断操作系统及编译器

    INTRODUCTION: C++的编译环境千奇百怪,很多时候一些代码在某些编译环境下可用,一旦移到其他环境下,就会干脆Compile Error 对此,我们可以使用C++的宏定义来判断操作系统,从而 ...