Problem:


Score : 100 points

Problem Statement

There are N children, numbered 1, 2, \ldots, N.

They have decided to share K candies among themselves.
Here, for each i (1 \leq i \leq N), Child i must receive between 0 and a_i candies (inclusive).
Also, no candies should be left over.

Find the number of ways for them to share candies, modulo 10^9 + 7.
Here, two ways are said to be different when there exists a child who receives a different number of candies.

Constraints

All values in input are integers.

1 \leq N \leq 100

0 \leq K \leq 10^5

0 \leq a_i \leq K

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 \ldots a_N

Output

Print the number of ways for the children to share candies, modulo 10^9 + 7.

Sample Input 1

3 4
1 2 3

Sample Output 1

5

There are five ways for the children to share candies, as follows:

(0, 1, 3)

(0, 2, 2)

(1, 0, 3)

(1, 1, 2)

(1, 2, 1)

Here, in each sequence, the i-th element represents the number of candies that Child i receives.

Sample Input 2

1 10
9

Sample Output 2

0

There may be no ways for the children to share candies.

Sample Input 3

2 0
0 0

Sample Output 3

1

There is one way for the children to share candies, as follows:

(0, 0)

Sample Input 4

4 100000
100000 100000 100000 100000

Sample Output 4

665683269

Be sure to print the answer modulo 10^9 + 7.



Sample input:
3 4
1 2 3


Sample output:
5

Submission status: Runtime Error

Solution code:
N,K = map(int, input().split())
A = list(map(int, input().split()))

dp = [[0] * (K+10) for _ in range(N+1)]
dp[0][0] = 1
for i in range(N):
    for j in range(K,0,-1):
        dp[i+1][j] += dp[i][j]
        dp[i+1][j] %= MOD
        if j + a[i] + 1 <= K+1:
            dp[i+1][j+a[i]+1] = (dp[i+1][j+a[i]+1] - dp[i][j] + MOD) % MOD
    
    for j in range(K+1):
        dp[i+1][j+1] += dp[i+1][j]
        dp[i+1][j+1] %= MOD

print(dp[N][K])

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

n, k = map(int, input().split())
a = list(map(int, input().split()))
cs_a = list(np.cumsum(a)) #累積和

dp = [[0] * (n + 1) for i in range(k + 1)]

for i in range(k + 1):
    for j in range(1, n + 1):
        if i > cs_a[j - 1]:
            dp[i][j] = 0
        elif i == 0:
            dp[i][j] = 1
        elif i == 1:
            dp[i][j] = dp[i][j - 1] + min(1, a[j - 1])
        else:
            dp[i][j] = sum(dp[i - x][j - 1] for x in range(a[j - 1] + 1))
            
print(dp[k][n] % (10 ** 9 + 7))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
if k > sum(a):
    print(0)
    exit()

mod = 1000000007
dp = [[0 for _ in range(k+1)]for __ in range(n+1)]
for i in range(n+1):
    dp[i][0] = 1
    dp[0][i] = 0

for i in range(1,n+1):
    for j in range(1,k+1):
        if j > a[i-1]:
            if i is 1:
                break
        dp[i][j] = dp[i-1][j] + dp[i][j-1]
        if j > a[i-1]:
            dp[i][j] -= dp[i-1][j-1-a[i-1]]
        dp[i][j] %= mod
print(dp[n][k])

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
if k > sum(a):
    print(-1)
    exit()

mod = 1000000007
dp = [[0 for _ in range(k+1)]for __ in range(n+1)]
for i in range(n+1):
    dp[i][0] = 1
    dp[0][i] = 0

for i in range(1,n+1):
    for j in range(1,k+1):
        if j > a[i-1]:
            if i is 1:
                break
        dp[i][j] = dp[i-1][j] + dp[i][j-1]
        if j > a[i-1]:
            dp[i][j] -= dp[i-1][j-1-a[i-1]]
        dp[i][j] %= mod
print(dp[n][k])

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#include <map>

#define int long long

#define loop1(i,a,b) for(int i=a;i<b;i++)

using namespace std;
#define m 1000000007

