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)如果前一盏灯亮着,则改变这一盏灯 ...
随机推荐
- Elasticsearch 7.1.1 集群 + 配置身份验证
一.安装Elasticsearch 1.1 环境说明 Centos7.6 Elasticsearch7.1.1 #挂载数据盘 fdisk /dev/vdb n,p,,回车,回车,wq fdisk -l ...
- [BZOJ1601] 灌水
难点:找到正确方式建图 知识点:Kruskal 分析:这种题肯定要把点权转换到边权上,但肯定无法搞到和其他点相连的边上,怎么办呢?那就再造一个点呗,这个“超级点”和所有点相连,且边权=点权,于是就可以 ...
- BZOJ 4698 差分+后缀数组
思路: 对所有序列差分一下 公共串的长度+1就是答案了 二分 扫一遍height即可,.. //By SiriusRen #include <cstdio> #include <cs ...
- scrollWidth clientWidth offsetWidth
scrollWidth:对象的实际内容的宽度,不包边线宽度,会随对象中内容超过可视区后而变大. 实际内容+padding 不包括滚动条 边框client ...
- springBoot jar/war打包部署问题
先给pom.xml配置导出插件 <!--配置插件将Maven 插件 导出成为jar --> <plugin> <groupId>org.springframewor ...
- Linux通信之同步阻塞模式
[参考]韦东山 教学笔记 1. 原子操作原子操作指的是在执行过程中不会被别的代码路径所中断的操作.常用原子操作函数举例:atomic_t v = ATOMIC_INIT(0); //定义原子变量v并初 ...
- SAP computer之input and MAR
Input and MAR Below the program counter is the input and MAR block. It includes the address and data ...
- Assembly之instruction之MOV
MOV[.W] Move source to destinationMOV.B Move source to destination Syntax MOV src,dst or M ...
- jQuery中容易让人困惑的东西
前言:jqueryt很灵活,太灵活了,可以说是他一个优点,也是他一个缺点,达到一种效果,十个人也许会用十种不同的方法来实现这个过程,结果一样,过程不一样,这到底是好,还是坏呢. 一,什么是jquery ...
- 使用 C# 进行 HTTP 操作
说明 主要使用到了 Newtonsoft.Json 和 System.Net 两个命名空间. Program.cs static void Main(string[] args) { WebOpert ...