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 itertools
dp=[[0]*400 for i in range(11)]
for i in range(1,11):
    t=range(1,1+i)
    for j in itertools.permutations(range(10),i):
        s=sum(l*k for l,k in zip(j,t))
        dp[i][s]+=1

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

Submission status: Time Limit Exceeded

Solution code:
import sys,itertools
for l in sys.stdin.readlines():
    n,s=map(int,l.rstrip().split())
    ans=0
    for d in itertools.permutations(range(10),n):
        tmp=[]
        for i,v in enumerate(d):
            tmp.append((i+1)*v)
        if s==sum(tmp): ans+=1
    print ans

Submission status: Time Limit Exceeded

Solution code:

import sys
import itertools


def solv(n, s):
    r = 0
    for p in itertools.permutations(range(10), n):
        if foo(p, n) == s:
            r += 1
    return r


def foo(perm, n):
    return sum(map(lambda x, y: x * y, range(1, n+1), perm))


for line in sys.stdin:
    n, s = map(int, line.split(' '))
    print solv(n, s)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools


def solv(n, s):
    r = 0
    for p in itertools.permutations(xrange(10), n):
        if foo(p, n) == s:
            r += 1
    return r


def foo(perm, n):
    return sum(map(lambda x, y: x * y, xrange(1, n+1), perm))


for line in sys.stdin:
    n, s = map(int, line.split(' '))
    print solv(n, s)

Submission status: Time Limit Exceeded

Solution code:

import sys
import itertools


def solv(n, s):
    if s > 330:
        return 0
    table = [{}] * 11
    r = 0
    if table[n]:
        for ps in table[n].keys():
            if table[n][ps] == s:
                r += 0
    else:
        for p in itertools.permutations(xrange(10), n):
            s0 = foo(p, n)
            table[n][str(p)] = s0
            if s0 == s:
                r += 1
    return r


def foo(perm, n):
    return sum(map(lambda x, y: x * y, xrange(1, n+1), perm))


for line in sys.stdin:
    n, s = map(int, line.split(' '))
    print solv(n, s)

Submission status: Time Limit Exceeded

Solution code:

import sys
import itertools


table = [[], [], [], [], [], [], [], [], [], [], []]

def solv(n, s):
    if s > 330:
        return 0
    dp = table[n]
    if dp:
        return dp[s]
    else:
        dp = [0] * 331
        for p in itertools.permutations(xrange(10), n):
            s0 = foo(p, n)
            dp[s0] += 1
        return dp[s]


def foo(perm, n):
    return sum(map(lambda x, y: x * y, xrange(1, n+1), perm))


for line in sys.stdin:
    n, s = map(int, line.split(' '))
    print solv(n, s)

Submission status: Time Limit Exceeded

Solution code:

import sys
import itertools


table = [[], [], [], [], [], [], [], [], [], [], []]

def solv(n, s):
    if s > 330:
        return 0
    dp = table[n]
    if dp:
        return dp[s]
    else:
        for i in xrange(331):
            dp.append(0)
        for p in itertools.permutations(xrange(10), n):
            s0 = foo(p, n)
            dp[s0] += 1
        return dp[s]


def foo(perm, n):
    return sum(map(lambda x, y: x * y, xrange(1, n+1), perm))


for line in sys.stdin:
    n, s = map(int, line.split(' '))
    print solv(n, s)

Submission status: Time Limit Exceeded

Solution code:

import sys
import itertools


def calc_table(t, perm, rest):
    for x in rest:
        p = list(perm)
        p.append(x)
        s = foo(p)
        t[len(p)][s] += 1
        r = list(rest)
        r.remove(x)
        calc_table(t, p, r)
    return t


def foo(perm):
    n = len(perm)
    return sum(map(lambda x, y: x * y, xrange(1, n+1), perm))



table = []
for i in xrange(11):
    table.append([0] * 331)
table = calc_table(table, [], range(10))

for line in sys.stdin:
    n, s = map(int, line.split(' '))
    if s > 330:
        print 0
    else:
        print table[n][s]

Submission status: Time Limit Exceeded

Solution code:

import sys
import itertools


def calc_table(t, perm, rest):
    if len(rest) == 0:
        return t
    for x in rest:
        p = list(perm)
        p.append(x)
        n = len(p)
        s = foo(p, n)
        t[n][s] += 1
        r = list(rest)
        r.remove(x)
        calc_table(t, p, r)
    return t


