Problem:


Combination of Number Sequences

0 から 9 までの整数を使った n 個の数の並び k1, k2, ..., kn を考えます。正の整数 n と s を読み込んで、

k1 + 2 &times; k2 + 3 &times; k3 + ... + n &times; kn = s 

となっているような n 個の数の並びが何通りあるかを出力するプログラムを作成してください。ただし、1 つの「n 個の数の並び」には同じ数が 2 回以上現われないものとします。

Input

入力は複数のデータセットからなります。各データセットとして、n (1 ≤ n ≤ 10) と s (0 ≤ s ≤ 10,000)が空白区切りで１行に与えられます。

データセットの数は 100 を超えません。

Output

データセットごとに、n 個の整数の和が s になる組み合わせの個数を１行に出力します。

Sample Input

3 10
3 1

Output for the Sample Input

8
0



Sample input:
3 10
3 1


Sample output:
8
0

Submission status: Time Limit Exceeded

Solution code:
# ??°??????????????????????????§???????????°??????
import sys

def get_min_max():
    max_s = []
    for i in range(1, 11):
        max_sum = 0
        min_sum = 0
        for j in range(i):
            max_sum += (9-j) * (i-j)
            min_sum += j * (i-j)

        max_s.append([min_sum, max_sum])

    return max_s

def get_sequence(s, n, k, ks):
    # s : ????¨?
    # n : n????????°???
    # k : k???????????°???????????????(1???10)
    # ks: ?????¨?????????????????°????????????

    if k == n:
        if s % k == 0 and s // k in ks:
            return 1
        else:
            return 0

    if k > n:
        return 0

    cnt = 0
    for i in ks:
        s_copy = s
        s_copy -= i * k
        if s_copy < 0:
            break
        
        ks_copy = ks[:]
        ks_copy.remove(i)
        cnt += get_sequence(s_copy, n, k+1, ks_copy)

    return cnt
        
