UVA 11551 Experienced Endeavour

Posted Fighting Heart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA 11551 Experienced Endeavour相关的知识,希望对你有一定的参考价值。

矩阵快速幂。

题意事实上已经告诉我们这是一个矩阵乘法的运算过程。

构造矩阵:把xi列的bij都标为1.

例如样例二:

技术分享

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;

long long const MOD = 1000;
int n, m;
long long a[50 + 5];

struct Matrix
{
    long long A[50 + 5][50 + 5];
    int R, C;
    Matrix operator*(Matrix b);
};

Matrix X, Y, Z;

Matrix Matrix::operator*(Matrix b)
{
    Matrix c;
    memset(c.A, 0, sizeof(c.A));
    int i, j, k;
    for (i = 1; i <= R; i++)
    for (j = 1; j <= b.C; j++)
    for (k = 1; k <= C; k++)
        c.A[i][j] = (c.A[i][j] + (A[i][k] * b.A[k][j]) % MOD) % MOD;
    c.R = R; c.C = b.C;
    return c;
}

void init()
{
    memset(X.A, 0, sizeof X.A);
    memset(Y.A, 0, sizeof Y.A);
    memset(Z.A, 0, sizeof Z.A);

    Y.R = n; Y.C = n;
    for (int i = 1; i <= n; i++) Y.A[i][i] = 1;

    X.R = n; X.C = n;
    for (int j = 1; j <= n; j++)
    {
        int xi; scanf("%d", &xi);
        for (int i = 1; i <= xi; i++)
        {
            int num; scanf("%d", &num); num++;
            X.A[num][j] = 1;
        }
    }

    Z.R = 1; Z.C = n;
    for (int i = 1; i <= n; i++) Z.A[1][i] = a[i];

}

void read()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld", &a[i]);
        a[i] = a[i] % MOD;
    }
}

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

    for (int i = 1; i <= n; i++)
    {
        printf("%lld", Z.A[1][i]);
        if (i<n) printf(" ");
        else printf("\n");
    }
}

int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        read();
        init();
        work();
    }
    return 0;
}

 

以上是关于UVA 11551 Experienced Endeavour的主要内容,如果未能解决你的问题,请参考以下文章

URAL 2089 Experienced coach Twosat

8 Traits of an Experienced Programmer that every beginner programmer should know

UVa11988 Broken Keyboard 损坏的键盘list

Immediate Decodability[UVA644](Trie树入门)

UVA11988:悲剧文本(模拟链表)

uva-188-枚举