HDU 2276 矩阵快速幂
Kiki & Little Kiki 2
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2650 Accepted Submission(s): 1393
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 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.
0101111
10
100000001
001000010
using namespace std;
int N,M;
struct Matrix
{
int a[105][105];
Matrix operator*(Matrix tmp){
Matrix ans;
memset(ans.a,0,sizeof(ans.a));
for(int i=1;i<=N;++i){
for(int k=1;k<=N;++k){
for(int j=1;j<=N;++j){
ans.a[i][j]+=a[i][k]*tmp.a[k][j];
ans.a[i][j]%=2;
}
}
}
return ans;
}
};
void show(Matrix a)
{int i,j,k;
for(i=1;i<=N;++i){
for(j=1;j<=N;++j){
cout<<a.a[i][j]<<" ";
}cout<<endl;
}cout<<endl;
}
Matrix qpow(Matrix A,int n)
{
Matrix ans;
memset(ans.a,0,sizeof(ans.a));
for(int i=0;i<=N;++i) ans.a[i][i]=1;
while(n){
if(n&1) ans=ans*A;
A=A*A;
n>>=1;
}
return ans;
}
void solve(string s)
{
Matrix A;
int i,j,k,u[105];
char ans[105];
for(i=0;i<s.size();++i) u[i+1]=s[i]-'0';
memset(A.a,0,sizeof(A.a));
A.a[1][1]=A.a[N][1]=1;
for(i=2;i<=N;++i){
A.a[i-1][i]=A.a[i][i]=1;
}
A=qpow(A,M);
for(i=1;i<=N;++i){int d=0;
for(j=1;j<=N;++j){
d+=u[j]*A.a[j][i];
d%=2;
}
cout<<d%2;
}cout<<endl;
}
int main()
{
string s;
while(cin>>M>>s){
N=s.size();
solve(s);
}
return 0;
}
HDU 2276 矩阵快速幂的更多相关文章
- HDU 2855 (矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...
- HDU 4471 矩阵快速幂 Homework
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...
- HDU - 1575——矩阵快速幂问题
HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...
- hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...
- 随手练——HDU 5015 矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5015 看到这个限时,我就知道这题不简单~~矩阵快速幂,找递推关系 我们假设第一列为: 23 a1 a2 ...
- HDU 3802 矩阵快速幂 化简递推式子 加一点点二次剩余知识
求$G(a,b,n,p) = (a^{\frac {p-1}{2}}+1)(b^{\frac{p-1}{2}}+1)[(\sqrt{a} + \sqrt{b})^{2F_n} + (\sqrt{a} ...
- How many ways?? HDU - 2157 矩阵快速幂
题目描述 春天到了, HDU校园里开满了花, 姹紫嫣红, 非常美丽. 葱头是个爱花的人, 看着校花校草竞相开放, 漫步校园, 心情也变得舒畅. 为了多看看这迷人的校园, 葱头决定, 每次上课都走不同的 ...
- HDU 5950 矩阵快速幂
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 1757 矩阵快速幂 **
一看正确率这么高,以为是水题可以爽一发,结果是没怎么用过的矩阵快速幂,233 题解链接:点我 #include<iostream> #include<cstring> ; us ...
随机推荐
- Android 小例子服务端
这是之前发布的Android项目的服务端源码,只是简单的根据请求返回了一些测试数据,没有实现对数据库的操作,可以根据需求自己实现. 这是mvc4 WebAPI项目,需要用vs2012打开. 如果是用的 ...
- 浅谈Lambda表达式详解
lambda简介 lambda运算符:所有的lambda表达式都是用新的lambda运算符 " => ",可以叫他,“转到”或者 “成为”.运算符将表达式分为两部分,左边指定 ...
- Oracle下select语句
先看scott下自带的emp表 empno:编号 ename:名字 Job:职位 mgr:上级编号 hiredate:入职时间 sal:薪水 comm:奖金 deptno:部门编号 部门表dep ...
- debug error 错误日志的调试模式
https://docs.nginx.com/nginx/admin-guide/monitoring/logging/ error_log logs/error.log warn; In this ...
- RGBA HSB opengl光照模型
RGBA HSB HSV颜色模型对应于画家的配色的方法.画家用改变色浓和色深的方法来从某种纯色获得不同色调的颜色.其做法是:在一种纯色中加入白色以改变色浓,加入黑色以改变色深,同时加入不同比例的白 ...
- 【Python】Python 打印和输出更多用法。
Python 打印和输出 简述 在编程实践中,print 的使用频率非常高,特别是程序运行到某个时刻,要检测产生的结果时,必须用 print 来打印输出. 关于 print 函数,前面很多地方已经提及 ...
- (1.3)DML增强功能-Apply、pivot、unpivot、for xml path行列转换
深入了解行列转换请参考另一篇文章:https://www.cnblogs.com/gered/p/9271581.html 总结: 1.apply一般形式 --基本形式 SELECT a FROM d ...
- centos LAMP第三部分php,mysql配置 php配置文件 配置php的error_log 配置php的open_basedir 安装php的扩展模块 phpize mysql配置第二十一节课
centos LAMP第三部分php,mysql配置 php配置文件 配置php的error_log 配置php的open_basedir 安装php的扩展模块 phpize mysql配 ...
- 怎么应对 domino文档损坏然后损坏文档别删除导致数据丢失
对于domino 有个机制是同步 ..然后如果文档被损坏之后会通过同步或者压缩 之类的 然后将损坏文档删除 那么这样就有个风险..知识管理文档会被删除. 并且删除了之后管理员如果不仔细看日志的话也不会 ...
- Flume+Morphlines实现数据的实时ETL
转载:http://mp.weixin.qq.com/s/xCSdkQo1XMQwU91lch29Uw Apache Flume介绍: Apache Flume是一个Apache的开源项目,是一个分布 ...