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("
    ");
        }
    }
  • 相关阅读:
    redis学习笔记(三)——redis的命令大全总结
    redis学习笔记(二)——java中jedis的简单使用
    redis学习笔记(一)——windows下redis的安装与配置
    快速排序
    SpringMvc实现批量删除,使用post传值一直报404错误
    Bootstrap-table 显示行号
    选择排序
    冒泡排序
    org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class
    Jquery Validate动态添加和删除校验规则
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7250778.html
Copyright © 2011-2022 走看看