def foo(perm, n):
    return sum(map(lambda x, y: x * y, xrange(1, n+1), perm))



table = []
for i in xrange(11):
    table.append([0] * 331)
table = calc_table(table, [], range(10))

for line in sys.stdin:
    n, s = map(int, line.split(' '))
    if s > 330:
        print 0
    else:
        print table[n][s]

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (division, absolute_import, print_function,
                        unicode_literals)
from sys import stdin

def solve(li, n, s, total, cnt):
    for i in li:
        value = i * n + total
        if value > s:
            break
        if n > 1:
            a = li[:]
            a.remove(i)
            solve(a, n - 1, s, value, cnt)
        elif value == s:
            cnt[0] += 1

for line in stdin:
    n, s = (int(s) for s in line.split())

    cnt = [0]
    solve(list(range(10)), n, s, 0, cnt)
    print(cnt[0])

Submission status: Time Limit Exceeded

Solution code:
import itertools,sys
for a in sys.stdin:
  c=0
  n,s=map(int,a.split())
  for e in itertools.permutations(range(9),n):
    x=sum([(i+1)*e[i] for i in range(len(e))])
    if x==s: c+=1
  print c

Submission status: Time Limit Exceeded

Solution code:
import itertools,sys
for a in sys.stdin:
  c=0
  n,s=map(int,a.split())
  x=[]
  for e in itertools.permutations(range(9),n):
    x.append(sum([(i+1)*e[i] for i in range(len(e))]))
  print x.count(s)

Submission status: Time Limit Exceeded

Solution code:
import itertools,sys
eq=[""]*10
eq[0]="{0}"
eq[1]=eq[0]+"+2*{1}"
eq[2]=eq[1]+"+3*{2}"
eq[3]=eq[2]+"+4*{3}"
eq[4]=eq[3]+"+5*{4}"
eq[5]=eq[4]+"+6*{5}"
eq[6]=eq[5]+"+7*{6}"
eq[7]=eq[6]+"+8*{7}"
eq[8]=eq[7]+"+9*{8}"
eq[9]=eq[8]+"+10*{9}"
for a in sys.stdin:
  c=0
  n,s=map(int,a.split())
  for e in itertools.permutations(range(10),n):
    if eval(eq[n-1].format(*e))==s: c+=1
  print c

Submission status: Time Limit Exceeded

Solution code:
import itertools,sys
for a in sys.stdin:
  c=0
  n,s=map(int,a.split())
  for e in itertools.permutations(range(10),n):
    x=0
    for i in range(n):
      x+=sum(e[i:])
      if x>s:break
    else:
      if x==s: c+=1
  print c

Submission status: Time Limit Exceeded

Solution code:
import itertools,sys
for a in sys.stdin:
  c=0
  n,s=map(int,a.split())
  for e1 in itertools.combinations(range(10),n):
    for e2 in itertools.permutations(e1,n):
      i=n
      x=0
      for e3 in e2:
        x+=i*e3
        i-=1
        if x>s:break
      else:
        if x==s:c+=1
  print c

Submission status: Time Limit Exceeded

Solution code:
import itertools,sys
for a in sys.stdin:
  c=0
  n,s=map(int,a.split())
  for e in itertools.permutations(range(9),n):
    x=sum([i*j for i,j in zip(range(1,n+1),e)])
    if x==s: c+=1
  print c

Submission status: Time Limit Exceeded

Solution code:
import itertools,sys
def f0070(A,n,s):
  if n==1:
    if s in A: return 1
    else: return 0
  c=0
  for i in range(len(A)):
    b=s-A[i]*n
    if b<=0:continue
    if b>0: c+=f0070(A[:i]+A[i+1:],n-1,b)
  return c

for a in sys.stdin:
  c=0
  n,s=map(int,a.split())
  c=f0070(range(10),n,s)
  print c

Submission status: Time Limit Exceeded

Solution code:
import itertools,sys
def f0070(A,n,s):
  if n==1:
    if s in A: return 1
    else: return 0
  c=0
  for i in range(len(A)):
    b=s-A[i]*n
    if b<=0:break
    c+=f0070(A[:i]+A[i+1:],n-1,b)
  return c

for a in sys.stdin:
  c=0
  n,s=map(int,a.split())
  c=f0070(range(10),n,s)
  print c

Submission status: Time Limit Exceeded

