LOJ#2799. 「CCC 2016」生命之环

题意

给你一个 \(n\)\(\rm 01\) 组成的环,每次操作之后每个位置为1当且仅当他的左右恰好有1个1.输出进行 \(T\) 次操作之后的环。

\(n\leq 10^5, T\leq 10^{15}\).

分析

  • 通过1~4步之内模拟可以得到结论:一个位置能够在 \(2^k\) 的操作之后为1当且仅当他的往左往右的 \(2^k\) 个位置的异或值为1.

  • 将数字拆成若干个 \(2^k\) 进行操作即可。

  • 总时间复杂度为 \(O(nlogT)\)

代码

#include<bits/stdc++.h>
using namespace std;
#define go(u) for(int i=head[u],v=e[i].to;i;i=e[i].lst,v=e[i].to)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define pb push_back
typedef long long LL;
inline int gi(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-48;ch=getchar();}
    return x*f;
}
template<typename T>inline bool Max(T &a,T b){return a<b?a=b,1:0;}
template<typename T>inline bool Min(T &a,T b){return b<a?a=b,1:0;}
const int N=1e5 +7;
LL n,T;
LL a[N],b[N];
char s[N];
int main(){
    scanf("%lld%lld",&n,&T);
    scanf("%s",s);
    rep(i,0,n-1) a[i]=s[i]-'0';
    for(int k=61;~k;--k)if(T&(1ll<<k)){
        memset(b,0,sizeof b);
        rep(i,0,n-1){
            b[((i+(1ll<<k))%n+n)%n]^=a[i];
            b[((i-(1ll<<k))%n+n)%n]^=a[i];
        }
        memcpy(a,b,sizeof a);
    }
    rep(i,0,n-1) printf("%d",a[i]);
    puts("");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/yqgAKIOI/p/10005398.html
ccc