def main():
    min_max_s = get_min_max()
    
    while True:
        line = sys.stdin.readline().strip()
        if line is None or line == '':
            break

        data = line.split(' ')
        n = int(data[0])
        s = int(data[1])
        
        if s < min_max_s[n-1][0] or s > min_max_s[n-1][1]:
            print(0)
        else:
            cnt = get_sequence(s, n, 1, list(range(10)))
            print(cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# ??°??????????????????????????§???????????°??????
import sys
import itertools

def get_min_max():
    max_s = []
    for i in range(1, 11):
        max_sum = 0
        min_sum = 0
        for j in range(i):
            max_sum += (9-j) * (i-j)
            min_sum += j * (i-j)

        max_s.append([min_sum, max_sum])

    return max_s

def get_sequence(s, n, k, ks):
    # s : ????¨?
    # n : n????????°???
    # k : k???????????°???????????????(1???10)
    # ks: ?????¨?????????????????°????????????
    #print(s)
    cnt = 0

    l = list(range(10))
    for nums in itertools.combinations(l, n):
        for num in itertools.permutations(nums, len(nums)):
            if num == (4, 0, 2):
                print('**** (4, 0, 2) ****')
                
            sum = 0
            for index, i in enumerate(num, 1):
                sum += index * i
            
            if sum == s:
                #print('num:', num)
                cnt += 1
                if n == 10:
                    break           # ?¬????nums???????????????

    return cnt
        
def main():
    min_max_s = get_min_max()
    
    while True:
        line = sys.stdin.readline().strip()
        if line is None or line == '':
            break

        data = line.split(' ')
        n = int(data[0])
        s = int(data[1])
        
        if s < min_max_s[n-1][0] or s > min_max_s[n-1][1]:
             print(0)
        else:
            cnt = get_sequence(s, n, 1, list(range(10)))
            print(cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# ??°??????????????????????????§???????????°??????
import sys
import itertools

def get_min_max():
    max_s = []
    for i in range(1, 11):
        max_sum = 0
        min_sum = 0
        for j in range(i):
            max_sum += (9-j) * (i-j)
            min_sum += j * (i-j)

        max_s.append([min_sum, max_sum])

    return max_s

def get_sequence(s, n, k, ks, min_max_s):
    # s : ????¨?
    # n : n????????°???
    # k : k???????????°???????????????(1???10)
    # ks: ?????¨?????????????????°????????????
    #print(s)
    cnt = 0

    l = list(range(10))
    for nums in itertools.combinations(l, n):
        for num in itertools.permutations(nums, len(nums)):
            if num == (4, 0, 2):
                print('**** (4, 0, 2) ****')
                
            sum = 0
            for index, i in enumerate(num, 1):
                sum += index * i
            
            if sum == s:
                #print('num:', num)
                cnt += 1
                if sum == min_max_s[n-1][1]:
                    break           # ?¬????nums???????????????

    return cnt
        
def main():
    min_max_s = get_min_max()
    
    while True:
        line = sys.stdin.readline().strip()
        if line is None or line == '':
            break

        data = line.split(' ')
        n = int(data[0])
        s = int(data[1])
        
        if s < min_max_s[n-1][0] or s > min_max_s[n-1][1]:
             print(0)
        else:
            cnt = get_sequence(s, n, 1, list(range(10)), min_max_s)
            print(cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
for line in sys.stdin:
	n,s=map(int,input().split())
	cnt=0
	for i in itertools.permutations(range(10),n):
		sum=0
		for j,k in enumerate(i):sum+=(j+1)*k
		if sum==s:cnt+=1
	print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys


def try_sum(xk=[], xn=[]):
    xi = 0
    for i in range(a):
        xi += xn[i] * xk[i]
    if xi == b:
        total[0] += 1


def make_k(ceng, xn):
    i = 9
    while i >= 0:
        if ceng > 1:
            gg[ceng - 1] = i
            make_k(ceng - 1, xn)
        else:
            gg[ceng - 1] = i
            if len(list(set(gg))) == a:
                try_sum(gg, xn)
        i -= 1


for xxx in sys.stdin:
    a, b = map(int, xxx.split())
    total = [0]
    xn = list(range(1, a + 1))
    gg = [0] * a
    make_k(a, xn)
    print(total[0])

Submission status: Time Limit Exceeded

Solution code:
import itertools
while 1:
    try:n,s=map(int,input().split())
    except:break
    if n>s:print(0);continue
    c=0
    for x in itertools.combinations(range(min(10,s)),n):
        for y in itertools.permutations(x):
            b=0
            for z,w in zip(y,range(1,n+1)):
                b+=w*z
            c+=b==s
    print(c)

Submission status: Time Limit Exceeded

Solution code:
import itertools
while 1:
    try:n,s=map(int,input().split())
    except:break
    if s>330:print(0);continue
    c=0
    for x in itertools.combinations(range(min(10,s)),n):
        for y in itertools.permutations(x):
            b=0
            for z,w in zip(y,range(1,n+1)):
                b+=w*z
            c+=b==s
    print(c)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

import sys
import os
import itertools

def f(lst):
    s = 0
    for i, v in enumerate(lst):
        s += (i+1) * v
    return s

for s in sys.stdin:
    N, S = map(int, s.split())
    L = [i for i in range(11)]

    # dp[n][s]
    # 11 x 100001
    T = [[0 for i in range(10001)] for j in range(11)]

    for n in range(1, N+1):
        for lst in itertools.permutations(L, n):
            T[n][f(lst)] += 1

    print(T[N][S])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0070
"""
import sys
from itertools import permutations


def solve(pick, target):
    hit = 0
    for nums in permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], pick):
        temp = []
        for i in range(1, pick+1):
            temp.append('{} * {}'.format(i, nums[0]))
            nums = nums[1:]
        exp = ' + '.join(temp)
        ans = eval(exp)
        if ans == target:
            # print(exp)
            hit += 1
    return hit


def main(args):

    for line in sys.stdin:
        pick, target = [int(x) for x in line.strip().split(' ')]
        result = solve(pick, target)
        print(result)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0070
"""
import sys
from itertools import permutations


def solve1(pick, target):
    # ????´???????????????????????????§???????????????????????????
    hit = 0
    for nums in permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], pick):
        temp = []
        for i in range(1, pick+1):
            temp.append('{} * {}'.format(i, nums[0]))
            nums = nums[1:]
        exp = ' + '.join(temp)
        ans = eval(exp)
        if ans == target:
            # print(exp)
            hit += 1
    return hit


def solve2(pick, target):
    #
    hit = 0
    for nums in permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], pick):
        ans = 0
        for i in range(1, pick+1):
            ans += (i * nums[i-1])
            if ans > target:
                break
        if ans == target:
            hit += 1
    return hit



