Rectangular 阵快 speed 幂之 Kiki & Little Kiki 2

Meaning of the questions is: given a string of 01 series, every second, every position must be changed according to the lamp that lights have left the state, (the first was left as the last one) if the left is 1, then your own will change state the left is not 0, Q n string 01 seconds to change state

/////

First, we found that, A [2] = (a [1] + a [2])% 2 ;,

Matrix can be used to calculate the power of fast results.

We found

(1 + 1) ^ 1 2 and 1% as a result, and 1 * 1 1 1 & same result, so the multiplication function change on the line

Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1024;
char str[110];
int len;
struct node{
int a[101][101];
};
node a,ans,b;
void init()
{
///我们令f(i)=A*f(i-1)+B*f(i-2);下面举例A=3,B=2;
memset(a.a,0,sizeof(a.a));
for(int i=1;i<len;i++) a.a[i][i]=a.a[i][i-1]=1;
a.a[0][0]=a.a[0][len-1]=1;
}
node mat(node x,node y)
{
node c;
for(int i=0;i<len;i++)
for(int j=0;j<len;j++)
c.a[i][j]=0;
for(int i=0;i<len;i++)
for(int j=0;j<len;j++)
for(int k=0;k<len;k++)
c.a[i][j]=c.a[i][j]^(1ll*x.a[i][k]&y.a[k][j]);
// printf("yes\n");
return c;
}
int main()
{
ll n;
int T;
while(~scanf("%d%s",&n,str)){
len=strlen(str);
init();
for(int i=0;i<len;i++)///
for(int j=0;j<len;j++)///
b.a[i][j]=0;///
for(int i=0;i<len;i++)///这里就只是为下面快速幂提供用的
b.a[i][i]=1;
while(n){
if(n&1) b=mat(b,a);
a=mat(a,a);
n>>=1;
}

for(int i=0;i<len;i++){
ans.a[i][0]=str[i]-'0';
}
ans=mat(b,ans);
for(int i=0;i<len;i++)
cout<<ans.a[i][0];
cout<<endl;
}
return 0;
}

 

Guess you like

Origin www.cnblogs.com/hgangang/p/11506510.html