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("
    ");
        }
    }
  • 相关阅读:
    简单站内HTML文件搜索程序
    用 PHP 使 Web 数据分析进入更高境界
    半小时教你学会正则表达式
    如何使用php开发高效的WEB系统
    PHP6将实现的几个特性/功能
    用php定制404错误页面,并发信给管理员的程序
    实用技巧:PHP截取中文字符串的问题
    Windows XP下全新安装Apache2,PHP5,MYSQL5,Zend的简单过程
    网络流—最大流(EdmondKarp算法)
    poj 1094 Sorting It All Out(拓扑排序)
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7250778.html
Copyright © 2011-2022 走看看