hdu 2276(矩阵快速幂)

Kiki & Little Kiki 2

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

Total Submission(s): 2301    Accepted Submission(s): 1176

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

/*
  能够把转换关系找出来的话,这题就是个比较裸的快速幂了
  用a[i]*代表上一层的a[i],通过推样例会发现如下规律:
  a[i]=(a[i]*+a[i-1]*)%2,其中a[1]=(a[1]*a[n]*)%2

  第一次敲快速幂,存做模板

*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <cmath>
#include <vector>
#include<cstdlib>
#define INF 1<<30
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
#define maxn 150
#define moo 2
char s[maxn];
int n,m;
struct Mat
{
    int a[maxn][maxn];
};
Mat res1;
Mat muti(Mat a,Mat b)
{
    Mat res;
    memset(res.a,0,sizeof(res.a));
    for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
    {
       for(int k=0;k<n;k++)
            res.a[i][j]=(res.a[i][j] + a.a[i][k]*b.a[k][j])%moo;
    }
    return res;
}
Mat pow(Mat res,int m)
{
    Mat ss;
    memset(ss.a,0,sizeof(ss.a));
    for(int i=0;i<n;i++)
        ss.a[i][i]=1;
    while(m)
    {
        if(m%2)
        ss=muti(ss,res);
        res=muti(res,res);
        m/=2;
    }
    return ss;
}
int main()
{
    int ans[maxn];
    Mat oo;
    while(~scanf("%d",&m))
    {
        scanf("%s",s);
        n=strlen(s);
        for(int i=0;i<n;i++)
        ans[i]=s[i]-'0';
        int ant[maxn];
        memset(ant,0,sizeof(ant));
        memset(res1.a,0,sizeof(res1.a));

        res1.a[0][0]=res1.a[n-1][0]=res1.a[n-1][n-1]=1;
        for(int i=0;i<n-1;i++)
            res1.a[i][i]=res1.a[i][i+1]=1;
        oo=pow(res1,m);

        for(int j=0;j<n;j++)
            for(int i=0;i<n;i++)
            ant[j]=(ant[j]+ans[i]*oo.a[i][j])%moo;
            for(int i=0;i<n;i++)
                cout<<ant[i];
            cout<<endl;
    }
    return 0;
}

版权声明:本文为博主原创文章,欢迎指教~

时间: 2024-08-05 05:15:12

hdu 2276(矩阵快速幂)的相关文章

HDU 2276 矩阵快速幂

Kiki & Little Kiki 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2650    Accepted Submission(s): 1393 Problem Description There are n lights in a circle numbered from 1 to n. The left of lig

HDU 4965 矩阵快速幂

顺手写了下矩阵类模板 利用到矩阵乘法的交换律 (A*B)^n == A * (B*A)^n-1 *B #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <utility> #include <stack> #includ

hdu 4965 矩阵快速幂 矩阵相乘性质

Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 170    Accepted Submission(s): 99 Problem Description One day, Alice and Bob felt bored again, Bob knows Alice is a gir

HDU 5895 矩阵快速幂+高次幂取模

HDU 5895 Mathematician QSC 题意:已知f(n)=2*f(n-1)+f(n-2), g(n)=∑f(i)²(0<=i<=n), 给出n,x,y,s, 求x^(g(n*y))%(s+1); 思路:OEIS查到了g(n)=f(n)*f(n+1)/2, f(n)可以用矩阵快速幂求得, 有一个定理可以用于高次幂取模 x^n %k=x^(n%phi(k)+phi(k)) %k, 此处phi(x)为欧拉函数,但是在对幂次取模时存在一个除2, 又因为(a/b)%k=(a%bk)/b,

hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10); 所求的是f(x)取m的模,而x,m,a[0]至a[9]都是输入项 初拿到这道题,最开始想的一般是暴力枚举,通过for循环求出f(x)然后再取模,但是有两个问题,首先f(x)可能特别大,其

HDU 2855 (矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ 解题思路: 题目挺吓人的.先把完整组合数+Fibonacci展开来. 利用Fibonacci的特性,从第一项开始消啊消,消到只有一个数: $S(0)=f(0)$ $S(1)=f(2)$ $S(2)=f(4)$ $S(n)=f(2*n)$ 这样矩阵快速幂就可以了,特判$n=0$时的情况. 快速幂矩阵

hdu 4549 (矩阵快速幂+费马小定理)

题意:已知F0=a,F1=b,Fn=Fn-1*Fn-2,给你a,b,n求Fn%1000000007的值 思路:我们试着写几组数 F0=a F1=b F2=a*b F3=a*b2 F4=a2*b3 F5=a3*b5 我们发现a,b的系数其实是斐波那契数列,我们只需用矩阵快速幂求出相应系数就行,但是 这个系数随着增长会特别大,这时我们需要利用费马小定理进行降幂处理 费马小定理 ap-1≡1(mod p) 代码: #include <iostream> #include <cmath>

HDU 3221 矩阵快速幂+欧拉函数+降幂公式降幂

装载自:http://www.cnblogs.com/183zyz/archive/2012/05/11/2495401.html 题目让求一个函数调用了多少次.公式比较好推.f[n] = f[n-1]*f[n-2].然后a和b系数都是呈斐波那契规律增长的.需要先保存下来指数.但是太大了.在这里不能用小费马定理.要用降幂公式取模.(A^x)%C=A^(x%phi(C)+phi(C))%C(x>=phi(C)) Phi[C]表示不大于C的数中与C互质的数的个数,可以用欧拉函数来求. 矩阵快速幂也不

hdu 2842(矩阵快速幂+递推)

题意:一个中国环的游戏,规则是一个木棒上有n个环,第一个环是可以随意放上或拆下的,剩下的环x如果想放上或拆下必须前一个环x-1是放上的且前x-2个环全部是拆下的,问n个环最少多少次操作可以全部拆掉. 题解:需要进行递推,首先第一步肯定是要拆第n个环保证操作次数最少,因为后面的环是否存在对前面的环不造成影响,而先拆前面的如果要拆后面的环还是要把前面的放上,f(n)表示拆掉前n个环需要的最少操作次数,先拆第n个要拆前n-2个再拆第n个,花费f(n-2)+1,然后这时是00-0010,要拆第n-1个需

hdu 4291(矩阵快速幂 + 循环节)

题意:求s s = g(g(g(n))) mod 1000000007 其中g(n) g(n) = 3g(n - 1) + g(n - 2) g(1) = 1 g(0) = 0 题解:普通的矩阵快速幂会超时,看到别人的题解是需要计算循环节得到小的MOD从而减小计算量.1000000007太大,需要计算更小的一个循环节,新技能get. #include <stdio.h> #include <string.h> struct Mat { long long g[3][3]; }ori