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. QQ第三方登录逻辑(微信,微博等同)

    实现过程:生成qq扫码登录连接(需要注册,链接里有几个参数需要按照开发文档的格式进行拼接,要后端完成),点击QQ登录按钮,前端Vue发送axios请求,后端收到请求把生成的QQ登录链接发送给vue,v ...

  2. Python之assert断言语句

    关键字assert构成断言语句,主要是可以在我们书写一个新的程序时,可以使用它帮我们锁定bug范围. 表达式: assert 表达式 ‘窗口提示的信息’ 括号中的项目为选填项目,选填项目将会在表达式的 ...

  3. wpf界面按钮自动点击

    Button Button = new Button();Button.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));//在按钮生成时便会自动触 ...

  4. AppBoxFuture: 123挨个站-数据按序存储

      最近几天在优化存储的编码规则,顺带把之前设计了但未实现的倒排序一并实现了.由于所有数据(元数据.实体.索引等)都映射至RocksDB的Key-Value存储,所以必须扩展RocksDB的自定义比较 ...

  5. mcrp 对接软件换

     如何配置UniMRCP Server的启动选项 UniMRCP Server的配置参数,比如:ASR server IP 地址.输出目录. 在哪儿设置这些自定义参数,在插件中如何获取这些参数. 修改 ...

  6. 【JDK】JDK源码分析-CyclicBarrier

    概述 CyclicBarrier 是并发包中的一个工具类,它的典型应用场景为:几个线程执行完任务后,执行另一个线程(回调函数,可选),然后继续下一轮,如此往复. 打个通俗的比方,可以把 CyclicB ...

  7. 什么是HTML,HTML的简介,HTML结构

    html:超文本标记语言(Hyper Text Markup Language) ==============基本结构================= <html><!--最外层为 ...

  8. 有一个时间插件引发的关于 newDate().setMonth() 的问题

    项目中遇到一个时间插件的BUG,查看源码之后发现是因为setMonth()的问题,使用了之后会某些月份会出现月份加一的问题, 查阅资料后发现  setMonth()其实是设置与当前时间天数相同的月份, ...

  9. Jenkins持续集成项目搭建——基于Python Selenium自动化测试

    参考链接:https://www.liaoxuefeng.com/article/1083282007018592 第一步:去官网https://jenkins.io/下载最新的war包 第二步:安装 ...

  10. Docker系列之.NET Core入门(三)

    前言 在Docker生态系统中除了上一节所讲解的基本概念,还有其他专业术语,本文我们将一笔带过,同时会开始陆续进入到在.NET Core中使用Docker. 专业术语 Docker Engine(Do ...