nyoj 300 (矩阵快速幂)Kiki & Little Kiki 2
描述
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的更多相关文章
- poj 3070 && nyoj 148 矩阵快速幂
poj 3070 && nyoj 148 矩阵快速幂 题目链接 poj: http://poj.org/problem?id=3070 nyoj: http://acm.nyist.n ...
- HDU2276 Kiki & Little Kiki 2 矩阵快速幂
Kiki & Little Kiki 2 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- HDU 2276 Kiki & Little Kiki 2( 矩阵快速幂 + 循环同构矩阵 )
蒟蒻的我还需深入学习 链接:传送门 题意:给出一个长度为 n,n 不超过100的 01 串 s ,每当一个数字左侧为 1 时( 0的左侧是 n-1 ),这个数字就会发生改变,整个串改变一次需要 1s ...
- 矩阵快速幂之Kiki & Little Kiki 2
题意是:给出一串01串,每一秒,每个位置得灯会根据左边那个灯得状态进行改变,(第一个得左边为最后一个)如果左边为1,那么自己就会改变状态,左边为0则不用,问n秒改01串的状态 ///// 首先,我们发 ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- 2017ACM暑期多校联合训练 - Team 2 1006 HDU 6050 Funny Function (找规律 矩阵快速幂)
题目链接 Problem Description Function Fx,ysatisfies: For given integers N and M,calculate Fm,1 modulo 1e ...
- hdu 1597(矩阵快速幂)
1597: 薛XX后代的IQ Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 228 Solved: 55[Submit][Status][Web Bo ...
- BNU29139——PvZ once again——————【矩阵快速幂】
PvZ once again Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java cla ...
- 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}\) 思路 一般看 ...
- 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...
随机推荐
- .NET RabbitMQ
在企业应用系统领域,会面对不同系统之间的通信.集成与整合,尤其当面临异构系统时,这 种分布式的调用与通信变得越发重要.其次,系统中一般会有很多对实时性要求不高的但是执行起来比较较耗时的地方,比如发送短 ...
- GXC 钱包部署
参考: [ 官方 wiki ] 基于 Ubuntu 的 GXC 部署 基础环境 OS: Ubuntu gxc: 官方 [ release 最新版本 ] 下载 release 包(ubuntu) cd ...
- BTA 常问的 Java基础40道常见面试题及详细答案(山东数漫江湖))
八种基本数据类型的大小,以及他们的封装类 引用数据类型 Switch能否用string做参数 equals与==的区别 自动装箱,常量池 Object有哪些公用方法 Java的四种引用,强弱软虚,用到 ...
- Plant (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/185/A 题目: Dwarfs have planted a very interesting plant ...
- 关于Solaris系统“mpt_sas”驱动
1.mpt_sas 驱动源文件所在系统源代码中目录: illumos-soulos/usr/src/uts/common/sys/scsi/adapters/mpt_sas -- 头文件 illum ...
- lnmp、lamp、lnmpa一键安装包(Updated: 2016-4-12)
lnmp.lamp.lnmpa一键安装包(Updated: 2016-4-12) 文章目录 脚本特性 安装步骤 如何添加虚拟主机? 如何删除虚拟主机? 如何管理ftp账号? 数据备份 如何管理服务 ...
- 解决嵌套GridView显示不全的问题
package com.adan.selectcitydome.view; import android.content.Context; import android.util.AttributeS ...
- 代码合并:Merge、Rebase 的选择
图解 Git 命令 基本用法 上面的四条命令在工作目录.stage 缓存(也叫做索引)和 commit 历史之间复制文件. git add files 把工作目录中的文件加入 stage 缓存 git ...
- linux安装lamp
github https://github.com/zblogcn/zblogphp Installation If your server system: CentOS yum -y install ...
- device tree 負值 property 寫法
倘若你要設定 負值的property, 可能需要括符才會 build 過. 正確 decidegc = <(-10)>; 錯誤 decidegc = <-10>;