Neural Network country (Codeforces 852B)
Tags: DP, matrices

Due to the recent popularity of the Deep learning new countries are starting to look like Neural Networks. That is, the countries are being built deep with many layers, each layer possibly having many cities. They also have one entry, and one exit point.There are exactly L layers, each having N cities. Let us look at the two adjacent layers L1 and L2. Each city from the layer L1 is connected to each city from the layer L2 with the traveling cost cij for , and each pair of adjacent layers has the same cost in between their cities as any other pair (they just stacked the same layers, as usual). Also, the traveling costs to each city from the layer L2 are same for all cities in the L1, that is cij is the same for , and fixed j.Doctor G. needs to speed up his computations for this country so he asks you to find the number of paths he can take from entry to exit point such that his traveling cost is divisible by given number M.

Input: The first line of input contains N (1 ≤ N ≤ 106), L (2 ≤ L ≤ 105) and M (2 ≤ M ≤ 100), the number of cities in each layer, the number of layers and the number that travelling cost should be divisible by, respectively.Second, third and fourth line contain N integers each denoting costs 0 ≤ cost ≤ M from entry point to the first layer, costs between adjacent layers as described above, and costs from the last layer to the exit point.

Output: Output a single integer, the number of paths Doctor G. can take which have total cost divisible by M, modulo 109 + 7.

Note: This is a country with 3 layers, each layer having 2 cities. Paths , and  are the only paths having total cost divisible by 13. Notice that input edges for layer cities have the same cost, and that they are same for all layers.

Sample Inputs:
2 3 13
4 6
2 1
3 4

Sample Outputs:
2

Solution (Python):
""""""
M = 1000000007

def mmod(x):
    return x % M

def populate(p_vec, nex_vec):
    """"""
    m = len(p_vec)
    return [mmod(sum([mmod(p_vec[(i - j) % m] * nex_vec[j]) for j in range(m)])) for i in range(m)]

def fast(p_vec, k):
    res = [0 for _ in range(len(p_vec))]
    res[0] = 1
    while k > 0:
        if k & 1:
            res = populate(res, p_vec)
        p_vec = populate(p_vec, p_vec)
        k >>= 1
    return res

def build_vec(m, cost):
    res = [0 for _ in range(m)]
    for c in cost:
        res[c % m] += 1
    return res
(n, l, m) = map(int, input().split())
start_cost = list(map(int, input().split()))
common_cost = list(map(int, input().split()))
end_cost = list(map(int, input().split()))
start_vec = build_vec(m, start_cost)
common_vec = build_vec(m, common_cost)
end_vec = build_vec(m, end_cost)
temp_vec = populate(start_vec, fast(common_vec, l - 2))
count = 0
for i in range(n):
    count = mmod(count + temp_vec[(-common_cost[i] - end_cost[i]) % m])
print(count)

Solution (Python):
import sys
M = 1000000007

def prod(x, y):
    z = [0 for _ in range(m)]
    for i in range(m):
        for j in range(m):
            z[(i + j) % m] += x[i] * y[j] % M
            z[(i + j) % m] % M
    return z

def pow_(x, k):
    r = x.copy()
    k -= 1
    while k:
        if k & 1:
            r = prod(r, x)
        x = prod(x, x)
        k >>= 1
    return r
(n, l, m) = map(int, sys.stdin.readline().split())
beg = [0 for _ in range(m)]
mid = [0 for _ in range(m)]
end = [0 for _ in range(m)]
b_ = list(map(int, sys.stdin.readline().split()))
m_ = list(map(int, sys.stdin.readline().split()))
e_ = list(map(int, sys.stdin.readline().split()))
for v in b_:
    beg[v % m] += 1
for v in m_:
    mid[v % m] += 1
for i in range(n):
    end[(e_[i] + m_[i]) % m] += 1
if l == 2:
    res = prod(beg, end)
else:
    res = pow_(mid, l - 2)
    res = prod(end, prod(res, beg))
print(res[0] % M)

Solution (Python):
from sys import stdin
input = stdin.readline

def main():
    mod = 10 ** 9 + 7
    (n, l, m) = map(int, input().split())
    ar = [0 for i in range(m)]
    ar3 = [0 for i in range(m)]

    def f(num):
        return int(num) % m

    def f2(num):
        return num % 1000000007
    ai1 = list(map(f, input().split()))
    ai2 = list(map(f, input().split()))
    ai3 = list(map(f, input().split()))
    mm = m ** 2
    ar2 = [0 for i in range(mm)]

    def mult(m1, m2):
        ans = [0 for i in range(mm)]
        m12 = [0 for i in range(m)]
        for i in range(m):
            for j in range(m):
                m12[i] += m1[i + j * m]
        for i in range(m):
            for j in range(m):
                temp = (i + j) % m
                for j2 in range(m):
                    ans[temp + j2 * m] += m12[i] * m2[j + j2 * m]
        ans = list(map(f2, ans))
        return ans

    def mult2(m1, m2):
        ans = [0] * m
        for i in range(m):
            for j in range(m):
                ans[(i + j) % m] += m1[i] * m2[j]
        print(ans[0] % mod)

    def mult3(m1, m2):
        m12 = [0 for i in range(m)]
        for i in range(m):
            for j in range(m):
                m12[i] += m1[i + j * m]
                m12[i] %= mod
        m22 = [0 for i in range(m)]
        for i in range(m):
            for j in range(m):
                m22[i] += m2[i + j * m]
                m22[i] %= mod
        ans = [0 for i in range(mm)]
        for i in range(m):
            for j in range(m):
                ans[(i + j) % m] += m12[i] * m22[j]
                ans[(i + j) % m] %= mod
        return ans

    def power(number, n):
        n -= 1
        if n == -1:
            return number
        res = number
        number2 = [number[i] for i in range(mm)]
        while n:
            if n & 1:
                res = mult3(res, number)
            number = mult3(number, number)
            n >>= 1
        return mult(res, number2)
    for i in range(n):
        ar[ai1[i]] += 1
        ar2[ai2[i] + ai3[i] * m] += 1
        ar3[ai2[i]] += 1
    if l == 1:
        mult2(ar, ar3)
        return
    ans = power(ar2, l - 2)
    ar4 = [0] * m
    for i in range(m):
        for j in range(m):
            ar4[(i + j) % m] += ans[i + j * m]
    mult2(ar, ar4)
main()