Kiki & Little Kiki 2

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1659    Accepted Submission(s): 837

Problem 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
 
Source
 
Recommend
lcy
 
 /*
题意:一个灯环,输出进行N此变化的结果,变化的规则是:如果左边是1,那么自己变成相反的
1变成0 0变成1 。第一个要以最后一个为参考。 矩阵的变化, | 1 0 0 0 0 1| a1 | (a1+a6)%2 |
| 1 1 0 0 0 0| a2 | (a1+a2)%2 |
| 0 1 1 0 0 0| a3 === | (a2+a3)%2 |
| 0 0 1 1 0 0| a4 | (a3+a4)%2 |
| 0 0 0 1 1 0| a5 | (a4+a5)%2 |
| 0 0 0 0 1 1| a6 | (a5+a6)%2 | 利用矩阵相乘就可以快速到求取出来.
优化1.由于左边是稀疏矩阵,那么在矩阵相乘到时候,可以优化。代码中有!!~
优化2.用位操作。怎么用位操作实现?
优化3.在快速幂的时候,顺序的变化。因为稀疏矩阵的存在,所以改变一下顺序
就会有提高。代码中有...
*/ #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std; char a[];
struct node
{
int mat[][];
}M_tom,M_hxl; void make_first(node *cur,int len)
{
int i,j;
for(i=;i<=len;i++)
for(j=;j<=len;j++)
if(i==j) cur->mat[i][j]=;
else cur->mat[i][j]=;
} void cheng2(node cur,char a[],int len)
{
node ww;
int i,j,k;
memset(ww.mat,,sizeof(ww.mat));
for(i=;i<=len;i++)
for(k=;k<=len;k++)
if(cur.mat[i][k])
{
for(j=;j<=;j++)
{
ww.mat[i][j]=(ww.mat[i][j]^(cur.mat[i][k]&(a[k]-'')))&;
}
}
for(i=;i<=len;i++)
printf("%d",ww.mat[i][]);
printf("\n");
} struct node cheng(node cur,node now,int len)
{
node ww;
int i,j,k;
memset(ww.mat,,sizeof(ww.mat));
for(i=;i<=len;i++)
for(k=;k<=len;k++)
if(cur.mat[i][k])//对稀疏矩阵的优化
{
for(j=;j<=len;j++)
{
if(now.mat[k][j])//同理
{
ww.mat[i][j]=(ww.mat[i][j]^(cur.mat[i][k]&now.mat[k][j]))&;
}
}
}
return ww;
} void make_init(int len)
{ for(int i=;i<=len;i++)
M_hxl.mat[][i]=; M_hxl.mat[][]=;
M_hxl.mat[][len]=;
for(int i=;i<=len;i++)
for(int j=;j<=len;j++)
if(i==j || i-==j)
M_hxl.mat[i][j]=;
else M_hxl.mat[i][j]=;
} void power_sum2(int n,int len,char a[])
{
make_first(&M_tom,len);
while(n)
{
if(n&)
{
M_tom=cheng(M_hxl,M_tom,len);//这也是优化。顺序改变结果就不同了
}
n=n>>;
M_hxl=cheng(M_hxl,M_hxl,len);
}
// cs(len);
cheng2(M_tom,a,len);
} int main()
{
int n,len;
while(scanf("%d",&n)>)
{
scanf("%s",a+);
len=strlen(a+);
make_init(len);
power_sum2(n,len,a);
}
return ;
}

HDU 2276 Kiki & Little Kiki 2 矩阵构造的更多相关文章

  1. 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 ...

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

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

  3. hdu 2276 Kiki & Little Kiki 2

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

  4. HDU 2276 矩阵快速幂

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

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

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

  6. [HDU2276]Kiki & Little Kiki 2

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

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

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

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

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

  9. HDU - 2276 Kiki &amp; Little Kiki 2

    Description There are n lights in a circle numbered from 1 to n. The left of light 1 is light n, and ...

随机推荐

  1. 洛谷P3676 小清新数据结构题(动态点分治+树链剖分)

    传送门 感觉这题做下来心态有点崩……$RMQ$求$LCA$没有树剖快我可以理解为是常数太大……然而我明明用了自以为不会退化的点分然而为什么比会退化的点分跑得反而更慢啊啊啊啊~~~ 先膜一波zsy大佬 ...

  2. C++多线程编程二

    1. 死锁与解锁: #include <iostream> #include <thread> #include <mutex> using namespace s ...

  3. SpringAOP的应用实例与总结

    一:AOP的背景 面试的时候面试官让我解释一下什么是AOP,当时不懂,在路上就查了,AOP:面向切面的编程技术,困惑了,JAVA是OOP:面向对象的编程技术.那么自己就立刻查了几个为题:1.什么是面向 ...

  4. tomcat运行springboot项目war包

    以最简单的spring boot demo项目来演示如何发布项目war包到tomcat,并成功运行(有很多小伙伴会出现404错误) 一.准备一个最简单的demo项目 在IDEA中新建一个项目,一直ne ...

  5. 前端知识总结--BFC

    Block Formatting Context,中文直译为块级格式上下文. 1. BFC的定义 是 W3C CSS 2.1 规范中的一个概念,它决定了元素如何对其内容进行定位,以及与其他元素的关系和 ...

  6. C++ class和struct的区别

    class和struct定义类唯一的区别就是默认的访问权限. 如果我们使用struct关键字,则定义在第一个访问说明符之前的成员是public的:相反,如果我们使用class关键字,组这些成员是pri ...

  7. CF1139D Steps to One 题解【莫比乌斯反演】【枚举】【DP】

    反演套 DP 的好题(不用反演貌似也能做 Description Vivek initially has an empty array \(a\) and some integer constant ...

  8. cool kickass

    I can stay like this alllllllllll daaaaaaaaayyyyyy.

  9. Windows开发经验 - WinDbg

    1. 远程调试 参考文章:https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/remode-debugging-usi ...

  10. 转 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()

    在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException:  Can't create handler inside thread that has ...