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;
}

最新文章

  1. DVWA安装,ALMP环境搭建以及php版本转换
  2. htmlFormat
  3. linux下mv命令使用方法
  4. hdu.1254.推箱子(bfs + 优先队列)
  5. Alarm(硬件时钟) init
  6. C中的setjmp与longjmp
  7. jxl.dll操作总结
  8. matlab画棋盘格程序
  9. 简单fcgi程序
  10. C++ STL算法系列1---unique , unique_copy函数
  11. Spring MVC 基础笔记
  12. SQL 行转列的运用
  13. PAT 甲级 1002 A+B for Polynomials (25 分)
  14. axure元件库导入后重启程序元件库消失问题
  15. [pat]1045 Favorite Color Stripe
  16. topcoder srm 545 div1
  17. 几个OOD概念
  18. 028、HTML 标签3表单标签插入组件
  19. 第二个Sprint冲刺总结
  20. Different between Telnet/SSH/FTP

热门文章

  1. 字符串函数---atof()函数详解
  2. SQL的子查询操作
  3. SLF4J其实只是一个门面服务而已,他并不是真正的日志框架,真正的日志的输出相关的实现还是要依赖Log4j、logback等日志框架的。
  4. 2014年百度之星程序设计大赛 - 资格赛 1001 Energy Conversion
  5. XShell已经内置rz 直接从Windows拖文件进去终端
  6. JavaScript类库汇总
  7. 【Lua】模块与包
  8. cocos代码研究(9)ProgressTimer类学习笔记
  9. html canvas 圆弧
  10. 1、安装electron