Solution code:
def cns(ls,m,sm):
    global s, n, count
    if m == n:
        if sm == s:
            count += 1
        return
    else:
        l = len(ls)
        for i in range(l):
            rls = ls[:]
            k = rls.pop(i)
            dammy =  cns(rls,m+1,sm+(m+1)*k)
        return


ls = [0,1,2,3,4,5,6,7,8,9]
while True:
    n, s = map(int, raw_input().split())
    count = 0
    dammy = cns(ls,0,0)
    print count

Submission status: Time Limit Exceeded

Solution code:
def cns(ls,m,sm):
    global n, s
    count = 0
    if m == n:
        if sm == s:
            return 1
        else:
            return 0
    elif sm <= s:
        for i in range(9-m+1):
            rls = ls[:]
            k = rls.pop(i)
            count += cns(rls,m+1,sm+(m+1)*k)
        return count
    else:
        return 0

ls = [0,1,2,3,4,5,6,7,8,9]
while True:
    n, s = map(int, raw_input().split())
    print cns(ls,0,0)

Submission status: Time Limit Exceeded

Solution code:
def cns(ls,m,sm):
    global n, s
    count = 0
    if m == n:
        if sm == s: return 1
        else      : return 0
    elif  sm <= s:
        for i in range(9-m+1):
            rls = ls[:]
            k = rls.pop(i)
            count += cns(rls,m+1,sm+(m+1)*k)
        return count
    else: return 0

def mx(i):
    if i <= 0: return 0
    else     : return (i+1)*i + mx(i-1)

ls = [0,1,2,3,4,5,6,7,8,9]
while True:
    n, s = map(int, raw_input().split())
    if s > mx(n): print 0
    else        : print cns(ls,0,0)

Submission status: Time Limit Exceeded

Solution code:
def mx(ls,i):
    rls = ls[:]
    if i <= 0 or len(rls) == 0: return 0
    else     : return (i+1)*rls.pop()+ mx(rls,i-1)

def cns(ls,m,sm):
    global n, s
    count = 0
    if m == n:
        if sm == s: return 1
        else      : return 0
    elif sm <= s:
        for i in range(9-m+1):
            rls = ls[:]
            k = rls.pop(i)
            count += cns(rls,m+1,sm+(m+1)*k)
        return count
    else: return 0

ls = [0,1,2,3,4,5,6,7,8,9]
while True:
    n, s = map(int, raw_input().split())
    if s > mx(ls,n): print 0
    else        : print cns(ls,0,0)

Submission status: Time Limit Exceeded

Solution code:
def f(ls,m,s):
	global n
	count = 0
	if s < 0:
		return 0
	if m > 0:
		for i in range(len(ls)):
			lls = ls[:]
			a = (n-m+1)*lls.pop(i)
			count += f(lls,m-1,s-a)
		return count
	else:
		if s == 0:
			return 1
		else:
			return 0
			
ls = [i for i in range(10)]
while True:
	try:
		n,s = map(int, raw_input().split())
		print f(ls,n,s)
	except:
		break

Submission status: Time Limit Exceeded

Solution code:
def f(ls,m,s):
	global n
	count = 0
	if m > 0:
		for i in range(len(ls)):
			lls = ls[:]
			a = (n-m+1)*lls.pop(i)
			if s-a < 0:
				break
			else:
				count += f(lls,m-1,s-a)
		return count
	else:
		if s == 0:
			return 1
		else:
			return 0
			
ls = [i for i in range(10)]
while True:
	try:
		n,s = map(int, raw_input().split())
		print f(ls,n,s)
	except:
		break

Submission status: Time Limit Exceeded

Solution code:
def mx(ls,m):
	global n
	sum = 0
	for i in range(m):
		sum += (n-i)*ls[-i-1]
	return sum
	
def f(ls,m,s):
	global n
	count = 0
	if m > 0:
		if mx(ls,m) < s: return 0
		for i in range(len(ls)):
			lls = ls[:]
			a = (n-m+1)*lls.pop(i)
			if s-a < 0:	break
			else: count += f(lls,m-1,s-a)
		return count
	else:
		return 1 if s == 0 else 0
			
ls = [i for i in range(10)]
while True:
	try:
		n,s = map(int, raw_input().split())
		print f(ls,n,s)
	except:
		break

Submission status: Time Limit Exceeded

Solution code:
def mx(ls,m):
	global n
	sum = 0
	for i in range(m):
		sum += (n-i)*ls[-i-1]
	return sum

