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. php install

    ./configure --prefix=/home/allen.mh/local/php --with-gd=/home/allen.mh/local/gd --with-jpeg-dir=/hom ...

  2. Labview学习之远程控制VI

    Labview学习之远程控制VI        从LabVIEW 6.1开始,LabVIEW集成了Remote Panels技术,允许用户直接在客户端计算机上打开并操作位于服务器端计算机上的VI的前面 ...

  3. JS中小数的差,比较大小

    var a = 0.3-0.2; -0.3; alert(a + "&" + b); if (a == b) { alert("true"); } el ...

  4. ExpandableListView 箭头样式

    ExpandableListVivew是ListView的子类,它在普通ListView的基础上进行了扩展,它把应用中的列表项分为几组,每组里 又可包含多个列表项.ExpandableListVive ...

  5. poj 1966 Cable TV Network 顶点连通度

    题目链接 给一个图, n个点m条边, 求至少去掉多少个点可以使得图不再联通.随便指定一个点为源点, 枚举其他点为汇点的情况, 跑网络流, 求其中最小的情况. 如果最后ans为inf, 说明是一个完全图 ...

  6. Python正则表达式指南(转载)

    转载自:http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html#3353540 1. 正则表达式基础 1.1. 简单介绍 正则表达式并不 ...

  7. Windows Phone 8.1 发送http 网络请求。

    在windows phone 8.1 中可以用 HttpClient 类来发送http 请求. 例子: try { Uri uri = new Uri(@"http://api.map.ba ...

  8. How to choose between zombie.js and PhantomJS for automated web testing? [closed]

    How to choose between zombie.js and PhantomJS for automated web testing? [closed] How to choose betw ...

  9. ceph启动脚本

    放在/etc/init.d/目录下,用法如下: root@u253:~# /etc/init.d/ceph === mon.a === usage: /etc/init.d/ceph [options ...

  10. xcode 工具栏中放大镜的替换的简单说明

    1.如果是在打开的文档范围内:       查找: Command+ F       替换: Option+Command+F                   Replace All   是全部替 ...