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. Scala学习之延迟绑定

    package com.swust.example object TraitDemo2 extends App{ //抽象类 abstract class Writer { def writeMess ...

  2. Android 快速选择联系人

    Activity 代码如下: /* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache ...

  3. 一道Python练习题

    有关字符串与编码的,转自廖雪峰Python教程. 小明的成绩从去年的72分提升到了今年的85分,请计算小明成绩提升的百分点,并用字符串格式化显示出'xx.x%',只保留小数点后1位: # -*- co ...

  4. poj 3487 稳定婚姻

    /** 稳定婚姻:男生不停的求婚,女生不停地拒绝 **/ #include <iostream> #include <queue> #include <cstdio> ...

  5. python爬虫数据抓取方法汇总

    概要:利用python进行web数据抓取方法和实现. 1.python进行网页数据抓取有两种方式:一种是直接依据url链接来拼接使用get方法得到内容,一种是构建post请求改变对应参数来获得web返 ...

  6. for循环产生的Cortex-M3汇编代码的一个奇怪现象

    最近比较一下KEIL和IAR两个编译器产生的代码,基于Cortex-M3处理器的,然后发现了一几个奇怪的地方. 很简单的一个C的for循环 void fun_for_add_65535(void) { ...

  7. NYOJ541 最强DE 战斗力(第五届省赛试题)

    最强DE 战斗力 时间限制:1000 ms  |  内存限制:65535 KB 难度: 描述 春秋战国时期,赵国地大物博,资源非常丰富,人民安居乐业.但许多国家对它虎视眈眈,准备联合起来对赵国发起一场 ...

  8. 黑客瑞士军刀NC使用教程

    ###################################################################### 1. 写在前面的话 ################### ...

  9. 关于js封装框架类库之属性操作

    在对DOM对象操作时,往往都要涉及到其属性的操作,为了提高开发效率,同时兼顾浏览器的性能,在这简单的封装了几个常见的属性.因为是模块化,在这只是引入了部分代码,其他代码在前几篇模块封装中有写.如有不足 ...

  10. MSSQL数据库统计所有表的记录数

    今天需要筛选出来库中行数不为零的表,于是动手写下了如下存储过程. CREATE PROCEDURE TableCount AS BEGIN SET NOCOUNT ON ),RowsCount INT ...