def mn(ls,m):
	global n
	sum = 0
	for i in range(m):
		sum += (n-i)*ls[i]
	return sum
	
def f(ls,m,s):
	global n
	count = 0
	if m > 0:
		for i in range(len(ls)):
			lls = ls[:]
			a = (n-m+1)*lls.pop(i)
			if s-a < 0:	break
			vmx,vmn = mx(ls,m),mn(ls,m)
			if vmx < s or vmn > s: return 0
			elif vmx == s or vmn == s: return 1
			else: count += f(lls,m-1,s-a)
		return count
	else:
		return 1 if s == 0 else 0
			
ls = [i for i in range(10)]
while True:
	try:
		n,s = map(int, raw_input().split())
		print f(ls,n,s)
	except:
		break

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations

while True:
    try:
        line = input()
    except EOFError:
        break

    n, s = map(int, line.split())
    count = 0
    for x in permutations(range(10), n):
        if sum(x[i]*(i+1) for i in range(n)) == s:
            count += 1
    print(count)

Submission status: Memory Limit Exceeded

Solution code:
import itertools

while(1):
    [n,s]=[0,0]
    ans=0
    try:
        [n,s]=[int(x) for x in raw_input().split()]
    except:
        break
    plist=list(itertools.permutations(range(10),n))
    for p in plist:
        tmp=0
        for i in range(n):
            tmp+=p[i]*(i+1)
        if tmp==s:
            ans+=1
    print ans

Submission status: Memory Limit Exceeded

Solution code:
import sys
def line():return sys.stdin.readline().strip()

import itertools
k = (0,1,2,3,4,5,6,7,8,9)
while True:
    cnt = 0
    n,s = map(int,line().split())
    for l in list(itertools.permutations(k,n)):
        t = 0
        for i in range(len(l)): t+=l[i] * (i + 1)
        if t == s:cnt+=1
    print(cnt)

Submission status: Memory Limit Exceeded

Solution code:
import sys
def line():return sys.stdin.readline().strip()

import itertools
k = (0,1,2,3,4,5,6,7,8,9)

while True:
    ln = line()
    if not ln:break
    cnt = 0
    n,s = map(int,ln.split())
    for l in list(itertools.permutations(k,n)):
        t = 0
        for i in range(len(l)): t+=l[i] * (i + 1)
        if t == s:cnt+=1
    print(cnt)

Submission status: Memory Limit Exceeded

Solution code:
import sys
def line():return sys.stdin.readline().strip()

import itertools
k = (0,1,2,3,4,5,6,7,8,9)

while True:
    try:
        cnt = 0
        n,s = map(int,line().split())
        for l in list(itertools.permutations(k,n)):
            t = 0
            for i in range(len(l)): t+=l[i] * (i + 1)
            if t == s:cnt+=1
        print(cnt)
    except:
        break

Submission status: Memory Limit Exceeded

Solution code:
from itertools import permutations as P
while True:
    try:
        n, s = map(int, input().split())
    except:
        break
    l = list(P(range(0, 10), n))
    ans = sum([1 for p in l if sum([(i+1)*q for i, q in enumerate(p)]) == s])
    print(ans)

Submission status: Memory Limit Exceeded

Solution code:
import sys
import itertools
def main():
    for line in sys.stdin:
        n, s = map(int, line.split())
        seq = [i for i in range(10)]
        A = list(itertools.permutations(seq, n))
        c = 0
        for x in range(len(A)):
            t = 0
            for y in range(len(A[x])):
                t += (y + 1) * A[x][y]
            if t == s:
                c += 1
        print(c)


if __name__ == "__main__":
    main()

Submission status: Memory Limit Exceeded

Solution code:
import itertools

try:
    while True:
        n, s = map(int, raw_input().split())
        x = []
        for i in range(10):
            x.append(i)

        a = list(itertools.permutations(x, n))

        u = 0
        m = 0
        for i in range(len(a)):
            for j in range(n):
                u += a[i][j] * (j + 1)

            if s == u:
                m += 1

            u = 0
        print m
except:
    None

Submission status: Memory Limit Exceeded

Solution code:
import itertools

try:
    while True:
        n, s = map(int, raw_input().split())
        x = []
        for i in range(10):
            x.append(i)

        a = list(itertools.permutations(x, n))

        u = 0
        m = 0
        for i in range(len(a)):
            for j in range(n):
                u += a[i][j] * (j + 1)
            if s == u:
                m += 1
            u = 0
        print m
except:
    None