signed main()
{
    // typedef tree<int,null_type,less<int>,rb_tree_tag,
    // tree_order_statistics_node_update> indexed_set;
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n,k;
    cin>>n>>k;
    // vector<pair<int,int>> v;
    int a[n];
    loop1(i1,0,n)
    {  
        cin>>a[i1];
        
    }
    pair<int,int> mat[n+1][k+1];
     loop1(i,0,n+1)
    {  
       loop1(j,0,k+1)
    {  
        
        mat[i][j]=make_pair(1,1);
    }
        
    }
    // ================
    loop1(j,1,k+1)
    {  
       loop1(i,1,n+1)
    {  int x=0;
        if(j==1){
			if(i==1 and a[0]>0){
				x=1;}
            
				
			else if(i>0 and a[i-1]>0 ){
				x=mat[i-1][j].first+1;}
				
			else if(i>0 and a[i-1]==0){
				x=mat[i-1][j].first;}
        }	
		else{
			int start=j-a[i-1];
			if(start<0){start=0;}
			if(start==0){
				x=mat[i-1][j].second;}
				
			else{
				x=mat[i-1][j].second-mat[i-1][start-1].second;}
        }
		mat[i][j]=make_pair(x%m,(mat[i][j-1].second+x)%m);
     
    }
        
    }
    cout<<mat[n][k].first;

    
}

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
#define vl vector<ll>
#define vvl vector<vector<ll> >
using namespace std;
int main(void){
    // Your code here!
    ll n,k; cin>>n>>k;
    vl a(n);
    for(int i=0;i<n;++i) cin>>a[i];
    vvl dp(n+1,vl(k+1,0));
    dp[0][0] = 1;
    for(int i=1;i<=n;++i){
        ll t = 0;
        for(int j=0;j<=k;j++){
            if(j <= a[i-1]) t += dp[i-1][j];
            else t += (dp[i-1][j]-dp[i-1][j-a[i-1]-1]);
            t %= 1000000007;
            dp[i][j] = t;
        }
    }cout<<dp[n][k]<<endl;
}

Submission status: Runtime Error

Solution code:
def wczytaj_liste(): 
    wczytana_lista = input()
    lista_znakow = wczytana_lista.split()
    ostateczna_lista = []
    for element in lista_znakow:
        ostateczna_lista.append(int(element))
    return ostateczna_lista 

def ciasteczka(): 
    N, K = wczytaj_liste()
    max_cukierkow = wczytaj_liste()
    przed = [1]
    for dziecko in max_cukierkow:
        po = [0]* min(len(przed)+ dziecko, K+1)
        for i in range(len(po)):
            for b in range(dziecko+1):
                j = i - b
                if  0 <= j < len(przed):
                    po[i] += przed[j]
                    if po[i] >= 10**9+7:
                        po[i] -= 10**9+7
        przed = po
    print(przed[K])
    
ciasteczka()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
ali = list(map(int,input().split()))
k= int(input())


def check(i, sum):
    # nこ決め終わったら、今までの和sumがkと等しいかを返す
    if(i == n):
        return sum==k
    
    # ali[i]をつかわない場合
    if(check(i+1,sum)):
        return True

    # ali[i]を使う場合
    if(check(i+1,sum+ali[i])):
        return True
    
    # ali[i]を使うつかわないに関わらずkが作れないのでfalseを返す
    return False

if(check(0,0)):
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
def comb(a,b):
      return fact[a]*inv[b]*inv[a-b]%mod

n,k = map(int, input().split())
a = list(map(int, input().split()))
fact = [1]
for i in range(n+k):
    fact.append(fact[-1]*(i+1)%mod)
inv = [1]*(n+k+1)
inv[n+k] = pow(fact[n+k],mod-2,mod)
for i in range(n+k)[::-1]:
    inv[i] = inv[i+1]*(i+1)%mod
f = [comb(i+n-1,n-1) for i in range(k+1)]
for i in a:
    for j in range(k-i)[::-1]:
        f[j+i+1] -= f[j]
        f[j+i+1] %= mod
print(f[k])

Submission status: Runtime Error

Solution code:
from numpy import *
N,K,*A=map(int,open(0).split())
d=zeros(K+1,int64)
d[0]=1
for a in A:
    d[a+1:]-=d.copy()[:-(a+1)]
    d=cumsum(d)%(10**9+7)
print(d[K])

Submission status: Runtime Error

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
from functools import reduce
import sys
import math
import bisect
import random 
import numpy as np 
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return list(map(float, sys.stdin.readline().split()))
def LS():return sys.stdin.readline().split()
def SR(n): return [list(sys.stdin.readline())[:-1] for i in range(n)]
def IR(n): return [int(sys.stdin.readline()) for i in range(n)]
def LIR(n): return [list(map(int, sys.stdin.readline().split())) for i in range(n)]
def LSR(n): return [list(map(list, sys.stdin.readline().split())) for i in range(n)]
_gcd = lambda x, y: _gcd(y, x%y) if (x%y) else y
_lcm = lambda x, y: x*y // _gcd(x, y)
def gcd(*numbers):
    return reduce(_gcd, numbers)
def lcm(*numbers):
    return reduce(_lcm, numbers)