def main(args):

    for line in sys.stdin:
        pick, target = [int(x) for x in line.strip().split(' ')]
        result = solve2(pick, target)
        print(result)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0070
"""
import sys
import time
from itertools import permutations


def solve1(pick, target):
    # ????´???????????????????????????§???????????????????????????
    hit = 0
    for nums in permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], pick):
        temp = []
        for i in range(1, pick+1):
            temp.append('{} * {}'.format(i, nums[0]))
            nums = nums[1:]
        exp = ' + '.join(temp)
        ans = eval(exp)
        if ans == target:
            # print(exp)
            hit += 1
    return hit


def solve2(pick, target):
    # ????????§??????????????£???????????§?????????????????§???????????????
    # 10?????\?????????????????£???????????????????????????
    if target > 330:
        return 0
    hit = 0
    for nums in permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], pick):
        ans = 0
        for i in range(1, pick+1):
            ans += (i * nums[i-1])
            if ans > target:
                break
        if ans == target:
            hit += 1
    return hit


def solve3(pick, target):
    #
    if target > 330:
        return 0
    hit = 0
    for nums in permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], pick):
        temp = [x * y for x, y in zip(nums, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])]
        ans = sum(temp)
        if ans == target:
            hit += 1
    return hit


def calc_min_max(pick, numbers):
    # ?????????????????°???????????¨??????pick????????°????????????????????????????°????????????§????????????
    multiplier =  range(1, pick+1)
    min_numbers = numbers[:pick]
    min_numbers.sort(reverse=True)
    temp = [x*y for x, y in zip(min_numbers, multiplier)]
    min = sum(temp)

    numbers = numbers[-pick:]
    temp = [x*y for x, y in zip(numbers, multiplier)]
    max = sum(temp)

    return min, max


Hit = 0
def solve4(pick, target, numbers=[0,1,2,3,4,5,6,7,8,9]):
    global Hit
    if pick == 0:
        if target == 0:
            Hit += 1
        return

    for n in numbers:
        lnumbers = numbers[:]
        lnumbers.remove(n)
        p_min, p_max = calc_min_max(pick-1, lnumbers)
        if target-(n*pick) > p_max or target-(n*pick) < p_min:
            continue
        else:
            solve4(pick-1, target-(n*pick), lnumbers)



def main(args):
    global Hit
    for line in sys.stdin:
        pick, target = [int(x) for x in line.strip().split(' ')]

#        start = time.time()
#        result = solve1(pick, target)
#        end = time.time()
#        print('solve1({}): {}'.format(end-start, result))

#        start = time.time()
#        result = solve2(pick, target)
#        end = time.time()
#        print('solve2({}): {}'.format(end-start, result))

#        start = time.time()
        result = solve4(pick, target)
#        end = time.time()
#        print('solve4({}): {}'.format(end-start, Hit))
        print(Hit)
        Hit = 0


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0070
"""
import sys
import time
from itertools import permutations


def solve1(pick, target):
    # ????´???????????????????????????§???????????????????????????
    hit = 0
    for nums in permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], pick):
        temp = []
        for i in range(1, pick+1):
            temp.append('{} * {}'.format(i, nums[0]))
            nums = nums[1:]
        exp = ' + '.join(temp)
        ans = eval(exp)
        if ans == target:
            # print(exp)
            hit += 1
    return hit


def solve2(pick, target):
    # ????????§??????????????£???????????§?????????????????§???????????????
    # 10?????\?????????????????£???????????????????????????
    if target > 330:
        return 0
    hit = 0
    for nums in permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], pick):
        ans = 0
        for i in range(1, pick+1):
            ans += (i * nums[i-1])
            if ans > target:
                break
        if ans == target:
            hit += 1
    return hit


def solve3(pick, target):
    #
    if target > 330:
        return 0
    hit = 0
    for nums in permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], pick):
        temp = [x * y for x, y in zip(nums, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])]
        ans = sum(temp)
        if ans == target:
            hit += 1
    return hit


def calc_min_max(pick, numbers):
    # ?????????????????°???????????¨??????pick????????°????????????????????????????°????????????§????????????
    multiplier =  range(1, pick+1)
    min_numbers = numbers[:pick]
    min_numbers.sort(reverse=True)
    temp = [x*y for x, y in zip(min_numbers, multiplier)]
    min = sum(temp)

    numbers = numbers[-pick:]
    temp = [x*y for x, y in zip(numbers, multiplier)]
    max = sum(temp)

    return min, max


