HDU 2276 Kiki & Little Kiki 2

矩阵快速幂。

0 1-> 第二个数字会变成1

0 0-> 第二个数字会变成0

1 0-> 第二个数字会变成1

1 1-> 第二个数字会变成0

根据这四个特点,就可以写转移矩阵了。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-6;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar();
    x = 0;
    while(!isdigit(c)) c = getchar();
    while(isdigit(c)) { x = x * 10 + c - ‘0‘; c = getchar(); }
}

char s[105];
int len,m;

struct Matrix
{
    int A[105][105];
    int R, C;
    Matrix operator*(Matrix b);
};

Matrix X, Y, Z;

Matrix ch(Matrix a, Matrix b)
{
    Matrix c;
    memset(c.A, 0, sizeof(c.A));
    int i, j, kk;
    for (i = 1; i <= len; i++)
        for (j = 1; j <= len; j++)
        {
            if(a.A[i][j])
            {
                for (kk = 1; kk <= len; kk++)
                    c.A[i][kk] = (c.A[i][kk]+a.A[i][j] * b.A[j][kk])%2;

            }
        }
    return c;
}

void init()
{
    for(int i=1;i<=len;i++) Z.A[1][i] = s[i-1]-‘0‘;
    Z.R = 1; Z.C = len;

    memset(Y.A,0,sizeof Y.A);
    for(int i=1;i<=len;i++) Y.A[i][i]=1;
    Y.R = len; Y.C = len;

    memset(X.A,0,sizeof X.A);
    X.A[1][1]=1; X.A[len][1]=1;
    for(int i=2;i<=len;i++) X.A[i][i]=1, X.A[i-1][i]=1;

    X.R = len; X.C = len;
}

void work()
{
    while (m)
    {
        if (m % 2 == 1) Y = ch(Y,X);
        m = m >> 1;
        X = ch(X,X);
    }
    Z = ch(Z,Y);

    for(int i=1;i<=len;i++) printf("%d",Z.A[1][i]);
    printf("\n");
}

int main()
{
    while(~scanf("%d%s",&m,s))
    {
        len=strlen(s);
        init();
        work();
    }

    return 0;
}
时间: 2024-08-04 14:16:17

HDU 2276 Kiki & Little Kiki 2的相关文章

HDU 2276 Kiki &amp; Little Kiki 2 (位运算+矩阵快速幂)

HDU 2276 Kiki & Little Kiki 2 (位运算+矩阵快速幂) ACM 题目地址:HDU 2276 Kiki & Little Kiki 2 题意: 一排灯,开关状态已知,每过一秒:第i个灯会根据刚才左边的那个灯的开关情况变化,如果左边是开的,它就会变化,如果是关的,就保持原来状态.问m秒后的状态. 第1个的左边是最后一个. 分析: 转移不好想啊... 变化是这样的: 原来 左边 变化 1 1 0 1 0 1 0 1 1 0 0 0 然后想到 (~原来)^(左边)=变化

HDU 2147 kiki&#39;s game kiki的游戏(找P/N状态)

题意:给一个有n*m 个格子的棋盘,将一个硬币放在右上角一格,每次可以往左/下/左下移动一格,碰到不能移动的局面者输. 思路:找P/N状态.先将(n,1)归为P状态,那么能一步到达此位置的有3个位置,分别是其上/右/右上的格子.根据这个规律来找,在整个棋盘的格子上标上P和N.可以发现,棋盘上是有规律的,若提供的n和m皆为奇数,则先手输:否则先手赢. 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4

HDU 2276 &amp; FZU 1692 (循环同构优化+矩阵快速幂)

HDU 2276 题意: 给定一个01**字符串环**(2<=字符串长度<=100)然后进行m次的变换. 定义变换的规则为:如果当前位置i的左边是1(下标为0的左边是n-1),那么i就要改变状态0->1 , 1->0 比如当前的状态为100101那么一秒过后的状态为010111. 思路: 用公式表示变化状态其实就是: ai=(a(i+n?1)%n+ai)%2 换成位操作就是: ai=a(i+n?1)%n^ ai 于是我们可以建立一个变化矩阵: ??????????1100...00

HDU 2276 Kiki &amp; 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 li

NYOJ 300 &amp;&amp; hdu 2276 Kiki &amp; Little Kiki 2 (矩阵快速幂)

Kiki & Little Kiki 2 时间限制:5000 ms  |  内存限制:65535 KB 难度:4 描述 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 t

hdu 2276 Kiki &amp; 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 li

HDU - 2276 Kiki &amp; Little Kiki 2 矩阵快速幂

题目大意:给出一个由0,1组成的字符串,每一秒的时候,如果该位字符左边是1的话,那么该字符就要变换(由0变1,或者由1变0,第一个的左边是最后一个),问M秒后这个字符串的状态 解题思路:用0,1矩阵来表示变化,具体的请看代码,现在没法给出矩阵,后面会补的 #include<cstdio> #include<cstring> const int N = 110; char str[N]; struct Matrix{ int mat[N][N]; }A, B, tmp; int n,

HDU - 2276 Kiki &amp;amp; Little Kiki 2

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, tu

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