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): 2265    Accepted Submission(s): 1146

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

Source

HDU 8th Programming Contest Site(1)

Recommend

lcy   |   We have carefully selected several similar problems for you:  1757 1588 2604 2256 2294

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")

#define eps 1e-8
typedef __int64 ll;

using namespace std;

#define INF 0x3f3f3f3f
#define N 101
int n;

struct mat{
   mat(){memset(a,0,sizeof(a));}
   int a[N][N];
   mat operator *(mat b)
   {
       mat c;
       for(int i=0;i<n;i++)
          for(int j=0;j<n;j++)
             for(int k=0;k<n;k++)
                 c.a[i][j]=(c.a[i][j]+a[i][k]*b.a[k][j])%2;
       return c;
   }
};

mat fdd(mat s,int m)
{
    mat ss;
    for(int i=0;i<n;i++) ss.a[i][i]=1;
    while(m)
    {
        if(m&1) ss=ss*s;
        s=s*s;
        m>>=1;
    }
    return ss;
}

int main()
{
    int i,j;
    char c[1000];
    int m;
    while(~scanf("%d",&m))
    {
        scanf("%s",c);
        mat s;
        n=strlen(c);
        for(i=0;i<n;i++)
            s.a[i][0]=c[i]-'0';
        mat ss;
        ss.a[0][0]=ss.a[0][n-1]=1;
        for(i=1;i<n;i++)
            ss.a[i][i]=ss.a[i][i-1]=1;
        ss=fdd(ss,m);
        s=ss*s;
        for(i=0;i<n;i++)
            printf("%d",s.a[i][0]);
      printf("\n");
    }
    return 0;
}

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

时间: 2024-11-13 20:52:25

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