Codeforces 224E.Partial Sums(组合数学+费马小定理,重复 k 次前缀和)

5 篇文章 0 订阅
4 篇文章 0 订阅

E.Partial Sums

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 nelements. 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

题意:有一个数组,输出对这个数组做 k 次前缀和后的  数组。

思路:

多写几项你就会发现系数是有规律的,是组合数。。然后将系数写出一个矩阵,会更加明显

\begin{pmatrix} a_{1}:& C_{k - 1}^{0} & 0 & 0 &0 &0 \\ a_{2}:& C_{k}^{1} &C_{k - 1}^{0} & 0 & 0 &0 \\ a_{3}:& C_{k + 1}^{2}& C_{k}^{1} &C_{k - 1}^{0} & 0 & 0 \\ a_{4}: & C_{k + 2}^{3} &C_{k + 1}^{2}& C_{k}^{1} &C_{k - 1}^{0} & 0 \\ a_{5}: & C_{k + 3}^{4} & C_{k + 2}^{3} &C_{k + 1}^{2}& C_{k}^{1} &C_{k - 1}^{0} \\ \end{pmatrix}

很显然,需要做的就是求出每一行第一个的系数就可以了。剩下了每一次都复制上一行的就可以了。

组合数有公式:C_{n}^{m} = C_{n - 1}^{m} + C_{n - 1}^{m -1} ,对每一行第一个的系数,现在有的是 C_{n - 1}^{m - 1}(在<i - 1,j>位置),所以只需要 n ^ 2 处理出 C_{n - 1}^{m} 放在矩阵的第0列就可以了。然后 n ^ 2 算出每一个数。

Code:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define fi first
#define se second
#define CLR(a) while(!(a).empty()) a.pop()

using namespace std;

const int maxn = 2100;
const LL mod = 1e9 + 7;
LL c[maxn][maxn];
LL a[maxn];
LL n,k;

LL pow(LL a, LL n) {  //快速幂 a^n % p
    LL ans = 1;
    while(n) {
        if(n & 1)
            ans = ans * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return ans;
}

LL niYuan(LL a) { //费马小定理求逆元
    return pow(a, mod - 2);
}


int main() {
    while(cin >> n >> k){
        for(int i = 1;i <= n;++ i) cin >> a[i];
        c[1][0] = 1;
        for(int i = 2;i <= 2000;++ i){
            LL tmpa = 1,tmpb = 1;
            for(int j = 1;j < i;++ j){
                (tmpa *= (k + i - 3 - (j - 1))) %= mod;
                (tmpb *= j) %= mod;
            }
            c[i][0] = tmpa * niYuan(tmpb) % mod;
        }
        c[1][1] = 1LL;
        for(int i = 2;i <= n;++ i){
            for(int j = i;j > 1;-- j)
                c[i][j] = c[i - 1][j - 1];
            c[i][1] = (c[i][0] + c[i - 1][1]) % mod;
        }
        for(int i = 1;i <= n;++ i){
            LL ans = 0;
            for(int j = 1;j <= i;++ j)
                ans = (ans + c[i][j] * a[j] % mod) % mod;
            cout << ans << " ";
        }
        cout << endl;
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值