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:
- 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.
- 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 次前缀和后的 数组。
思路:
多写几项你就会发现系数是有规律的,是组合数。。然后将系数写出一个矩阵,会更加明显
很显然,需要做的就是求出每一行第一个的系数就可以了。剩下了每一次都复制上一行的就可以了。
组合数有公式: ,对每一行第一个的系数,现在有的是
(在<i - 1,j>位置),所以只需要 n ^ 2 处理出
放在矩阵的第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;
}