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 ...
随机推荐
- 【Atcoder】CODE FESTIVAL 2017 qual A D - Four Coloring
[题意]给定h,w,d,要求构造矩阵h*w满足任意两个曼哈顿距离为d的点都不同色,染四色. [算法]结论+矩阵变换 [题解] 曼哈顿距离是一个立着的正方形,不方便处理.d=|xi-xj|+|yi-yj ...
- 【洛谷 P2513】 [HAOI2009]逆序对数列(DP)
题目链接 这种求方案数的题一般都是\(dp\)吧. 注意到范围里\(k\)和\(n\)的范围一样大,\(k\)是完全可以更大的,到\(n\)的平方级别,所以这暗示了我们要把\(k\)写到状态里. \( ...
- bzoj 1588 bst
用set存下就好了. /************************************************************** Problem: 1588 User: BLADE ...
- vue_router添加点击事件
1.在vue学习中遇到给router-link 标签添加事件@click .@mouseover等无效的情况 原来的代码: <router-link to='/SelectPage' @clic ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- 10.异步SRAM的FPGA读写操作
异步SRAM:正如其名,不是与特定的时钟信号同步运行,而是根据输入信号的状态运行的.因为没有信号表明读取时已确定了有效数据,也没有信号表明写入时已接收到数据,所以,需要获取制造商的数据手册,根据时序图 ...
- Laravel artisan commands
使用php artisan list 可以看到artisan的所有命令以及选项. 当然你也可以在此基础上扩展自己的命令. 1. key 1.1 key:generate 这是一个加密秘钥,用于保证安全 ...
- 自定制Form组件
代码 import re import copy class ValidateError(Exception): def __init__(self,detail): self.detail = de ...
- QT中ui更改后不能更新的解决方法
ui源文件到界面显示的原理可以网上搜索,这里不再描述.简单讲就是先要从*.ui生成ui_*.h然后再编译,所以界面未更新实际上是因为ui_*.h这个文件没有更新导致的. 出现此问题后我尝试了以下几个方 ...
- 1.Spark Streaming另类实验与 Spark Streaming本质解析
1 Spark源码定制选择从Spark Streaming入手 我们从第一课就选择Spark子框架中的SparkStreaming. 那么,我们为什么要选择从SparkStreaming入手开始我们 ...