pid=300">Kiki & Little Kiki 2

时间限制:5000 ms  |  内存限制:65535 KB
难度:4
描写叙述
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)

输入
The input contains no more than 1000 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.
输出
For each data set, output all lights' state at m seconds in one line. It only contains character '0' and '1.
例子输入
1
0101111
10
100000001
例子输出
1111000
001000010

题意:给出一些灯的初始状态(用0、1表示)。对这些灯进行m次变换。若当前灯的前一盏灯的状态为1,则调整当前灯的状态。0变为1,1变为0;否则不变。第1盏灯的前一盏灯是最后一盏灯。

问最后每盏灯的状态。

分析:通过模拟能够发现,如果有n盏灯。第i盏灯的状态为f[i],则f[i] = (f[i] + f[i-1])%2;又由于这些灯形成了环,则f[i] = (f[i] + f[(n+i-2)%n+1])%2. 这样初始状态形成一个1*n的矩阵。然后构造出例如以下n*n的矩阵:

1  1  0…… 0   0

0  1  1…… 0   0

……………………

1  0  0…… 0   1

每次乘以这个矩阵得出的结果就是下一个状态。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 105;
char state[N];
struct Matrix {
Matrix() {}
void Init(int n) {
row = n; col = n;
memset(mat, 0, sizeof(mat));
for(int i = 0; i < n; i++)
mat[i][i] = 1;
}
void Init(int m, int n) {
row = m; col = n;
memset(mat, 0, sizeof(mat));
}
int row, col;
int mat[N][N];
const Matrix &Pow(int n);
}; const Matrix &operator *(const Matrix &A, const Matrix &B) {
Matrix res;
res.Init(A.row, B.col);
for(int i = 0; i < A.row; i++) {
for(int j = 0; j < A.col; j++) {
if(A.mat[i][j]) {
for(int k = 0; k < B.col; k++) {
if(B.mat[j][k])
res.mat[i][k] = res.mat[i][k] ^ (A.mat[i][j] & B.mat[j][k]);
}
} }
}
return res;
} const Matrix &Matrix::Pow(int n) {
Matrix tmp, pre;
tmp = *this;
pre.Init(this->row);
while(n) {
if(n&1) pre = tmp * pre;
tmp = tmp * tmp;
n >>= 1;
}
return pre;
} int main() {
int m;
Matrix A, B, ans;
while(~scanf("%d", &m)) {
scanf("%s",state);
int len = strlen(state);
A.Init(1, len);
for(int i = 0; i < len; i++)
A.mat[0][i] = state[i] - '0';
B.Init(len);
for(int i = 0; i < len; i++) {
B.mat[i][i] = 1;
B.mat[i][(i+1)%len] = 1;
}
ans = A * B.Pow(m);
for(int i = 0; i < len; i++)
printf("%d", ans.mat[0][i]);
printf("\n");
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

NYOJ 300 &amp;&amp; hdu 2276 Kiki &amp; Little Kiki 2 (矩阵高速功率)的更多相关文章

  1. hdu 2243 考研绝望——复杂的文字(AC自己主动机+矩阵高速功率)

    pid=2243" target="_blank" style="">题目链接:hdu 2243 考研路茫茫--单词情结 题目大意:略. 解题思 ...

  2. hdu 5318 The Goddess Of The Moon 矩阵高速幂

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5318 The Goddess Of The Moon Time Limit: 6000/3000 MS ( ...

  3. HDU 2842 Chinese Rings(矩阵高速功率+递归)

    职务地址:HDU 2842 这个游戏是一个九连环的游戏. 如果当前要卸下前n个环.由于要满足前n-2个都卸下,所以要先把前n-2个卸下.须要f(n-2)次.然后把第n个卸下须要1次,然后这时候要卸下第 ...

  4. HDU 1757 A Simple Math Problem(矩阵高速幂)

    题目地址:HDU 1757 最终会构造矩阵了.事实上也不难,仅仅怪自己笨..= =! f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 ...

  5. HDU 4896 Minimal Spanning Tree(矩阵高速功率)

    意甲冠军: 给你一幅这样子生成的图,求最小生成树的边权和. 思路:对于i >= 6的点连回去的5条边,打表知907^53 mod 2333333 = 1,所以x的循环节长度为54,所以9个点为一 ...

  6. hdu 1757 A Simple Math Problem (矩阵高速幂)

    和这一题构造的矩阵的方法同样. 须要注意的是.题目中a0~a9 与矩阵相乘的顺序. #include <iostream> #include <cstdio> #include ...

  7. hdu 3306 Another kind of Fibonacci(矩阵高速幂)

    Another kind of Fibonacci                                                        Time Limit: 3000/10 ...

  8. NYOJ 298 相变点(矩阵高速功率)

    点的变换 时间限制:2000 ms  |  内存限制:65535 KB 难度:5 描写叙述 平面上有不超过10000个点.坐标都是已知的.如今可能对全部的点做下面几种操作: 平移一定距离(M),相对X ...

  9. HDU - 2294 Pendant (DP滚动数组降维+矩阵高速功率)

    Description On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend ...

随机推荐

  1. Linux Shell脚本编程--curl命令详解

    用途说明 curl命令是一个功能强大的网络工具,它能够通过http.ftp等方式下载文件,也能够上传文件.其实curl远不止前面所说的那些功能,大家可以通过man curl阅读手册页获取更多的信息.类 ...

  2. hdu2063+hdu1083(最大匹配数)

    传送门:hdu2063过山车 #include <cstdio> #include <cstring> #include <string> #include < ...

  3. Windows内核

    每天我们都在使用Windows系统学习.编程.听音乐.玩游戏,Windows的操作想来是非常熟练了,但是你又对Windows究竟了解多少呢?本系列的目的,就是让你对Windows系统有个更直观.更清楚 ...

  4. EXPORT_SYMBOL解析

    一般我们编写C程序时,要调用某个文件中的函数,需要在本文件中包含声明有被调用函数的头文件,然后编译连接后,方能找到调用函数.对于模块依赖的情况,不能简单的使用上面的方法,内核提供了一个机制,就是EXP ...

  5. SE 2014年5月27日

    R1模拟总部,R2 与R3模拟分部 如图配置 要求使用 GRE over IPSec VPN 主模式,启用动态路由协议rip使得总部与两分部内网可相互通讯,但要求分部用户数据流不允许互通! 步骤: 1 ...

  6. 内存分析工具 MAT 的使用

    1 内存泄漏的排查方法 Dalvik Debug Monitor Server (DDMS) 是 ADT插件的一部分,当中有两项功能可用于内存检查 : ·    heap 查看堆的分配情况 ·     ...

  7. hotmail邮箱pop3server设置方法

    hotmail邮箱 的POP3/SMTP功能仅仅向Hotmail Plus的用户开放,普通用户想要使用这一功能的话,得进行一些特别的设置.如今这一功能总算面向全部的用户开放了,虽然微软官方还没宣布这一 ...

  8. 代码重构 & 代码中的坏味道

    1.重构 1.1 为什么要重构 1.1.1 改进程序设计 程序员为了快速完成任务,在没有完全理解整体架构之前就开始写代码, 导致程序逐渐失去自己的结构.重构则帮助重新组织代码,重新清晰的体现 程序结构 ...

  9. There is no Action mapped for namespace / and action name UserAction

    果断收藏了,说的非常具体.刚開始学习的人常常遇到的问题. There is no Action mapped for namespace / and action name UserAction 在网 ...

  10. Python学习入门基础教程(learning Python)--3.3.1 Python下的布尔表达式

    简单的说就是if要判断condition是真是假,Python和C语言一样非0即真,所以如果if的condition是布尔表达式我们可以用True或者非0数(不可是浮点数)表示真,用False或者0表 ...