Hit = 0
def solve4(pick, target, numbers=[0,1,2,3,4,5,6,7,8,9]):
    # 7.72 [s]
    global Hit
    if pick == 0:
        if target == 0:
            Hit += 1
        return

    for n in numbers:
        lnumbers = numbers[:]
        lnumbers.remove(n)
        p_min, p_max = calc_min_max(pick-1, lnumbers)
        if target-(n*pick) > p_max or target-(n*pick) < p_min:
            continue
        else:
            solve4(pick-1, target-(n*pick), lnumbers)



def main(args):
    global Hit
    for line in sys.stdin:
        pick, target = [int(x) for x in line.strip().split(' ')]
#        start = time.time()
        result = solve2(pick, target)
#        end = time.time()
#        print('solve1({}): {}'.format(end-start, result))

        print(result)

#        solve4(pick, target)
#        print(Hit)
#        Hit = 0



if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0070
"""
import sys
import time
from itertools import permutations


def solve1(pick, target):
    # ????´???????????????????????????§???????????????????????????
    hit = 0
    for nums in permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], pick):
        temp = []
        for i in range(1, pick+1):
            temp.append('{} * {}'.format(i, nums[0]))
            nums = nums[1:]
        exp = ' + '.join(temp)
        ans = eval(exp)
        if ans == target:
            # print(exp)
            hit += 1
    return hit


def solve2(pick, target):
    # ????????§??????????????£???????????§?????????????????§???????????????
    # 10?????\?????????????????£??????????????????????????????40.0[s]??\???????????£??????????????§?????§????????????????????????
    if target > 330:
        return 0
    hit = 0
    for nums in permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], pick):
        ans = 0
        for i in range(1, pick+1):
            ans += (i * nums[i-1])
            if ans > target:
                break
        if ans == target:
            hit += 1
    return hit


def solve3(pick, target):
    #
    if target > 330:
        return 0
    hit = 0
    for nums in permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], pick):
        temp = [x * y for x, y in zip(nums, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])]
        ans = sum(temp)
        if ans == target:
            hit += 1
    return hit


def calc_min_max(pick, numbers):
    # ?????????????????°???????????¨??????pick????????°????????????????????????????°????????????§????????????
    multiplier =  range(1, pick+1)
    min_numbers = numbers[:pick]
    min_numbers.sort(reverse=True)
    temp = [x*y for x, y in zip(min_numbers, multiplier)]
    min = sum(temp)

    numbers = numbers[-pick:]
    temp = [x*y for x, y in zip(numbers, multiplier)]
    max = sum(temp)

    return min, max


Hit = 0
def solve4(pick, target, numbers=[0,1,2,3,4,5,6,7,8,9]):
    # 7.72 [s]
    global Hit
    if pick == 0:
        if target == 0:
            Hit += 1
        return

    for n in numbers:
        lnumbers = numbers[:]
        lnumbers.remove(n)
        p_min, p_max = calc_min_max(pick-1, lnumbers)
        if target-(n*pick) > p_max or target-(n*pick) < p_min:
            continue
        else:
            solve4(pick-1, target-(n*pick), lnumbers)



def has_possibility(pick, target, numbers):
    # ?????????????????°???????????¨??????pick????????°????????????????????????????°????????????§????????????
    multiplier =  range(1, pick+1)
    max_numbers = numbers[-pick:]
    temp = [x*y for x, y in zip(max_numbers, multiplier)]
    max = sum(temp)
    if target > max:
        return False

    min_numbers = numbers[:pick]
    min_numbers.sort(reverse=True)
    temp = [x*y for x, y in zip(min_numbers, multiplier)]
    min = sum(temp)
    if target < min:
        return False

    return True


Hit = 0
def solve5(pick, target, numbers=[0,1,2,3,4,5,6,7,8,9]):
    # 7.72 [s]
    global Hit
    if pick == 0:
        if target == 0:
            Hit += 1
        return

    for n in numbers:
        lnumbers = numbers[:]
        lnumbers.remove(n)
        if has_possibility(pick-1, target-(n*pick), lnumbers):
            solve5(pick-1, target-(n*pick), lnumbers)
        else:
            continue




def main(args):
    global Hit
    for line in sys.stdin:
        global Hit
        pick, target = [int(x) for x in line.strip().split(' ')]
        solve5(pick, target)
        print(Hit)
        Hit = 0
#        start = time.time()
#        result = solve2(pick, target)
#        end = time.time()
#        print('solve1({}): {}'.format(end-start, result))

#    solve5(8, 100)
#    print(Hit)
#    Hit = 0
#    solve5(9, 150)
#    print(Hit)
#    Hit = 0



if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0070
"""
import sys
import time
from itertools import permutations


