hdu2276---Kiki & Little Kiki 2(矩阵)
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
HDU 8th Programming Contest Site(1)
Recommend
lcy | We have carefully selected several similar problems for you: 2256 2254 3117 2855 2971
每个灯的状态都由它左边那一个决定
构造出一个n*n的系数矩阵,每一行,每一列相应位置为1,
比方对于3个数的系数矩阵
1 1 0
0 1 1
1 0 1
那么初始为110的状态,1s以后即为101
也就是[1 1 0] 去乘上面那个矩阵得到的新矩阵
那么仅仅要矩阵高速幂加速即可了
/*************************************************************************
> File Name: hdu2276.cpp
> Author: ALex
> Mail: zchao1995@gmail.com
> Created Time: 2015年03月12日 星期四 21时28分55秒
************************************************************************/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
int n;
class MARTIX
{
public:
int mat[110][110];
MARTIX();
MARTIX operator * (const MARTIX &b)const;
MARTIX& operator = (const MARTIX &b);
};
MARTIX :: MARTIX()
{
memset (mat, 0, sizeof(mat));
}
MARTIX MARTIX :: operator * (const MARTIX &b)const
{
MARTIX ret;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
for (int k = 0; k < n; ++k)
{
ret.mat[i][j] += (this -> mat[i][k] * b.mat[k][j]);
ret.mat[i][j] %= 2;
}
}
}
return ret;
}
MARTIX& MARTIX :: operator = (const MARTIX &b)
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
this -> mat[i][j] = b.mat[i][j];
}
}
return *this;
}
MARTIX fastpow(MARTIX A, int m)
{
// void Debug(MARTIX A);
MARTIX ret;
for (int i = 0; i < n; ++i)
{
ret.mat[i][i] = 1;
}
while (m)
{
if (m & 1)
{
ret = ret * A;
}
m >>= 1;
A = A * A;
}
return ret;
}
void Debug(MARTIX A)
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
printf("%d ", A.mat[i][j]);
}
printf("\n");
}
}
char str[110];
int main ()
{
int m;
while (~scanf("%d", &m))
{
scanf("%s", str);
MARTIX F;
n = strlen(str);
MARTIX A;
for (int i = 0; i < n - 1; ++i)
{
A.mat[i][i] = A.mat[i][i + 1] = 1;
}
A.mat[n - 1][0] = A.mat[n - 1][n - 1] = 1;
// Debug(A);
A = fastpow(A, m);
for (int i = 0; i < n; ++i)
{
F.mat[0][i] = str[i] - '0';
}
F = F * A;
for (int i = 0; i < n; ++i)
{
printf("%d", F.mat[0][i]);
}
printf("\n");
}
return 0;
}
hdu2276---Kiki & Little Kiki 2(矩阵)的更多相关文章
- HDU2276 - Kiki & Little Kiki 2(矩阵高速幂)
pid=2276">题目链接 题意:有n盏灯.编号从1到n.他们绕成一圈,也就是说.1号灯的左边是n号灯.假设在第t秒的时候,某盏灯左边的灯是亮着的,那么就在第t+1秒的时候改变这盏灯 ...
- NYOJ 300 && hdu 2276 Kiki & Little Kiki 2 (矩阵高速功率)
pid=300">Kiki & Little Kiki 2 时间限制:5000 ms | 内存限制:65535 KB 难度:4 描写叙述 There are n light ...
- HDU - 2276 Kiki & 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 ...
- HDU 2147 kiki's game kiki的游戏(博弈,找规律)
题意: 给一个有n*m 个格子的棋盘,将一个硬币放在右上角一格,每次可以往左/下/左下移动一格,碰到不能移动的局面者输. 思路: 找P/N状态.先将(n,1)归为P状态,那么能一步到达此位置的有3个位 ...
- HDU2276 Kiki & Little Kiki 2 矩阵快速幂
Kiki & Little Kiki 2 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- Linux基础学习-用户的创建修改删除
用户添加修改删除 1 useradd添加用户 添加一个新用户hehe,指定uid为3000,家目录为/home/haha [root@qdlinux ~]# useradd -u 3000 -d /h ...
- HDU 2276 Kiki & Little Kiki 2(矩阵位运算)
Kiki & Little Kiki 2 转载自:点这里 [题目链接]Kiki & Little Kiki 2 [题目类型]矩阵位运算 &题意: 一排灯,开关状态已知,每过一秒 ...
- HDU 2276 Kiki & Little Kiki 2 矩阵构造
Kiki & Little Kiki 2 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- [HDU2276]Kiki & Little Kiki 2
题目:Kiki & Little Kiki 2 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2276 分析: 1)如果前一盏灯亮着,则改变这一盏灯 ...
随机推荐
- PCB SQL MS 将多行有序数据转为一行数据(一列转一行)
一.原数据:多行有序 SELECT CC.techname FROM PPEflow BB LEFT JOIN pubgyxxb CC ON BB.techno = CC.techno ORDER B ...
- Agri-Net(prim)
http://poj.org/problem?id=1258 #include<stdio.h> #include<string.h> ; <<; int map[ ...
- 2-bitmap
在2.5亿个整数中找出不重复的整数,注,内存不足以容纳这2.5亿个整数. 思路: bitmap用一个bit来代表存在还是不存在,现在我们要判断重不重复,则需要三个状态:不存在,存在一个,存在多个.2b ...
- BZOJ 1303
思路: 水题 竟然不会做 尴尬 比b大的数=1 比b小的数=-1 找到b 统计一下左边比b大x的数有多少 扫右边的时候就查左边的表 就可以了 //By SiriusRen #include < ...
- SQLServer2008 关于Having
转自百度百科 与where 相比 HAVING 子句对 GROUP BY 子句设置条件的方式与 WHERE 子句和 SELECT 语句交互的方式类似. WHERE 子句搜索条件在进行分组操作之前应用: ...
- vue-pdf的使用方法及解决在线打印预览乱码
最近在用vue做项目的时候,页面中需要展示后端返回的PDF文件,于是便用到了vue-pdf,其使用方法为 : npm install --save vue-pdf 官网地址:https://www.n ...
- ie8及其以下版本兼容性问题之响应式
解决办法:引入Respond.js让IE6-8支持CSS3 Media Query 使用方式 参考官方demo:http://scottjehl.github.com/Respond/test/tes ...
- java的原子变量
java的原子变量类似c++的InterlockedDecrement()操作.其实就是在进行算术时,把整个算式看为一个整体,并且保证同一时间只计算该式子一次. 它的用途比如,多个线程可能会调用某个函 ...
- js-事件处理(重点)
1:各种常用事件: 2:简单用法: <body onLoad="javascript:alert('hello world');" onUnload="javasc ...
- 【SQL】BETWEEN操作符
BETWEEN 操作符在 WHERE 子句中使用,作用是选取介于两个值之间的数据范围. 操作符 BETWEEN ... AND 会选取介于两个值之间的数据范围.这些值可以是数值.文本或者日期. 注意: ...