码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2276 Kiki & Little Kiki 2(矩阵快速幂)

时间:2015-08-25 19:37:27      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

Kiki & Little Kiki 2

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2265    Accepted Submission(s): 1146


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
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1757 1588 2604 2256 2294 
 

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")

#define eps 1e-8
typedef __int64 ll;

using namespace std;

#define INF 0x3f3f3f3f
#define N 101
int n;

struct mat{
   mat(){memset(a,0,sizeof(a));}
   int a[N][N];
   mat operator *(mat b)
   {
       mat c;
       for(int i=0;i<n;i++)
          for(int j=0;j<n;j++)
             for(int k=0;k<n;k++)
                 c.a[i][j]=(c.a[i][j]+a[i][k]*b.a[k][j])%2;
       return c;
   }
};

mat fdd(mat s,int m)
{
    mat ss;
    for(int i=0;i<n;i++) ss.a[i][i]=1;
    while(m)
    {
        if(m&1) ss=ss*s;
        s=s*s;
        m>>=1;
    }
    return ss;
}

int main()
{
    int i,j;
    char c[1000];
    int m;
    while(~scanf("%d",&m))
    {
        scanf("%s",c);
        mat s;
        n=strlen(c);
        for(i=0;i<n;i++)
            s.a[i][0]=c[i]-'0';
        mat ss;
        ss.a[0][0]=ss.a[0][n-1]=1;
        for(i=1;i<n;i++)
            ss.a[i][i]=ss.a[i][i-1]=1;
        ss=fdd(ss,m);
        s=ss*s;
        for(i=0;i<n;i++)
            printf("%d",s.a[i][0]);
      printf("\n");
    }
    return 0;
}
















版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 2276 Kiki & Little Kiki 2(矩阵快速幂)

标签:

原文地址:http://blog.csdn.net/u014737310/article/details/47981129

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!