def solve1(pick, target):
    # ????´???????????????????????????§???????????????????????????
    hit = 0
    for nums in permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], pick):
        temp = []
        for i in range(1, pick+1):
            temp.append('{} * {}'.format(i, nums[0]))
            nums = nums[1:]
        exp = ' + '.join(temp)
        ans = eval(exp)
        if ans == target:
            # print(exp)
            hit += 1
    return hit


def solve2(pick, target):
    # ????????§??????????????£???????????§?????????????????§???????????????
    # 10?????\?????????????????£??????????????????????????????40.0[s]??\???????????£??????????????§?????§????????????????????????
    if target > 330:
        return 0
    hit = 0
    for nums in permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], pick):
        ans = 0
        for i in range(1, pick+1):
            ans += (i * nums[i-1])
            if ans > target:
                break
        if ans == target:
            hit += 1
    return hit


def solve3(pick, target):
    #
    if target > 330:
        return 0
    hit = 0
    for nums in permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], pick):
        temp = [x * y for x, y in zip(nums, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])]
        ans = sum(temp)
        if ans == target:
            hit += 1
    return hit


def calc_min_max(pick, numbers):
    # ?????????????????°???????????¨??????pick????????°????????????????????????????°????????????§????????????
    multiplier =  range(1, pick+1)
    min_numbers = numbers[:pick]
    min_numbers.sort(reverse=True)
    temp = [x*y for x, y in zip(min_numbers, multiplier)]
    min = sum(temp)

    numbers = numbers[-pick:]
    temp = [x*y for x, y in zip(numbers, multiplier)]
    max = sum(temp)

    return min, max


Hit = 0
def solve4(pick, target, numbers=[0,1,2,3,4,5,6,7,8,9]):
    # 7.72 [s]
    global Hit
    if pick == 0:
        if target == 0:
            Hit += 1
        return

    for n in numbers:
        lnumbers = numbers[:]
        lnumbers.remove(n)
        p_min, p_max = calc_min_max(pick-1, lnumbers)
        if target-(n*pick) > p_max or target-(n*pick) < p_min:
            continue
        else:
            solve4(pick-1, target-(n*pick), lnumbers)


def has_possibility(pick, target, numbers):
    # ?????????????????°???????????¨??????pick????????°????????????????????????????°????????????§???????±???????
    # ???????????°?????????????????????????????????????????????????????????
    if pick == 1:
        return numbers[0] <= target <= numbers[-1]
    multiplier = range(1, pick+1)
    max_numbers = numbers[-pick:] #  ??§??????????????°??????pick????????????
    max = sum(x*y for x, y in zip(max_numbers, multiplier))
    if target > max:
        return False

    min_numbers = numbers[:pick]
    min_numbers.sort(reverse=True)
    min = sum(x*y for x, y in zip(min_numbers, multiplier))
    if target < min:
        return False

    return True


Hit = 0
def solve5(pick, target, numbers=[0,1,2,3,4,5,6,7,8,9]):
    # 6.88 [s]
    global Hit
    if pick == 0:
        if target == 0:
            Hit += 1
        return

    for n in numbers:
        lnumbers = numbers[:]
        lnumbers.remove(n)
        if has_possibility(pick-1, target-(n*pick), lnumbers):
            solve5(pick-1, target-(n*pick), lnumbers)
        else:
            continue


def main(args):
    global Hit
    for line in sys.stdin:
        pick, target = [int(x) for x in line.strip().split(' ')]
        solve5(pick, target)
        print(Hit)
        Hit = 0
#        start = time.time()
#        result = solve2(pick, target)
#        end = time.time()
#        print('solve1({}): {}'.format(end-start, result))

