Kiki & Little Kiki 2

时间限制:5000 ms  |  内存限制:65535 KB
难度:4

描写叙述
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)

输入
The input contains no more than 1000 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.
输出
For each data set, output all lights' state at m seconds in one line. It only contains character '0' and '1.
例子输入
1
0101111
10
100000001
例子输出
1111000
001000010

题意:给出一些灯的初始状态(用0、1表示)。对这些灯进行m次变换。若当前灯的前一盏灯的状态为1,则调整当前灯的状态。0变为1,1变为0;否则不变。第1盏灯的前一盏灯是最后一盏灯。

问最后每盏灯的状态。

分析:通过模拟能够发现,如果有n盏灯。第i盏灯的状态为f[i],则f[i] = (f[i] + f[i-1])%2;又由于这些灯形成了环,则f[i] = (f[i] + f[(n+i-2)%n+1])%2. 这样初始状态形成一个1*n的矩阵。然后构造出例如以下n*n的矩阵:

1  1  0…… 0   0

0  1  1…… 0   0

……………………

1  0  0…… 0   1

每次乘以这个矩阵得出的结果就是下一个状态。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 105;
char state[N];
struct Matrix {Matrix() {}void Init(int n) {row = n; col = n;memset(mat, 0, sizeof(mat));for(int i = 0; i < n; i++)mat[i][i] = 1;}void Init(int m, int n) {row = m; col = n;memset(mat, 0, sizeof(mat));}int row, col;int mat[N][N];const Matrix &Pow(int n);
};const Matrix &operator *(const Matrix &A, const Matrix &B) {Matrix res;res.Init(A.row, B.col);for(int i = 0; i < A.row; i++) {for(int j = 0; j < A.col; j++) {if(A.mat[i][j]) {for(int k = 0; k < B.col; k++) {if(B.mat[j][k])res.mat[i][k] = res.mat[i][k] ^ (A.mat[i][j] & B.mat[j][k]);}}}}return res;
}const Matrix &Matrix::Pow(int n) {Matrix tmp, pre;tmp = *this;pre.Init(this->row);while(n) {if(n&1) pre = tmp * pre;tmp = tmp * tmp;n >>= 1;}return pre;
}int main() {int m;Matrix A, B, ans;while(~scanf("%d", &m)) {scanf("%s",state);int len = strlen(state);A.Init(1, len);for(int i = 0; i < len; i++)A.mat[0][i] = state[i] - '0';B.Init(len);for(int i = 0; i < len; i++) {B.mat[i][i] = 1;B.mat[i][(i+1)%len] = 1;}ans = A * B.Pow(m);for(int i = 0; i < len; i++)printf("%d", ans.mat[0][i]);printf("\n");}return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

NYOJ 300 amp;amp; hdu 2276 Kiki amp; Little Kiki 2 (矩阵高速功率)相关推荐

  1. HDU 2842 Chinese Rings(矩阵高速功率+递归)

    职务地址:HDU 2842 这个游戏是一个九连环的游戏. 如果当前要卸下前n个环.由于要满足前n-2个都卸下,所以要先把前n-2个卸下.须要f(n-2)次.然后把第n个卸下须要1次,然后这时候要卸下第 ...

  2. HDU 2276 Kiki Little Kiki 2 (位运算+矩阵快速幂)

    HDU 2276 Kiki & Little Kiki 2 (位运算+矩阵快速幂) ACM 题目地址:HDU 2276 Kiki & Little Kiki 2 题意:  一排灯,开关 ...

  3. mysql 矩阵运算_HDU 2276 Kiki amp; Little Kiki 2 (位运算+矩阵快速幂)

    HDU 2276 Kiki Little Kiki 2 (位运算矩阵快速幂) ACM 题目地址:HDU 2276 Kiki Little Kiki 2 题意 : 一排灯,开关状态已知,每过一秒:第i个 ...

  4. 【HDU - 4990】 Reading comprehension (构造+矩阵快速幂)

    题干: Read the program below carefully then answer the question.  #pragma comment(linker, "/STACK ...

  5. hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)

    http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...

  6. NYOJ 300 hdu 2276 Kiki Little Kiki 2 (矩阵快速幂)

    Kiki & Little Kiki 2 时间限制:5000 ms  |  内存限制:65535 KB 难度:4 描述 There are n lights in a circle numbe ...

  7. HDU 2276 Kiki Little Kiki 2

    题意:给出N个灯,"1"表示开,"0"表示关,并且灯连成环,比如"0101"第一个"0"灯左侧是最后一个"1& ...

  8. hdu 2276【Kiki Little Kiki 2】

    看到有人0ms爽过,我781ms过,心里有点不是滋味儿...... 还是说说自己的思路吧: 1.根据题目的意思我们就应该知道是将原来的数据右移(譬如第二位移到第一位),当然了最高位应该被最低位的数补充 ...

  9. HDU 2157 How many ways?? 临接矩阵+快速幂

    Problem Description 春天到了, HDU校园里开满了花, 姹紫嫣红, 非常美丽. 葱头是个爱花的人, 看着校花校草竞相开放, 漫步校园, 心情也变得舒畅. 为了多看看这迷人的校园, ...

  10. HDU 1757 A Simple Math Problem(矩阵快速幂)

    题目链接 题意 :给你m和k, 让你求f(k)%m.如果k<10,f(k) = k,否则 f(k) = a0 * f(k-1) + a1 * f(k-2) + a2 * f(k-3) + -- ...

最新文章

  1. 企业不要求工程师资格认证_谁说工程师不能成为企业家?
  2. 【Python-ML】SKlearn库性能指标-混淆矩阵和F1
  3. mac下对NTFS格式的磁盘进行读写操作
  4. leetcode22. 括号生成(回溯)
  5. TabBarController创建及使用方法简介
  6. 电脑护眼设置_解锁办公新技能 海信护眼平板Q5玩转工作无负担-科技频道
  7. java如何同时画多个图形_如何绘制两个不同系列的箱线图?
  8. 在docker上和ubuntu上运行InfoGAN
  9. wxpython grid设置字体颜色_Ext grid改变行背景颜色 和改变行字体颜色
  10. java调用jni_Java调用JNI
  11. 高斯过程回归python_GPR(高斯过程回归)详细推导
  12. Nvidia风扇速度自动调节工具推荐
  13. ipv6的127位掩码如何表示_IP地址与子网掩码划分经验分享
  14. 2019 年第 33 周 DApp 影响力排行榜 | TokenInsight
  15. 一枚钻戒如何成功借势世界杯,与粉丝秀恩爱
  16. elementui表格鼠标滑轮控制横向滚动
  17. ArcGIS三维分析之ArcGlobe简要说明
  18. cleanmymac苹果电脑必备mac系统垃圾清理工具分享
  19. STK中心天体cb等相关文件说明
  20. [HDF5] 封装了一个简单的C++ HDF5工具库,实现常用数据类型的读写

热门文章

  1. html内容超出不自动滚动,16.css: overflow使用 例: 固定div大小,不让内容超出div
  2. linux脚本彩色提示,在shell脚本里显示带颜色的字(linux)
  3. 工程力学考研 可以转计算机专业吗,跨专业考研我是工程力学的本科生,想要考飞行 – 手机爱问...
  4. matlab padarray,MATLAB中padarray函数用法
  5. html取消css样式,css如何取消样式
  6. mysql grant 用户权限
  7. defunct 进程占用端口_纯干货:23个服务器常见问题处理方法!
  8. mysql references关键字_mysql关键字有哪些?
  9. mysql 纵列转横列_mysql行列转换方法总结
  10. linux 下 dhcp failover相关