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)如果前一盏灯亮着,则改变这一盏灯 ...
随机推荐
- git上
## 建立本地版本库 ## 本地版本库与远程关联 ## 修改文件并提交 ## 创建分支,修改文件合并至master 1. git的由来 linux系统是很多开发者贡献代码不断完善的,linux的创始人 ...
- HDU 1054 Hungary
Strategic Game Problem Description Bob enjoys playing computer games, especially strategic games, bu ...
- 我的MYSQL学习心得(推荐)
http://www.cnblogs.com/lyhabc/category/573945.html
- android中复制图片
activity_main.xml中的配置 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi ...
- spring data jpa 、hibernate、jpa之间的关系
引用:http://blog.csdn.net/u014421556/article/details/52635000 hibernate作为JPA的实现. JPA规范与ORM框架之间的关系 ...
- Excel常用的小技巧
1.Excel如何实现单元格内轻松换行:按住ALT+enter就可以了. 2.Excel固定表头:在“视图”>冻结窗口>冻结首行. 3.防止电脑突然断电,导致正在编辑的Excel数据丢失, ...
- 数据库 'tempdb' 的事务日志已满。若要查明无法重用日志中的空间的原因
最常的做法: --1.清空日志 DUMP TRANSACTION tempdb WITH NO_LOG --2.截断事务日志: BACKUP LOG tempdb WITH NO_LOG --3.收缩 ...
- 复习java基础第六天(IO)
一:File 类 • 输入:读取外部数据(磁盘.光盘等存储设备的数据)到程序(内存)中. • 输出:将程序(内存)数据输出到磁盘.光盘等存储设备中 • Java 的 IO 流主要包括输入.输出两种 ...
- 浅谈Java三大框架与应用
前言:对于一个程序员来说,尤其是在java web端开发的程序员,三大框架:Struts+Hibernate+Spring是必须要掌握熟透的,因此,下面谈谈java三大框架的基本概念和原理. JAVA ...
- Yoga710笔记本Win10和Ubuntu系统共存
联想yoga710默认安装了win10系统,且使用EFI分区格式,安装Ubuntu不是一般的困难,经公司小哥的帮助下,几次终于完成了Ubuntu和Win10 共存. 经过多次安装测试,暂时能运行成功的 ...