Submission status: Memory Limit Exceeded

Solution code:
import itertools

try:
    while True:
        n, s = map(int, raw_input().split())
        x = []
        for i in xrange(10):
            x.append(i)

        a = list(itertools.permutations(x, n))

        u = 0
        m = 0
        for i in range(len(a)):
            for j in range(n):
                u += a[i][j] * (j + 1)

            if s == u:
                m += 1

            u = 0
        print m
except:
    None

Submission status: Memory Limit Exceeded

Solution code:

import sys
import itertools


def solv(n, s):
    table = [{}] * 10
    r = 0
    if table[n]:
        for p in table[n].keys():
            if table[n][p] == s:
                r += 0
    else:
        for p in itertools.permutations(xrange(10), n):
            s0 = foo(p, n)
            table[n][p] = s0
            if s0 == s:
                r += 1
    return r


def foo(perm, n):
    return sum(map(lambda x, y: x * y, xrange(1, n+1), perm))


for line in sys.stdin:
    n, s = map(int, line.split(' '))
    print solv(n, s)

Submission status: Memory Limit Exceeded

Solution code:

import sys
import itertools


def solv(n, s):
    if s > 330:
        return 0
    table = [{}] * 10
    r = 0
    if table[n]:
        for p in table[n].keys():
            if table[n][p] == s:
                r += 0
    else:
        for p in itertools.permutations(xrange(10), n):
            s0 = foo(p, n)
            table[n][p] = s0
            if s0 == s:
                r += 1
    return r


def foo(perm, n):
    return sum(map(lambda x, y: x * y, xrange(1, n+1), perm))


for line in sys.stdin:
    n, s = map(int, line.split(' '))
    print solv(n, s)

Submission status: Memory Limit Exceeded

Solution code:

import sys
import itertools


table = [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]

def solv(n, s):
    if s > 330:
        return 0
    r = 0
    dic = table[n]
    if dic:
        for ps in dic.keys():
            if dic[ps] == s:
                r += 1
    else:
        for p in itertools.permutations(xrange(10), n):
            s0 = foo(p, n)
            dic[p] = s0
            if s0 == s:
                r += 1
    return r


def foo(perm, n):
    return sum(map(lambda x, y: x * y, xrange(1, n+1), perm))


for line in sys.stdin:
    n, s = map(int, line.split(' '))
    print solv(n, s)

Submission status: Memory Limit Exceeded

Solution code:

import sys
import itertools


table = [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]

def solv(n, s):
    if s > 330:
        return 0
    r = 0
    dic = table[n]
    if dic:
        for ps in dic.keys():
            if dic[ps] == s:
                r += 1
    else:
        for p in itertools.permutations(xrange(10), n):
            s0 = foo(p, n)
            dic[str(p)] = s0
            if s0 == s:
                r += 1
    return r


def foo(perm, n):
    return sum(map(lambda x, y: x * y, xrange(1, n+1), perm))


for line in sys.stdin:
    n, s = map(int, line.split(' '))
    print solv(n, s)

Submission status: Memory Limit Exceeded

Solution code:
import itertools,sys
d={}
def f0070(e,n):
  if d.has_key(e): return d[e]
  if n==1: d[e]=e[0]
  else: d[e]=f0070(e[:-1],n-1)+e[-1]*n
  return d[e]
for a in sys.stdin:
  c=0
  n,s=map(int,a.split())
  for e in itertools.permutations(range(10),n):
    if f0070(e,n)==s: c+=1
  print c

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
import sys

# dp[n][使った数字(bit)][合計] = count
dp = [defaultdict(Counter) for _ in [0]*11]
dp[0][0][0] = 1

for i in range(1, 11):
    for used, counter in dp[i-1].items():
        for j in filter(lambda x: used & 2**x == 0, range(10)):
            for total, count in counter.items():
                dp[i][used|2**j][total+j*i] += count

for n, s in (map(int, l.rstrip().split()) for l in sys.stdin):
    print(sum(v for counter in dp[n].values() for k, v in counter.items() if k == s))

Submission status: Accepted

Solution code:
from itertools import combinations
import sys

# dp[n][使った数字(bit)][合計] = count
dp = [[[0]*331 for _ in [0]*2048] for _ in [0]*11]
for i in range(11):
    dp[1][2**i][i] = 1

