• HDU


    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.
    Sample Input
    1
    0101111
    10
    100000001
    Sample Output
    1111000
    001000010
     
    思路:
    想到矩阵快速幂是关键.
    其实这个公式我推的时候也没有想到,但实在是不应该.
    s[i]=(s[i]+s[i-1])%2;
    代码:
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<ctime>
    
    #define fuck(x) cout<<#x<<" = "<<x<<endl;
    #define debug(a, x) cout<<#a<<"["<<x<<"] = "<<a[x]<<endl;
    #define ls (t<<1)
    #define rs ((t<<1)|1)
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int maxn = 108;
    const int maxm = 100086;
    const int inf = 0x3f3f3f3f;
    const ll Inf = 999999999999999999;
    const int mod = 2;
    const double eps = 1e-6;
    const double pi = acos(-1);
    
    struct Matrix{
        int mp[108][108];
    };
    Matrix mul(Matrix a,Matrix b,int n){
        Matrix ans;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                ans.mp[i][j]=0;
                for(int k=1;k<=n;k++){
                    ans.mp[i][j]+=a.mp[i][k]*b.mp[k][j];
                }
                ans.mp[i][j]%=mod;
            }
        }
        return ans;
    }
    Matrix q_pow(Matrix a,int b,int n){
        Matrix ans;
        memset(ans.mp,0,sizeof(ans.mp));
        for(int i=1;i<=n;i++){
            ans.mp[i][i]=1;
        }
        while (b){
            if(b&1){
                ans=mul(ans,a,n);
            }
            b>>=1;
            a=mul(a,a,n);
        }
        return ans;
    }
    
    int num[maxn];
    char s[maxn];
    int main() {
        int n,m;
        while (scanf("%d",&m)!=EOF){
            scanf("%s",s+1);
            n=strlen(s+1);
            for(int i=1;i<=n;i++){
                num[i]=s[i]-'0';
            }
    
            Matrix tmp;
            memset(tmp.mp,0,sizeof(tmp.mp));
            tmp.mp[1][1]=tmp.mp[1][n]=1;
            for(int i=2;i<=n;i++){
                tmp.mp[i][i]=tmp.mp[i][i-1]=1;
            }
            tmp=q_pow(tmp,m,n);
            for(int i=1;i<=n;i++){
                int ans=0;
                for(int j=1;j<=n;j++){
                    ans+=tmp.mp[i][j]*num[j];
                    ans%=mod;
                }
                printf("%d",ans);
            }
            printf("
    ");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    you must restart adb and eclipse的相关解决办法
    Qt slot中获取sender
    添加开机启动项
    Unreal开发HTC Vive程序,开启VR编辑模式
    Android弹出一项权限请求
    Unreal新建C++类或C++项目失败
    win10 设置C盘访问权限
    windows系统共享设置最顺的一次
    下载Qt安装包
    单例模式
  • 原文地址:https://www.cnblogs.com/ZGQblogs/p/10908695.html
Copyright © 2020-2023  润新知