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. EditText 双击才能获取点击事件

    在获取EditText点击事件的过程中,发现EditText setOnClickListener事件响应中,只有获取焦点的时候才会响应, 如当焦点在别的控件上时,只能先点击获取焦点,第二次点击才会响 ...

  2. C# Excel或表格插件

    NOPI好像比较好用对WINFORM支持更好! http://www.cnblogs.com/dreamof/archive/2010/06/02/1750151.html NOPI教程 http:/ ...

  3. php phalcon

    1. 准备所需工具 1) php环境 浏览 2) phalcon插件 浏览 2. 安装phalcon 将php_phalcon.dll拷贝到%PHP_HOME%\ext目录中 修改php.ini文件, ...

  4. Linux 硬盘、网卡

    根据硬盘接口的不同,在Liunx中会有不同的命名 IDE 接口的硬盘会被叫成: hda,hdb,hdc (hd -- hard disk) hda 表示第一块硬盘,hdb表示第二块硬盘! 一般来说我们 ...

  5. DelphiXe5中的双向绑定(使用使用TBindScope和TBindExpression,并覆盖AfterConstruction函数)

    在Delphi下等这一功能很久了,虽然C#下早已实现了这一功能.但是在Dephi下尝试这项功能时还是有些许的激动.闲言少絮,直接上代码. unit BindingDemo; interface use ...

  6. mysql远程登录权限

    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'WITH GRANT OPTION; FLUSH PRIVILEGES;

  7. 自增字段 auto_commit的研究分析

    MySQL自增字段,自增字段计数器在主存储里面,不在硬盘上(This counter is stored only in main memory, not on disk). 1,添加表,设立自增主键 ...

  8. mysql 函数执行权限

    mysql> show grants for query_all@'115.236.1x0.x'; +---------------------------------------------- ...

  9. HDU 1076 An Easy Task

    题解:枚举即可…… #include <cstdio> int main(){ int now,y,n,T,count; scanf("%d",&T); whi ...

  10. 九一八-->我逝去的青春

    九一八纪念馆 十二年前 30元一张门票 我毫不犹豫掏钱进去参观 你们笑我 钱少人傻 在东北的四年 从2001到2005 每年都感慨这一天 北国的秋色里 警钟长鸣 长鸣声中 有我逝去的青春 如今 三十而 ...