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

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
 
一开始没想到公式。。。看了一眼别人的公示后推开了矩阵。。脑子秀逗了总把乘号写成加号
公式a[i]=(a[i]+a[i-1])%2;,特别的对于a[1]=(a[i]+a[N])%2
M最大10亿显然朴素法不可取,由于是看专题进来的所以直接想的就是矩阵= =
假设第零次的数组为原始01串(a,b,c,d)
则第一次 ((a+d)%2,(b+a)%2,(c+b)%2,(d+c)%2)
   第二次 (((a+d)%2+(d+c)%2)%2,......)
不难构造出一个N*N的矩阵,第i列的第i和i-1个元素置为1其余元素置0即可。
然后计算出这个转移矩阵的N次幂后再与原始行矩阵相乘得到答案。
此处还用到了同余定理 (A+B)%M=(A%M+B%M)%M;
 
 
#include<bits/stdc++.h>
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 矩阵快速幂的更多相关文章

  1. HDU 2855 (矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...

  2. HDU 4471 矩阵快速幂 Homework

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...

  3. HDU - 1575——矩阵快速幂问题

    HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973.  Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...

  4. hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...

  5. 随手练——HDU 5015 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5015 看到这个限时,我就知道这题不简单~~矩阵快速幂,找递推关系 我们假设第一列为: 23 a1 a2 ...

  6. 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} ...

  7. How many ways?? HDU - 2157 矩阵快速幂

    题目描述 春天到了, HDU校园里开满了花, 姹紫嫣红, 非常美丽. 葱头是个爱花的人, 看着校花校草竞相开放, 漫步校园, 心情也变得舒畅. 为了多看看这迷人的校园, 葱头决定, 每次上课都走不同的 ...

  8. HDU 5950 矩阵快速幂

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. hdu 1757 矩阵快速幂 **

    一看正确率这么高,以为是水题可以爽一发,结果是没怎么用过的矩阵快速幂,233 题解链接:点我 #include<iostream> #include<cstring> ; us ...

随机推荐

  1. .NET截取指定长度字符超出部分以"..."代替

    /// <summary> /// 将指定字符串按指定长度进行剪切, /// </summary> /// <param name= "Str "&g ...

  2. 集成tomcat插件到eclipse

    Eclipse中Tomcat的配置及简单例子 环境: 系统: Windows 7 64位专业版 Eclipse: Eclipse Java EE IDE for Web Developers.     ...

  3. Ora-1157 ora-1110错误解决案例一枚

    1.数据库打开报错如下: SQL> alter database open; alter database open * ERROR at line 1: ORA-01157: cannot i ...

  4. ArcGIS API for JavaScript开发笔记(一)——ArcGIS for Javascript API 3.14本地部署

    堪称史上最详细的< ArcGIS forJavascript API 3.14本地部署>文档,有图有真相~~~ ---------环境:Windows server 2012R2,IIS ...

  5. collision weaknesses

    15.1. hashlib — Secure hashes and message digests — Python 3.5.6 documentation https://docs.python.o ...

  6. Oracle数据库的经典问题 snapshot too old是什么原因引起的

    Oracle数据库的经典问题 snapshot too old是什么原因引起的 ORACLE经典错误求解:ORA-1555错误(Snapshot too old ) - ... 书上说是因为the r ...

  7. JS操作符转化数字

    在Node.js源代码里,随处可见使用各种符号处理字符串为数字的.可能由于不同人编写,使用的风格也各有不同. 基本上有下面几种. 将字符串转化为数字 + 将一个数字的字符串转化为数字很简单的一种做法就 ...

  8. 1124 Raffle for Weibo Followers[简单]

    1124 Raffle for Weibo Followers(20 分) John got a full mark on PAT. He was so happy that he decided t ...

  9. cocos代码研究(21)Widget子类TextField学习笔记

    基础理论 一个接受用户输入的widget. 输入文本的渲染基于TextFieldTTF. 如果你想用系统控制行为,请使用EditBox来替代.继承自 Widget. 代码实践 //与占位符有关void ...

  10. uva 1456

    这题说的是 给了 n 个 点 然后每个点 都有 相应的概率,你要将这n个点划分成w个集合使得 下面定义的这种算法 得到的 值最小 n1 是集合一的 个数  是 集合一内的每个点的概率和, 下面是分成两 ...