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)如果前一盏灯亮着,则改变这一盏灯 ...
随机推荐
- Html5 ajax的跨域请求
1.XMLHttpRequest升级版已经实现了跨域请求.不过需要在后台设置:header("Access-Control-Allow-Origin:http://www.a.com&quo ...
- 在PL/SQL中使用带参数的游标
需求:查询并输出部门名称为SALES的员工信息 SET serveroutput ON; DECLARE CURSOR c_emp(paramName VARCHAR2) IS SELECT * FR ...
- My97DatePicker 动态设置有效/无效日期
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...
- PHP开发笔记(三)关于PHP伪静态的问题总结
Apache 第一个问题就是关于PHPStudy集成Apache环境下5.5版本以上”No input file specified“问题. 针对TP5框架,以下是.htaccess文件的配置,PHP ...
- html5与css3入门知识点精炼
<meta name = "keywords" content="…………"/>(网页搜索时要输入的关键字) <meta name = &qu ...
- windows下react-native搭建环境
第一步:安装Java 1.下载JDK,选择适应自己的机型:官网地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downl ...
- USB 接口探测分类
USB 接口探测分类 SDP (Standand Downstream Port) 标准下行接口 标准USB都支持的接口 这种端口的D+和D-线上具有15kΩ下拉电阻.限流值如上讨论:挂起时为2.5m ...
- 【汇编】dosbox钢琴
DATA SEGMENT msg DB 0DH,0AH,'[ 1 2 3 4 5 6 7 ]' DB 0DH,0AH,' [ q w e r t y u ]' DB 0DH,0AH,'________ ...
- (转)基于MVC4+EasyUI的Web开发框架经验总结(12)--利用Jquery处理数据交互的几种方式
http://www.cnblogs.com/wuhuacong/p/4085682.html 在基于MVC4+EasyUI的Web开发框架里面,大量采用了Jquery的方法,对数据进行请求或者提交, ...
- HDU_1729_sg函数(dfs)
Stone Game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...