hdu 2276 Kiki & Little Kiki 2矩阵快速幂

Kiki & Little Kiki 2

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2300    Accepted Submission(s): 1175

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

题意:告诉一个01序列表示灯的开关情况,0就是关,1就是开,现在要操作次,如果一个数左边是1就改变这个数的状态,第一个数左边的数是最右边那个数。

分析:最开始第一反应是找循环节,但是直接存起来会MLE,找到后再算会TLE,赛后才知道是举证快速幂。对于每个数,操作时只与当前以及它左边的数有关,跟其他数没有任何关系,于是构造矩阵时,就能想到只有当前位置和它左边位置有关,只有这两个位置的数是1,其他都是0。

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cmath>
typedef __int64 ll;
using namespace std;

struct matrix
{
    int f[102][102];
}s,t;

char str[109];
int mp[109][109];
int ss[109];

matrix mul(matrix a,matrix b,int len)
{
    matrix c;
    memset(c.f,0,sizeof c.f);

    for(int i=0;i<len;i++)
    {
        for(int j=0;j<len;j++)
        {
            for(int k=0;k<len;k++)
            {
                c.f[i][j]=(c.f[i][j]+a.f[i][k]*b.f[k][j])%2;
            }
        }
    }
    return c;
}

matrix fun(matrix a,ll n,int len)
{
    matrix b;

    for(int i=0;i<len;i++)
        for(int j=0;j<len;j++)
        if(i==j) b.f[i][j]=1;
         else b.f[i][j]=0;

         while(n)
         {
             if(n&1)
                b=mul(b,a,len);
             a=mul(a,a,len);
             n>>=1;
         }
         return b;
}

int main()
{
    ll n;
    while(~scanf("%I64d",&n))
    {
        scanf("%s",str);
        int len=strlen(str);

        memset(s.f,0,sizeof s.f);
        for(int i=0;i<len;i++)//构造矩阵
            if(i==0 || i==len-1) s.f[i][0]=1;
        for(int j=1;j<len;j++)
            for(int i=0;i<len;i++)
                if(i==j || i==j-1) s.f[i][j]=1;

        for(int i=0;i<len;i++)
            ss[i]=str[i]-'0';

            t=fun(s, n ,len);

            for(int i=0;i<len;i++)
            {
                int ans=0;
                for(int j=0;j<len;j++)
                {
                    ans=(ans+ss[j]*t.f[j][i])%2;
                }
                printf("%d",ans);
            }
            puts("");

    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-06 11:52:08

hdu 2276 Kiki & Little Kiki 2矩阵快速幂的相关文章

HDU 5171 GTY&#39;s birthday gift 矩阵快速幂

点击打开链接 GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 225    Accepted Submission(s): 78 Problem Description FFZ's birthday is coming. GTY wants to give a gift to ZZF. He as

HDU 4965 Fast Matrix Calculation(矩阵快速幂)

HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次,可以变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个只有6x6,就可以用矩阵快速幂搞了 代码: #include <cstdio> #include <cstring> const int N = 1005; const int M = 10; int n, m; int A[N][M], B[M][N], C[M][M], CC[N]

HDU 2256 Problem of Precision (矩阵快速幂)

HDU 2256 Problem of Precision (矩阵快速幂) ACM 题目地址:HDU 2256 Problem of Precision 题意: 给出一个式子,求值. 分析: 推起来最后那步会比较难想. 具体过程见: 表示共轭只听说过复数的和图的... 这构题痕迹好明显... 跟基友开玩笑说:如果遇到这种题,推到Xn+Yn*sqrt(6)这步时,打表最多只能打到10就爆int了,这是输出正解和Xn,说不定眼神好能发现ans = Xn * 2 - 1呢.= =... 代码: /*

hdu 4965 Fast Matrix Calculation(矩阵快速幂)2014多校训练第9场

Fast Matrix Calculation                                                                   Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description One day, Alice and Bob felt bored again, Bob knows Ali

hdu 1757 A Simple Math Problem 矩阵快速幂

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 Lele now is thinking about a simple function f(x).If x < 10 f(x) = x.If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);And ai(0<=i<=9) can only be 0 or 1 .Now, I w

HDU 2256 Problem of Precision(矩阵快速幂)+ HDU 4565

HDU 2256 题意: 计算?(2√+3√)2n?mod1024 思路: ∵f(n)=(2√+3√)2n=(5+26√)n=An+Bn?6√ ∴f(n?1)=An?1+Bn?1?6√ 又∵f(n)=(5+26√)?f(n?1) ∴f(n)=(5?An?1+12?Bn?1)+(2?An?1+5?Bn?1)?6√ 所以递推矩阵就是: (52125)?(An?1Bn?1)=(AnBn) A1=5,B1=2. 然后套矩阵快速幂模板即可求出An,Bn. 又∵(5+26√)n=An+Bn?6√ ∴(5?2

hdu 4565 So Easy! (共轭构造+矩阵快速幂)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4565 题目大意: 给出a,b,n,m,求出的值, 解题思路: 因为题目中出现了开根号,和向上取整后求余,所以用矩阵快速幂加速求解过程的时候,会产生误差,就很自然地想到了凑数,因为(a-1)^2<b<a^2,得出0<a-sqrt(b)<1,则无论n取多大,(a-sqrt(b))^n都是小于1的,(a-sqrt(b))^n 与 (a+sqrt(b))^n共轭,两者展开后会相互抵销,所以(

HDU 3292 【佩尔方程求解 &amp;&amp; 矩阵快速幂】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292 No more tricks, Mr Nanguo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 587    Accepted Submission(s): 400 Problem Description Now Sailormoon

HDU 2256 Problem of Precision 数论矩阵快速幂

题目要求求出(√2+√3)2n的整数部分再mod 1024. (√2+√3)2n=(5+2√6)n 如果直接计算,用double存值,当n很大的时候,精度损失会变大,无法得到想要的结果. 我们发现(5+2√6)n+(5-2√6)n是一个整数(2√6的偶数次幂总会正负抵消掉),并且(5-2√6)n是小于1的.所以我们就只需要求出Sn-1即可.令 An=(5+2√6)n;  Bn=(5-2√6)n. Sn=An+Bn     Sn为整数. Sn*((5+2√6)+(5-2√6))=Sn*10 Sn*

hdu 4965 Fast Matrix Calculation(矩阵快速幂)

题意: 给你一个N*K的矩阵A和一个K*N的矩阵B,设矩阵C=AB,M=C^(N*N),矩阵Mmod6后,所有数的和是多少 思路: 刚开始我是直接计算的,开了一个1000*1000的矩阵,结果直接爆掉了 后来看了看题解,发现想的很巧妙 观察 M=ABABAB....AB,AB每次都是1000*1000,可是BA确是6*6的 那么 M=A(BABABA..BA)B,我们让BA进行矩阵快速幂就不会爆掉了 M=A(BA)^(N*N-1)B,最后计算一下就好了 代码: #include <iostrea