zoukankan      html  css  js  c++  java
  • F

    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("
    ");
        }
    }
  • 相关阅读:
    面试系列14 redis的过期策略都有哪些
    面试系列13 redis都有哪些数据类型
    面试系列12 redis和memcached有什么区别
    面试系列11 缓存是如何使用
    面试系列10 es生产集群的部署架构
    linux命令中的“<”和“|”是什么意思?
    如何征服面试官,拿到Offer [转]
    ddt框架优化(生成html报告注释内容传变量)
    python笔记31-使用ddt报告出现dict() -> new empty dictionary dict(mapping) 问题解决
    测试中 unittest.main(verbosity=1) 是什么意思
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7250778.html
Copyright © 2011-2022 走看看