最近开始学习矩阵快速幂相关的题目,感觉矩阵好强大……
Kiki & Little Kiki 2
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2518 Accepted Submission(s): 1313
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)
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.
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
题意:有一个环,有n盏灯,如果前一盏灯是亮的,那么下一秒它的下一盏灯就会改变。求m秒后的状态。
题解:构造矩阵比较难。可以看出来要想知道第n盏灯的下一秒的状态,那么就需要前一盏灯的状态以及自身的状态,其他灯无所谓。
所以构造矩阵为
1 0 0.....1 a0 a1
1 1 0 0.... * a1 = a2
0 1 1 0... ..... ..
...........1 1 .... ...
ac code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define sd1(a) scanf("%lf",&a)
#define sd2(a,b) scanf("%lf%lf",&a,&b)
#define ss1(s) scanf("%s",s)
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d %d\n",a,b)
#define mset(a,b) memset(a,b,sizeof(a))
#define forb(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a;i<=b;i++)
#define LL long long
#define eps 1e-8
#define INF 0x3f3f3f3f
//#define mod 1000000007
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
using namespace std;
const int mod=2;
struct Matrix
{
LL m[100][100];
}E,A;
void init()
{
mset(E.m,0);
for(int i=0;i<100;i++)
{
E.m[i][i]=1;
}
return ;
}
Matrix Mul(Matrix a,Matrix b,int n)
{
Matrix ans;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
ans.m[i][j]=0;
for(int k=0;k<n;k++)
{
ans.m[i][j]^=(a.m[i][k]&b.m[k][j]);
}
}
}
return ans;
}
Matrix quick(Matrix a,int k,int n)
{
Matrix t=a,d=E;
while(k)
{
if(k&1)
d=Mul(d,t,n);
k>>=1;
t=Mul(t,t,n);
}
return d;
}
int main()
{
int n,m;
init();
while(~si1(m))
{
int a[105];
char s[105];
scanf("%s",s);
n=strlen(s);
for(int i=0;i<strlen(s);i++)
{
a[i]=s[i]-'0';
}
mset(A.m,0);
A.m[0][n-1]=A.m[0][0]=1;
for(int i=1;i<n;i++)
{
A.m[i][i-1]=A.m[i][i]=1;
}
Matrix ans=quick(A,m,n);
for(int i=0;i<n;i++)
{
int temp=0;
for(int j=0;j<n;j++)
{
temp^=(ans.m[i][j]&a[j]);
}
printf("%d",temp%2);
}
printf("\n");
}
return 0;
}