描述

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<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<cstdio>
#include<sstream>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include <ctype.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std; typedef long long ll;
const int maxn=1001;
const int INF=0x3f3f3f3f; const int N = 102; struct mat
{
int r, c;
int M[N][N];
mat(int r, int c):r(r), c(c)
{
memset(M, 0, sizeof(M));
}
}; mat mul(mat& A, mat& B)
{
mat C(A.r, B.c);
for(int i = 0; i < A.r; ++i)
for(int j = 0; j < A.c; ++j)
if(A.M[i][j]) //优化,只有state为1的时候才需要改变
{
for(int k = 0; k < B.r; ++k)
if(B.M[j][k])
C.M[i][k] ^= A.M[i][j] & B.M[j][k];
}
return C;
} mat pow(mat& A, int k)
{
mat B(A.r, A.c);
for(int i = 0; i < A.r; ++i) B.M[i][i] = 1; while(k)
{
if(k & 1) B = mul(B, A);
A = mul(A, A);
k >>= 1;
}
return B;
} int main()
{
int m;
char t[105]; while(scanf("%d %s", &m, t) != EOF)
{
int n = strlen(t);
mat A(1, n);
mat T(n, n);
for(int i = 0; i < n; ++i)
{
A.M[0][i] = t[i] - '0';
T.M[i][i] = T.M[i][(i + 1) % n] = 1;
} T = pow(T, m);
A = mul(A, T); for(int i = 0; i < n; ++i)
{
printf("%d", A.M[0][i]);
}
printf("\n");
} return 0;
}

nyoj 300 (矩阵快速幂)Kiki & Little Kiki 2的更多相关文章

  1. poj 3070 && nyoj 148 矩阵快速幂

    poj 3070 && nyoj 148 矩阵快速幂 题目链接 poj: http://poj.org/problem?id=3070 nyoj: http://acm.nyist.n ...

  2. HDU2276 Kiki & Little Kiki 2 矩阵快速幂

    Kiki & Little Kiki 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  3. HDU 2276 Kiki & Little Kiki 2( 矩阵快速幂 + 循环同构矩阵 )

    蒟蒻的我还需深入学习 链接:传送门 题意:给出一个长度为 n,n 不超过100的 01 串 s ,每当一个数字左侧为 1 时( 0的左侧是 n-1 ),这个数字就会发生改变,整个串改变一次需要 1s ...

  4. 矩阵快速幂之Kiki & Little Kiki 2

    题意是:给出一串01串,每一秒,每个位置得灯会根据左边那个灯得状态进行改变,(第一个得左边为最后一个)如果左边为1,那么自己就会改变状态,左边为0则不用,问n秒改01串的状态 ///// 首先,我们发 ...

  5. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  6. 2017ACM暑期多校联合训练 - Team 2 1006 HDU 6050 Funny Function (找规律 矩阵快速幂)

    题目链接 Problem Description Function Fx,ysatisfies: For given integers N and M,calculate Fm,1 modulo 1e ...

  7. hdu 1597(矩阵快速幂)

    1597: 薛XX后代的IQ Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 228  Solved: 55[Submit][Status][Web Bo ...

  8. BNU29139——PvZ once again——————【矩阵快速幂】

    PvZ once again Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java cla ...

  9. 2019牛客多校第五场 B - generator 1 矩阵快速幂+十倍增+二进制倍增优化

    B - generator 1 题意 给你\(x_{0}.x_{1}.a.b.b.mod\),根据\(x_{i} = a*x_{i-1} + b*x_{i-2}\)求出\(x_{n}\) 思路 一般看 ...

  10. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

随机推荐

  1. Android中Handler导致的内存泄露

    http://www.androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html Consider the follo ...

  2. GBK UTF-16 UTF-8 编码表

    GBK   UTF-16 UTF-8 ================== D2BB  4E00  E4 B8 80  一 B6A1  4E01  E4 B8 81  丁 C6DF  4E03  E4 ...

  3. Spark-2.3.2【SparkStreaming+SparkSQL-实时仪表盘应用】

    应用场景:实时仪表盘(即大屏),每个集团下有多个mall,每个mall下包含多家shop,需实时计算集团下各mall及其shop的实时销售分析(区域.业态.店铺TOP.总销售额等指标)并提供可视化展现 ...

  4. metlnfo 5.3.1 sql注入复现

    首先还是要说的是metlnfo是伪全局变量机制 所以如下: /admin/include/global.func.php function save_met_cookie(){ global $met ...

  5. 有向有权图的最短路径算法--Dijkstra算法

    Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Di ...

  6. chrome://settings/content

    chrome://settings/content C:\Users\用户名\AppData\Roaming\Microsoft\Internet Explorer chrome://version/

  7. Linux进程调度与源码分析(二)——进程生命周期与task_struct进程结构体

    1.进程生命周期 Linux操作系统属于多任务操作系统,系统中的每个进程能够分时复用CPU时间片,通过有效的进程调度策略实现多任务并行执行.而进程在被CPU调度运行,等待CPU资源分配以及等待外部事件 ...

  8. linux系统下git使用

    转载:http://www.cnblogs.com/bear2flymoon/p/4335364.html?ADUIN=563508762&ADSESSION=1430887070&A ...

  9. TCP之非阻塞connect和accept

    套接字的默认状态是阻塞的,这就意味着当发出一个不能立即完成的套接字调用时,其进程将被投入睡眠,等待响应操作完成,可能阻塞的套接字调用可分为以下四类: (1) 输入操作,包括read,readv,rec ...

  10. 数据库===轻量级mysql数据库管理工具

    已经上传至: https://download.csdn.net/download/bo_mask/10276952