#    solve5(8, 100)
#    print(Hit)
#    Hit = 0
#    solve5(9, 150)
#    print(Hit)
#    Hit = 0


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0070
"""
import sys
import time
from itertools import permutations


def solve1(pick, target):
    # ????´???????????????????????????§???????????????????????????
    hit = 0
    for nums in permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], pick):
        temp = []
        for i in range(1, pick+1):
            temp.append('{} * {}'.format(i, nums[0]))
            nums = nums[1:]
        exp = ' + '.join(temp)
        ans = eval(exp)
        if ans == target:
            # print(exp)
            hit += 1
    return hit


def solve2(pick, target):
    # ????????§??????????????£???????????§?????????????????§???????????????
    # 10?????\?????????????????£??????????????????????????????40.0[s]??\???????????£??????????????§?????§????????????????????????
    if target > 330:
        return 0
    hit = 0
    for nums in permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], pick):
        ans = 0
        for i in range(1, pick+1):
            ans += (i * nums[i-1])
            if ans > target:
                break
        if ans == target:
            hit += 1
    return hit


def solve3(pick, target):
    #
    if target > 330:
        return 0
    hit = 0
    for nums in permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], pick):
        temp = [x * y for x, y in zip(nums, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])]
        ans = sum(temp)
        if ans == target:
            hit += 1
    return hit


def calc_min_max(pick, numbers):
    # ?????????????????°???????????¨??????pick????????°????????????????????????????°????????????§????????????
    multiplier =  range(1, pick+1)
    min_numbers = numbers[:pick]
    min_numbers.sort(reverse=True)
    temp = [x*y for x, y in zip(min_numbers, multiplier)]
    min = sum(temp)

    numbers = numbers[-pick:]
    temp = [x*y for x, y in zip(numbers, multiplier)]
    max = sum(temp)

    return min, max


Hit = 0
def solve4(pick, target, numbers=[0,1,2,3,4,5,6,7,8,9]):
    # 7.72 [s]
    global Hit
    if pick == 0:
        if target == 0:
            Hit += 1
        return

    for n in numbers:
        lnumbers = numbers[:]
        lnumbers.remove(n)
        p_min, p_max = calc_min_max(pick-1, lnumbers)
        if target-(n*pick) > p_max or target-(n*pick) < p_min:
            continue
        else:
            solve4(pick-1, target-(n*pick), lnumbers)


def has_possibility(pick, target, numbers):
    # ?????????????????°???????????¨??????pick????????°????????????????????????????°????????????§???????±???????
    # ???????????°?????????????????????????????????????????????????????????
    if pick == 1:
        return numbers[0] <= target <= numbers[-1]

    multiplier = range(1, pick+1)
    max_numbers = numbers[-pick:] #  ??§??????????????°??????pick????????????
    max = sum(x*y for x, y in zip(max_numbers, multiplier))
    if target > max:
        return False

    min_numbers = numbers[:pick]
    min_numbers.sort(reverse=True)
    min = sum(x*y for x, y in zip(min_numbers, multiplier))
    if target < min:
        return False

    return True


Hit = 0
def solve5(pick, target, numbers=[0,1,2,3,4,5,6,7,8,9]):
    # 5.99 [s]
    global Hit
    if pick == 1:
        if target in numbers:
            Hit += 1
        return

    for n in numbers:
        lnumbers = numbers[:]
        lnumbers.remove(n)
        if has_possibility(pick-1, target-(n*pick), lnumbers):
            solve5(pick-1, target-(n*pick), lnumbers)
        else:
            continue


def main(args):
    global Hit
    for line in sys.stdin:
        pick, target = [int(x) for x in line.strip().split(' ')]
        solve5(pick, target)
        print(Hit)
        Hit = 0
#        start = time.time()
#        result = solve2(pick, target)
#        end = time.time()
#        print('solve1({}): {}'.format(end-start, result))

#    solve5(8, 100)
#    print(Hit)
#    Hit = 0
#    solve5(9, 150)
#    print(Hit)
#    Hit = 0


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Time Limit Exceeded

Solution code:
def check(s_i, n_i, count, i_set):
    for i in range(9):
        i_set_tmp = set(i_set)
        if i not in i_set_tmp:
            continue
        s_tmp = s_i
        n_tmp = n_i
        if s_tmp == 0 and n_tmp == 0:
            return count + 1
        elif s_tmp < 0 or n_tmp == 0:
            break
        else:
            s_tmp -= i*n_tmp
            n_tmp -= 1
            i_set_tmp.remove(i)
            count = check(s_tmp, n_tmp, count, i_set_tmp)
    return count

while True:
    try:
        n, s = map(int, input().split())
        print(check(s, n, 0, set([0,1,2,3,4,5,6,7,8,9])))
    except:
        break

Submission status: Time Limit Exceeded

Solution code:
def check(s_i, n_i, count, i_set):
    for i in range(9):
        print("s:",s_i,"n:",n_i,"i:",i,"count:",count)
        i_set_tmp = set(i_set)
        if i not in i_set:
            continue
        s_tmp = s_i - i*n_i
        n_tmp = n_i - 1
        if s_tmp == 0 and n_tmp == 0:
            return count + 1
        elif s_tmp < 0 or n_tmp < 0:
            break
        else:
            i_set_tmp.remove(i)
            count = check(s_tmp, n_tmp, count, i_set_tmp)
    return count

while True:
    try:
        n, s = map(int, input().split())
        print(check(s, n, 0, set([0,1,2,3,4,5,6,7,8,9])))
    except:
        break

Submission status: Time Limit Exceeded

Solution code:
def check(s_i, n_i, count, i_set):
    for i in range(9):
        i_set_tmp = set(i_set)
        if i not in i_set:
            continue
        if s_i == 0 and n_i == 0:
            return count + 1
        elif s_i < 0 or n_i < 0:
            break
        else:
            s_tmp = s_i - i*n_i
            n_tmp = n_i - 1
            i_set_tmp.remove(i)
            count = check(s_tmp, n_tmp, count, i_set_tmp)
    return count

i_list = [i for i in range(10)]
while True:
    try:
        n, s = map(int, input().split())
        ans = check(s, n, 0, set(i_list))
        print(ans)
    except:
        break

Submission status: Time Limit Exceeded

Solution code:
import itertools,sys
for e in sys.stdin:
 n,s=map(int,e.split())
 print([sum(-~i*k for i,k in zip(range(n),p))for p in itertools.permutations(range(10),n)].count(s))

Submission status: Time Limit Exceeded

Solution code:
import itertools,sys
for e in sys.stdin:
 n,s=map(int,e.split())
 print(sum(s==sum(-~i*k for i,k in zip(range(n),p))for p in itertools.permutations(range(10),n)))

Submission status: Time Limit Exceeded

Solution code:
import itertools,sys
for e in sys.stdin:
 n,s=map(int,e.split())
 print(sum(1for p in itertools.permutations(range(10),n)if s==sum(-~i*k for i,k in zip(range(n),p))))

Submission status: Time Limit Exceeded

Solution code:
import sys,itertools
for line in sys.stdin.readlines():
    n,s=map(int,line.strip().split())
    count=0
    for c in itertools.permutations(xrange(10),n):
        total=0
        for i in xrange(1,n+1):
            total+=i*c[i-1]
        if total==s:
            count+=1
    print count

Submission status: Time Limit Exceeded

Solution code:
import sys,itertools
for line in sys.stdin.readlines():
    n,s=map(int,line.strip().split())
    count=0
    for c in itertools.permutations(xrange(10),n):
        total=0
        for i in xrange(1,n+1):
            if total>s:break
            total+=i*c[i-1]
        if total==s:
            count+=1
    print count

Submission status: Time Limit Exceeded

Solution code:
import itertools
while True:
    try:
        n,s=map(int,raw_input().split())
    except EOFError:
	break
    cnt=0
    for i in itertools.permutations(range(10),n):
        temp=sum(k*j for k,j in zip(i,range(1,n+1)))
        if temp==s:
            cnt+=1
    print cnt

Submission status: Time Limit Exceeded

Solution code:
import itertools
while True:
    try:
        n,s=map(int,raw_input().split())
    except EOFError:
        break
    cnt=0
    for ls in itertools.permutations(range(10),n):
        t=sum(i*j for i,j in zip(ls,range(1,n+1)))
        if t==s:
            cnt+=1
    print cnt

Submission status: Time Limit Exceeded

Solution code:
import itertools
memo=[[0]*331 for i in range(11)]
for i in range(1,11):
    for ls in itertools.permutations(range(10),i):
        t=sum(i*j for i,j in zip(ls,range(1,11)))
        memo[i][t]+=1

while True:
    try:
        n,s=map(int,raw_input().split())
    except EOFError:
        break
    print memo[n][s]