for i in range(2, 11):
    for used in (sum(comb) for comb in combinations([2**n for n in range(10)], i-1)):
        for j in filter(lambda x: used & 2**x == 0, range(10)):
            for total, count in filter(lambda p: p[1]>0, enumerate(dp[i-1][used])):
                dp[i][used|2**j][total+j*i] += count

for n, s in (map(int, l.rstrip().split()) for l in sys.stdin):
    print(sum(a[s] for a in dp[n] if a[s]) if s<331 else 0)

Submission status: Accepted

Solution code:
K=1024
N=331
R=range(K)
B=[1,2,4,8,16,32,64,128,256,512]
C=[0]*K
D=[[0 for i in range(N)]for j in R]

def init():
    global C,D
    for i in R:
        s=format(i,'b')
        n=s.count('1')
        C[i]=n
        if n==1:
            D[i][B.index(i)]=1
        else:
            for j in range(len(s)):
                if s[-j-1]=='1':
                    dp=n*j
                    i2=i-B[j]
                    for k in range(N-dp):
                        D[i][k+dp]+=D[i2][k]
    return

init()
while 1:
    try:
        n,s=map(int,raw_input().split())
        x=0
        if s<N:
            x=sum([D[i][s] for i in R if C[i]==n])
        print x
    except:
        break

Submission status: Accepted

Solution code:
import sys


def memoize(f):
    memo = {}

    def main(*args):
        if args in memo:
            return memo[args]
        result = memo[args] = f(*args)
        return result

    return main


def get_num(remains):
    i = 0
    while remains:
        if remains & 1:
            yield i
        i += 1
        remains >>= 1


@memoize
def calc(n, s, remains):
    if n == 1:
        if remains & (1 << s):
            return 1
        else:
            return 0
    if s <= 0:
        return 0

    return sum(calc(n - 1, s - n * m, remains ^ (1 << m)) for m in get_num(remains) if s - n * m >= 0)


for line in sys.stdin:
    n, s = map(int, line.split())
    print(calc(n, s, (1 << 10) - 1))

Submission status: Accepted

Solution code:
import sys
def oncount(x):
    r=0
    while x>0:
        r+=x&1
        x=x>>1
    return r

dp=[[0]*331 for i in range(1024)]
for i in range(10):
    dp[1<<i][i]=1
for i in range(1024):
    for j in range(331):
        t=oncount(i)
        for k in range(10):
            if 1&(i>>k):
                x=j-t*k
                if x>=0: dp[i][j]+=dp[i&~(1<<k)][x]
                
for a in sys.stdin:
    n,s=map(int,a.split())
    ans=0
    if s<=330:
        for i in range(1024):
            if oncount(i)==n:ans+=dp[i][s]
    print ans

Submission status: Accepted

Solution code:
maxs=[0]
num=0
for i in range(1,11):
    sum=0
    for j in range(i):
        sum+=(10-j-1)*(i-j)
    maxs.append(sum)


def solve(used,rest,sum,memo):
    
    if rest==0:
        return sum==0
    else:
        num=0
        for i in range(10):
            num*=2
            if used[i]:num+=1

        if (num,rest,sum) in memo:
            return memo[(num,rest,sum)]
        if sum<0:return 0
        if sum>maxs[rest]:return 0
        ans=0
        for i in range(10):
            if not used[i]:
                used[i]=True
                ans+=solve(used,rest-1,sum-(i)*rest,memo)
                used[i]=False
        memo[(num,rest,sum)]=ans
        return memo[(num,rest,sum)]

memo={}
while True:
    try:
        n,s=map(int,input().split())
        used=[False for i in range(10)]
        ans=solve(used,n,s,memo)
        print(ans)
    except:
        break

Submission status: Accepted

Solution code:
def solve(used,rest,sum,memo):
    
    if rest==0:
        return sum==0
    else:
        num=0
        for i in range(10):
            num*=2
            if used[i]:num+=1

        if (num,rest,sum) in memo:
            return memo[(num,rest,sum)]
        if sum<0:return 0
        ans=0
        for i in range(10):
            if not used[i]:
                used[i]=True
                ans+=solve(used,rest-1,sum-(i)*rest,memo)
                used[i]=False
        memo[(num,rest,sum)]=ans
        return memo[(num,rest,sum)]

memo={}
while True:
    try:
        n,s=map(int,input().split())
        used=[False for i in range(10)]
        ans=solve(used,n,s,memo)
        print(ans)
    except:
        break

Submission status: Accepted

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


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_numbers.reverse()
    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.07 [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

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


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