F - Experienced Endeavour 矩阵快速幂

Alice is given a list of integers by Bob and is asked to generate a new list where each element in the new list is the sum of some other integers in the original list. The task is slightly more involved, as Bob also asks Alice to repeat this several times before giving him the result. Help Alice automate her task. Input The first line of the input is t (1 ≤ t ≤ 10), the number of cases to follow. Each case is in the following format: n r a0 a1 . . . an−1 x0 b0,0 b0,1 . . . b0,x0−1 x1 b1,0 b1,1 . . . b1,x1−1 . . . xn−1 bn−1,0 bn−1,1 . . . bn−1,xn−1−1 Each case begins with the integer n (1 ≤ n ≤ 50), which is the number of elements in the list of integers that Alice is given. The integer r (1 ≤ r ≤ 109 ) is the number of times these operations are to be repeated on a list before returning the result. The values are the nonnegative integers in the original list. Then n lines follow that define how Alice will generate a new list from a previous one. Each of these lines are in the form: xi bi,0 bi,1 . . . b1,xi This line defines the value of the i-th element in the new list to be the sum of elements: abi,0 , abi,1 , . . . , ab1,xi−1 Output The output consists of t lines, one line for each test case listing the final list of integers modulo 1000 in the form: c0 c1 . . . cn−1 Sample Input 2 2 2 1 2 2 0 1 1 1 2 4 507 692 2 0 1 1 1 Sample Output 5 2 275 692

 

 

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<string>
using namespace std;
typedef long long  LL;
typedef unsigned long long ULL;
#define MAXN 51
#define MOD 10000007
#define INF 1000000009
const double eps = 1e-9;
/*
矩阵快速幂 列出状态转移方程
这个题读了半天...
*/
int T, n, k;
int l[MAXN],res[MAXN];
struct Mat
{
    int a[MAXN][MAXN];
    Mat()
    {
        memset(a, 0, sizeof(a));
    }
    Mat operator* (const Mat& rhs)const
    {
        Mat ans;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                for (int t = 0; t < n; t++)
                    ans.a[i][j] = (ans.a[i][j] + a[i][t] * rhs.a[t][j]) % 1000;
            }
        }
        return ans;
    }
};
Mat fpow(Mat m, int b)
{
    if (b <= 0) return m;
    Mat ans;
    for (int i = 0; i < n; i++)
        ans.a[i][i] = 1;
    while (b != 0)
    {
        if (b & 1)
            ans = m*ans;
        m = m * m;
        b = b / 2;
    }
    return ans;
}
int main()
{
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d", &n, &k);
        for (int i = 0; i < n; i++)
            scanf("%d", &l[i]);
        Mat M;
        int x, tmp;
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &x);
            while (x--)
            {
                scanf("%d", &tmp);
                M.a[i][tmp] = 1;
            }
        }

        M = fpow(M, k );
        memset(res, 0, sizeof(res));
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                res[i] = (res[i] + M.a[i][j] * l[j])%1000;
            }
        }
        for (int i = 0; i < n; i++)
        {
            if (i) printf(" ");
            printf("%d", res[i]);
        }
        printf("\n");
    }
}

 

转载于:https://www.cnblogs.com/joeylee97/p/7250778.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值