首页 > 其他分享 >hdu:Kiki & Little Kiki 2(矩阵快速幂)

hdu:Kiki & Little Kiki 2(矩阵快速幂)

时间:2023-01-25 10:34:52浏览次数:59  
标签:hdu matrix int light Little lights state Kiki

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.

 

输入样例

1
0101111
10
100000001

输出样例

1111000
001000010

二维矩阵快速幂化一维
#include<bits/stdc++.h>
using namespace std;
const int N=110,mod=2;
struct matrix
{
    int m[N][N];
};
matrix muti(matrix a,matrix b,int l)
{
    matrix c;
    for(int i=1;i<=l;++i)
      for(int j=1;j<=l;++j)
      c.m[i][j]=0;
    for(int i=1;i<=l;++i)
      for(int j=1;j<=l;++j)
        {for(int z=1;z<=l;++z)
        c.m[i][j]+=(a.m[i][z]*b.m[z][j])%mod;
        c.m[i][j]%=mod;
        }
    return c;
}
matrix pow_ma(matrix a,int k,int l)
{
    if(k==1) return a;
    matrix ans;
    ans=pow_ma(muti(a,a,l),k/2,l);
    if(k%2) ans=muti(ans,a,l);
    return ans;
}
int main()
{
    int n;
    char ch[110];
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    while(cin>>n)//读入用cin>>n
    {
      cin>>ch;
      if(n==0) {cout<<ch<<'\n';continue;}
      int l=strlen(ch);
     // cout<<l<<endl;
      matrix ans;
      for(int i=1;i<=l;++i)
        for(int j=1;j<=l;++j)
          ans.m[i][j]=0;
      for(int i=1;i<=l;++i)
      {
          if(i==1) {ans.m[i][i]=1,ans.m[i][l]=1;}
          else ans.m[i][i]=1,ans.m[i][i-1]=1;
      }
      ans=pow_ma(ans,n,l);
      int t;
      for(int i=1;i<=l;++i)
      {
        t=0;//t每次加之前赋值为0
       for(int j=1;j<=l;++j)
        t+=ans.m[i][j]*(ch[j-1]-'0')%mod;//ch里应该用j
        cout<<t%mod;
      }
      cout<<'\n';
    }
    return 0;
}

 

标签:hdu,matrix,int,light,Little,lights,state,Kiki
From: https://www.cnblogs.com/ruoye123456/p/17066714.html

相关文章