题目描述:
主要思路:
状态压缩。
因为题目1<<16 种情况最多,所以肯定存在不同的时间是相同的状态,采用状态压缩思想进行计算。
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1<<16;
typedef long long ll;
int book[N];
int n;
ll update(ll x)
{
ll ans=0;
for(int i=0;i<n;i++)
{
int j=(i-1+n)%n;
//计算i位置的灯在下一秒的亮暗情况
int xx=x>>i&1,yy=x>>j&1;
ans|=(xx^yy)<<i;
}
return ans;
}
void P(ll x)
{
for(int i=0;i<n;i++)
{
cout<<x%2<<endl;
x/=2;
}
}
int main()
{
std::ios::sync_with_stdio;
ll b;
cin>>n>>b;
ll a=0;
int x;
for(int i=0;i<n;i++) cin>>x,a|=x<<i;
memset(book,-1,sizeof(book));
book[a]=0;
for(ll i=1;;i++)
{
a=update(a);
if(i==b)
{
P(a);
break;
}
else if(book[a]==-1) book[a]=i;
else // 状态压缩环
{
ll cha=i-book[a];
ll m=(b-i)%cha;
while(m--) a=update(a);
P(a);
break;
}
}
return 0;
}