sys.setrecursionlimit(1000000)
INF = float('inf')
mod = 1000000007
dire4 = [(1,0), (0,1), (-1,0), (0,-1)]
dire8 = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]

N, K = LI()
a = LI()
"""

dp = np.zeros(K+1, dtype=np.int64)
dp[0:a[0]+1] = 1

for i in range(1,N):
    acc = np.cumsum(dp)
    for j in range(K+1)[::-1]:
        if (j-a[i]<=0):
            dp[j] = acc[j]%mod
        else:
            dp[j] = (acc[j] - acc[j-a[i]-1])%mod

print(dp[K])

"""
dp = [0]*(K+1)

for i in range(K+1):
    if (i > a[0]):
        break
    dp[i] =  1

#acc = [0]*(K+1)


for i in range(1,N):
    #for i in range(K):
        #acc[i+1] = acc[i] + dp[i]
    #print(acc)
    for j in range(K+1)[::-1]:
        res = 0
        #res = acc[j] - acc[max(j-a[i-1], 0)]
        for k in range(a[i]+1):
            if (k>j):
                break
            res += dp[j-k]
        dp[j] = res%mod

print(dp[K])

Submission status: Runtime Error

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
from functools import reduce
import sys
import math
import bisect
import random 
import numpy as np 
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return list(map(float, sys.stdin.readline().split()))
def LS():return sys.stdin.readline().split()
def SR(n): return [list(sys.stdin.readline())[:-1] for i in range(n)]
def IR(n): return [int(sys.stdin.readline()) for i in range(n)]
def LIR(n): return [list(map(int, sys.stdin.readline().split())) for i in range(n)]
def LSR(n): return [list(map(list, sys.stdin.readline().split())) for i in range(n)]
_gcd = lambda x, y: _gcd(y, x%y) if (x%y) else y
_lcm = lambda x, y: x*y // _gcd(x, y)
def gcd(*numbers):
    return reduce(_gcd, numbers)
def lcm(*numbers):
    return reduce(_lcm, numbers)
sys.setrecursionlimit(1000000)
INF = float('inf')
mod = 1000000007
dire4 = [(1,0), (0,1), (-1,0), (0,-1)]
dire8 = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]
"""
N, K = LI()
a = LI()


dp = np.zeros(K+1, dtype=np.int64)
dp[0:a[0]+1] = 1

for i in range(1,N):
    acc = np.cumsum(dp)
    for j in range(K+1)[::-1]:
        if (j-a[i]<=0):
            dp[j] = acc[j]%mod
        else:
            dp[j] = (acc[j] - acc[j-a[i]-1])%mod

print(dp[K])

"""
dp = [0]*(K+1)

for i in range(K+1):
    if (i > a[0]):
        break
    dp[i] =  1

#acc = [0]*(K+1)


for i in range(1,N):
    print(dp)
    #for i in range(K):
        #acc[i+1] = acc[i] + dp[i]
    #print(acc)
    for j in range(K+1)[::-1]:
        res = 0
        #res = acc[j] - acc[max(j-a[i-1], 0)]
        for k in range(a[i]+1):
            if (k>j):
                break
            res += dp[j-k]
        dp[j] = res%mod

print(dp)

Submission status: Runtime Error

Solution code:
mod = 10**9+7
N,K = map(int,input().split())
a = list(map(int,input().split()))
dp = [[0]*(K+1) for _ in range(N+1)]
for n in range(N+1):
    dp[n][0] = 1

for n in range(N):
    for k in range(1,K+1):
        if k-1-a[i] >= 0:
            dp[n+1][k] = dp[n+1][k-1]-dp[n][k-1-a[n]]
        else:
            dp[n+1][k] = dp[n][k]+dp[n+1][k-1]
        dp[n+1][k] %= mod

print(dp[N][K])

Submission status: Runtime Error

Solution code:
# coding: utf-8
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def f2(n):

    dp = [[0] * n for _ in range(n)]
    s = [1] * n

    def dfs(i, j):  # from i to j
        dpi = dp[i]
        if dpi[j]:
            return(dpi[j])
        dpij = 1
        for u in U[i]:
            if u != j:
                dpij = dpij * (dfs(u, i) + 1) % M
        s[j] = s[j] * (dpij + 1) % M
        dpi[j] = dpij
        return(dpij)

    for i in range(n):
        for u in U[i]:
            dfs(u, i)
    print("\n".join([str(x) for x in s]))

N, M = map(int, input().split()) # 1 <= N <= 10^5, 2 <= M <= 10^9
U = [set() for _ in range(N)]
for x, y in [map(int, input().split()) for _ in range(N - 1)]:
    U[x - 1].add(y - 1)
    U[y - 1].add(x - 1)
f2(N)