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

题意:一排灯,给你初始状态,然后每秒都会有这种操作:假设该盏灯的左边是亮的话,就改变状态,否则不变,最左边的參考最右边的

思路:非常easy发现有:a1 = (a1+an)%2 , a2 = (a2 + a1) % 2 ......... an = (an + an-1)%2

然后构造类似矩阵: 1 0 0 1   ,位运算快的多,由于这道题的特殊性

1 1 0 0

0 1 1 0

0 0 1 1

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 105;
const int mod = 2; int cnt;
struct Matrix {
int v[maxn][maxn];
Matrix() {}
Matrix(int x) {
init();
for (int i = 0; i < maxn; i++)
v[i][i] = x;
}
void init() {
memset(v, 0, sizeof(v));
}
Matrix operator *(Matrix const &b) const {
Matrix c;
c.init();
for (int i = 0; i < cnt; i++)
for (int j = 0; j < cnt; j++)
for (int k = 0; k < cnt; k++)
c.v[i][j] ^= (v[i][k] & b.v[k][j]);
return c;
}
Matrix operator ^(int b) {
Matrix a = *this, res(1);
while (b) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
} a, b, tmp; int main() {
int t;
char str[maxn];
int num[maxn];
while (scanf("%d", &t) != EOF) {
scanf("%s", str);
cnt = strlen(str);
for (int i = 0; i < cnt; i++)
num[i] = str[i] - '0';
a.init();
a.v[0][cnt-1] = a.v[0][0] = 1;
for (int i = 1; i < cnt; i++)
a.v[i][i] = a.v[i][i-1] = 1;
tmp = a^t;
int ans[maxn];
memset(ans, 0, sizeof(ans));
for (int i = 0; i < cnt; i++)
if (num[i])
for (int j = 0; j < cnt; j++)
if (tmp.v[j][i])
ans[j] = (ans[j]+ (tmp.v[j][i]*num[i])%mod) % mod;
for (int i = 0; i < cnt; i++)
printf("%d", ans[i]);
printf("\n");
}
return 0;
}

HDU - 2276 Kiki &amp; Little Kiki 2的更多相关文章

  1. HDU 2276 Kiki & Little Kiki 2 矩阵构造

    Kiki & Little Kiki 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  2. hdu 2276 Kiki & Little Kiki 2

    点击打开hdu 2276 思路: 矩阵快速幂 分析: 1 题目给定一个01字符串然后进行m次的变换,变换的规则是:如果当前位置i的左边是1(题目说了是个圆,下标为0的左边是n-1),那么i就要改变状态 ...

  3. NYOJ 300 &amp;&amp; hdu 2276 Kiki &amp; Little Kiki 2 (矩阵高速功率)

    pid=300">Kiki & Little Kiki 2 时间限制:5000 ms  |  内存限制:65535 KB 难度:4 描写叙述 There are n light ...

  4. HDU 2276 Kiki & Little Kiki 2(矩阵位运算)

    Kiki & Little Kiki 2 转载自:点这里 [题目链接]Kiki & Little Kiki 2 [题目类型]矩阵位运算 &题意: 一排灯,开关状态已知,每过一秒 ...

  5. [HDU2276]Kiki & Little Kiki 2

    题目:Kiki & Little Kiki 2 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2276 分析: 1)如果前一盏灯亮着,则改变这一盏灯 ...

  6. HDU 2276 矩阵快速幂

    Kiki & Little Kiki 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  7. HDU2276 Kiki & Little Kiki 2 矩阵快速幂

    Kiki & Little Kiki 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  8. hdu 2276 Kiki & Little Kiki 2 矩阵快速幂

    题目链接 n个灯围成一圈, 1左边是n. 有两种状态, 1是亮, 0是不亮. 如果一个灯, 它左边的灯是亮的, 那么下一时刻这个灯就要改变状态, 1变为0, 0变为1. 给出初始状态和时间t, 问t时 ...

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

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

随机推荐

  1. JS计算两个日期相差几天

    function Computation(sDate1, sDate2){ var aDate, oDate1, oDate2, iDays aDate = sDate1.split("-& ...

  2. bzoj 1088: [SCOI2005]扫雷Mine

    题目链接 1088: [SCOI2005]扫雷Mine Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2525  Solved: 1495[Submi ...

  3. 有一个警告:Could not open/create prefs root node

    WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. 虽然程序也能正常运 ...

  4. 基于linux c的mysql操作——幼儿园数据管理系统

    上周对于mysql进行了简单的学习,利用c对mysql进行操作,主要用到了以下几个函数: mysql_init(); mysql_real_connect(数据库变量指针,网络地址,用户名,登录密码, ...

  5. 《Pointers On C》读书笔记(第四章 语句)

    1.空语句只包含一个分号,它本身并不执行任何任务,其适用的场合是语法要求出现一条完整的语句,但并不需要它执行任何任务. 2.C语言中并不存在专门的“赋值语句”,赋值就是一种操作,在表达式内进行.通过在 ...

  6. 树的判断(poj nyoj hduoj)

    题目: http://ac.jobdu.com/problem.php?pid=1481 http://acm.nyist.net/JudgeOnline/problem.php?pid=129 ht ...

  7. VS2015如何另存解决方案文件-修改解决方案sln文件的路径

    原文:VS2005如何另存解决方案文件-修改解决方案sln文件的路径 修改解决方案sln文件的路径 方法一:工具→选项→项目和解决方案,可设置项目的默认保存位置.方法二:"解决方案资源管理器 ...

  8. Openstack命令收集

    查看rabbitmq 队列 rabbitmqctl list_queues 查看keystone的用户 keystone user-list 查看keystone endpoint keystone ...

  9. ZOJ Monthly, March 2013

    A题 题目大意:给出一棵树,一开始节点值均为0,先要求完成在线操作:将某子树所有节点值取反,或者查询某子树总点权. 题解:很基础的线段树题,既然两个操作都是子树操作,那么就先树链剖分一下,将子树操作转 ...

  10. 《Linux内核设计与实现》内存管理札记

    1.页 芯作为物理页存储器管理的基本单元,MMU(内存管理单元)中的页表,从虚拟内存的角度来看,页就是最小单位. 内核用struct page结构来标识系统中的每个物理页.它的定义例如以下: flag ...