Codeforces 233C Partial Sums(特殊三角矩阵压缩)

21 篇文章 0 订阅
11 篇文章 0 订阅
C. Partial Sums
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You've got an array a, consisting of n integers. The array elements are indexed from 1 to n. Let's determine a two step operation like that:

  1. First we build by the array a an array s of partial sums, consisting of n elements. Element number i (1 ≤ i ≤ n) of array s equals . The operation x mod y means that we take the remainder of the division of number x by number y.
  2. Then we write the contents of the array s to the array a. Element number i (1 ≤ i ≤ n) of the array s becomes the i-th element of the array a (ai = si).

You task is to find array a after exactly k described operations are applied.

Input

The first line contains two space-separated integers n and k (1 ≤ n ≤ 2000, 0 ≤ k ≤ 109). The next line contains n space-separated integers a1, a2, ..., an — elements of the array a (0 ≤ ai ≤ 109).

Output

Print n integers  — elements of the array a after the operations are applied to it. Print the elements in the order of increasing of their indexes in the array a. Separate the printed numbers by spaces.

Examples
Input
3 1
1 2 3
Output
1 3 6
Input
5 0
3 14 15 92 6
Output
3 14 15 92 6


题目大意:

    有一个长度为N的数组,每次用它的前缀和数组代替它,求执行K次操作后的数组。


解题思路:

    非常自然想到矩阵快速幂。不过,如果使用裸的矩阵快速幂的话时间复杂度是O(N^3 * log K),毫无疑问会TLE。于是我们想到这个矩阵非常特殊,可以使用它的一些性质来降低矩阵乘法的时间复杂度。

    我们假设数组长度为3先手动推一下,就可以发现执行1、2、3次操作的操作矩阵为

1 1 1   1 2 3   1 3 6
0 1 1   0 1 2   0 1 3
0 0 1   0 0 1   0 0 1
    非常明显,这个矩阵主对角线下方全为0,上方每一条和主对角线平行的斜线上的值都是相等的。所以对于整个矩阵真正有意义的值只是最上面一行。所以我们就可以考虑把矩阵压缩成一个1*N的矩阵。所以我们要解决的核心问题就是实现压缩后矩阵的乘法运算。

    利用每条与主对角线平行的斜线上的值是相等这一性质,我们就可以按照普通矩阵乘法的计算方式找到在压缩后矩阵对应值的位置写乘法(详情请见代码)。由于我们只需要求一个1*N的矩阵,所以乘法的时间复杂度就从O(N^3)减少到了O(N^2),总复杂度将为O(N^2 * log K),也就可以在规定时间通过这道题了。


AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define LL long long
#define mem(a,b) memset((a),(b),sizeof(a));
#define MOD 1000000007

const int maxn=2000+3;
int N,K;
LL a[maxn],h[maxn],b[maxn],temp[maxn];

void mul(LL *A,LL *B)//矩阵A乘矩阵B,结果储存在A中
{
    mem(temp,0);
    for(int i=1;i<=N;++i)
        for(int j=1;j<=i;++j)
            temp[i]=(temp[i]+A[j]*B[i-j+1])%MOD;
    for(int i=1;i<=N;++i)
        A[i]=temp[i];
}

void pow(LL n)//矩阵快速幂,结果储存在b中
{
    for(int i=1;i<=N;++i)//初始状态矩阵最上层全为1
        h[i]=1;
    mem(b,0);
    b[1]=1;//单位矩阵第一行
    while(n>0)
    {
        if(n&1)
            mul(b,h);
        mul(h,h);
        n>>=1;
    }
}

int main()
{
    scanf("%d%d",&N,&K);
    for(int i=1;i<=N;++i)
        scanf("%lld",&a[i]);
    pow(K);//求出K次操作的矩阵第一行
    for(int i=1;i<=N;++i)
    {
        LL x=0;
        for(int j=1;j<=i;++j)//计算每一个值操作K次后的值
            x=(x+a[i-j+1]*b[j])%MOD;
        printf("%lld ",x);
    }
    putchar('\n');
    
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值