Problem:


４つの整数の和 II

4,000 以下の正の整数 n を入力し、0 〜 1000 の範囲の整数 a, b, c, d の組で

a + b + c + d = n

を満たすものの組み合わせ数を出力するプログラムを作成して下さい。

Input

複数のデータセットが与えられます。各データセットに n が１行に与えられます。入力の最後まで処理して下さい。

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

Output

各データセットごとに、a, b, c, d の組み合わせの個数を１行に出力して下さい。

Sample Input

2
3
35

Output for the Sample Input

10
20
8436



Sample input:
2
3
35


Sample output:
10
20
8436

Submission status: Wrong Answer

Solution code:
import sys
for line in sys.stdin.readlines():
  n = int(line.rstrip())
  count = 0
  for i in range(min(2001,n+1)):
    if n - i >= 0:
      count += (i+1)*(n-i+1)
  print(count)

Submission status: Wrong Answer

Solution code:
import sys
for line in sys.stdin.readlines():
  n = int(line.rstrip())
  count = 0
  for i in range(1001):
    if 0 <= n - i <= 1000:
      count += (n-i+1)*(i+1)
    elif 1000 <= n-i:
      count += (2000-(n-i)+1)*(i+1)
  for i in range(1001,2001):
    if 0 <= n - i <= 1000:
      count += (n-i+1)*(2000-i+1)
    elif 1000 <= n-i:
      count += (2000-(n-i)+1)*(2000-i+1)
  print(count)

Submission status: Wrong Answer

Solution code:
import sys
for line in sys.stdin.readlines():
  n = int(line.rstrip())
  count = 0
  for i in range(1001):
    if 0 <= n - i <= 1000:
      count += (n-i+1)*(i+1)
    elif 1001 <= n-i:
      count += (2000-(n-i)+1)*(i+1)
  for i in range(1001,2001):
    if 0 <= n - i <= 1000:
      count += (n-i+1)*(2000-i+1)
    elif 1001 <= n-i:
      count += (2000-(n-i)+1)*(2000-i+1)
  print(count)

Submission status: Wrong Answer

Solution code:
def solve(m):
	n=m+1
	#(n-1)n(2n-1)/12+3(n-1)n/4+n
	print(int((n-1)*n*(2*n-1)/12+3*(n-1)*n/4+n))

while True:
	try:
		n=int(input())
		solve(n)
	except EOFError:
		break

Submission status: Wrong Answer

Solution code:
def solve(m):
	n=m+1
	#(n-1)n(2n-1)/12+3(n-1)n/4+n
	print(int(n+n*(n-1)*(n+4)/6))

while True:
	try:
		n=int(input())
		solve(n)
	except EOFError:
		break

Submission status: Wrong Answer

Solution code:
def calc(n):
	return int(n+n*(n-1)*(n+4)/6)






def solve(m):
	n=m+1
	middle=2000
	if n<=2000:
		return calc(n)
	else:
		return calc(4002-n)




	
while True:
	try:
		n=int(input())
		print(solve(n))
	except EOFError:
		break

Submission status: Wrong Answer

Solution code:
def calc(n):
	#n+n*(n+4)*(n-1)/6
	n=n+1
	return int(n+n*(n+4)*(n-1)/6)






def solve(n):
	#the range of n is 0-4000
	#the center is 2000.5 
	if n<=2000:
		return calc(n)
	else:
		return calc(4000-n)
	



	
while True:
	try:
		n=int(input())
		print(solve(n))
	except EOFError:
		break

Submission status: Wrong Answer

Solution code:
def calc(n):
	#n+n*(n+4)*(n-1)/6
	n=n+1
	return int(n+n*(n+4)*(n-1)/6)



def solve(n):
	#the range of n is 0-4000
	#the center is 2000.5 
	if n<2000:
		return calc(n)
	else:
		return calc(4000-n)
	

	
while True:
	try:
		n=int(input())
		print(solve(n))
	except EOFError:
		break

Submission status: Wrong Answer

Solution code:
A=[i+1 for i in range(2001)]
 
while(1):
    try:
        n=int(raw_input())
        ans=0
        for i in range(n+1):
            ans+=A[i]*A[n-i]
        print ans
    except:
        break

Submission status: Wrong Answer

Solution code:
n=input()
print (n**3+6*n*n+11*n+6)/6

Submission status: Wrong Answer

Solution code:
while True:
    try:
        N = int(input())
    except EOFError:
        break
    ans = (N + 3) * (N + 2) * (N + 1) // 6
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
a=[0]*51
for i in range(19):a[i]=a[36-i]=(i+3)*(i+2)*(i+1)//6-[0,a[i-10]*4][i>9]
for e in sys.stdin:print(a[int(e)])

Submission status: Wrong Answer

Solution code:
import sys
a=[0]*4001
for i in range(2001):a[i]=a[4000-i]=(i+3)*(i+2)*(i+1)//6-[0,a[i-1000]*4][i>999]
for e in sys.stdin:print(a[int(e)])

Submission status: Wrong Answer

Solution code:
def get_input():
    while True:
        try:
            yield ''.join(input())
        except EOFError:
            break

ans2 = [0 for i in range(4001)]
ans3 = [0 for i in range(4001)]
ans4 = [0 for i in range(4001)]

for i in range(1001):
    ans2[i] = i + 1
for i in range(1001,4001):
    ans2[i] = 1001

for i in range(4001):
    for j in range(i+1):
        ans3[i] += ans2[j]

for i in range(4001):
    for j in range(i+1):
        ans4[i] += ans3[j]

#print("DONE")
N = list(get_input())
for l in range(len(N)):
    print(ans4[int(N[l])])

Submission status: Wrong Answer

Solution code:
while True:
    try:
        n=input()
        print reduce(lambda x,y:x*y,range(n+1,n+3+1))/6
    except EOFError:
        break

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit


def main():
    for line in stdin:
        print(sum(i * (i + 1) // 2 for i in range(1, int(line)+2)))
    exit()


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
while 1:
    try:
        n = input()
        print (n+1)*(n+2)*(n+3)/6
    except EOFError:
        break

Submission status: Wrong Answer

Solution code:
while 1:
    try:
        n = input(); p = min(2000, n);
        print (p+1)*(p+2)*(3*n-2*p+3)/6
    except EOFError:
        break

Submission status: Wrong Answer

Solution code:
while True:
    try:
        n = int(raw_input())
        if n > 2000:
            n = 4000 - n
        print n*(n+1)*(n+2)/6
    except:
        break

Submission status: Wrong Answer

Solution code:
while True:
    try:
        n = int(raw_input())
        if n > 2000:
            n = 4001 - n
        print n*(n+1)*(n+2)/6
    except:
        break

Submission status: Wrong Answer

Solution code:
while True:
    try:
        n = int(raw_input())
        if n > 2000:
            n = 4000 - n
        print (n+1)*(n+2)*(n+3)/6
    except:
        break

Submission status: Time Limit Exceeded

Solution code:
data_map = [0]*4001

for i1 in range(1001):
  for i2 in range(1001):
    for i3 in range(1001):
      for i4 in range(1001):
        data_map[i1+i2+i3+i4] += 1

while True:
  try:
    line = int(raw_input())
  except:
    break
  print data_map[line][3]

Submission status: Time Limit Exceeded

Solution code:
while 1:
	try:
		stack = []
		st = raw_input().split()
		for s in st:
			if s == "-":
				stack.append(stack.pop(-2) - stack.pop())
			elif s == "+":
				stack.append(stack.pop(-2) + stack.pop())
			elif s == "/":
				stack.append(stack.pop(-2) / stack.pop())
			elif s == "*":
				stack.append(stack.pop(-2) * stack.pop())
			elif s == "%":
				stack.append(stack.pop(-2) % stack.pop())
			else:
				stack.append(float(s))
	
		print stack[0]
	except:
		pass

Submission status: Time Limit Exceeded

Solution code:
while 1:
	try:
		stack = []
		st = map(str, raw_input().split())
		for s in st:
			if s == "-":
				stack.append(stack.pop(-2) - stack.pop())
			elif s == "+":
				stack.append(stack.pop(-2) + stack.pop())
			elif s == "/":
				stack.append(stack.pop(-2) / stack.pop())
			elif s == "*":
				stack.append(stack.pop(-2) * stack.pop())
			elif s == "%":
				stack.append(stack.pop(-2) % stack.pop())
			else:
				stack.append(float(s))
	
		print stack[0]
	except:
		pass

Submission status: Time Limit Exceeded

Solution code:
def doc_string():
    """
    I can fly.
    """
while True:
    if __name__ == "__main__":
        count = 0
        judge = False
        try:
            n = int(raw_input())
        except EOFError:
            break
        for a in xrange(1000):
            if a > n:
                break
            for b in xrange(1000):
                if b > n:
                    break
                for c in xrange(1000):
                    if c > n:
                        break
                    for d in xrange(1000):
                        if a + b + c + d == n:
                            count += 1
                        if d > n:
                            break
        print count

Submission status: Time Limit Exceeded

Solution code:
def solve(n):
	ans=0
	for a in range(n+1):
		for b in range(n+1):
			if a+b>n:
				break
			for c in range(n+1):
				if n-(a+b+c)>=0:
					ans+=1
				else:
					break
	return ans


while True:
	try:
		n=int(input())
		print(solve(n))
	except EOFError:
		break

Submission status: Time Limit Exceeded

Solution code:
def solve(n):
	ans=0
	for a in range(int(n/4)+1):
		for b in range(a,int(n/3)+1):
			for c in range(b,int(n/2)+1):
				for d in range(c,n+1):
					if a+b+c+d==n:
						if a==b==c==d:
							ans+=1
						elif a==b==c or b==c==d:
							ans+=4
						elif a==b and c==d:
							ans+=6
						elif (a==b and c!=d) or (b==c) or (a!=b and c==d):
							ans+=12
						else:
							ans+=24
	return ans 


while True:
	try:
		n=int(input())
		print(solve(n))
	except EOFError:
		break

Submission status: Time Limit Exceeded

Solution code:
def solve(n):
	ans=0
	for a in range(int(n/4)+1):
		for b in range(a,int(n/3)+1):
			for c in range(b,int(n/2)+1):
				d=n-(a+b+c)
				if d>=c:
					if a==b==c==d:
						ans+=1#4!/4!
					elif a==b==c or b==c==d:
						ans+=4#4!/3!
					elif a==b and c==d:
						ans+=6#4!/(2!*2!)
					elif (a==b and c!=d) or (b==c) or (a!=b and c==d):
						ans+=12#4!/2!
					else:
						ans+=24#4!
				else:
					break
	return ans 


while True:
	try:
		n=int(input())
		print(solve(n))
	except EOFError:
		break

Submission status: Time Limit Exceeded

Solution code:
def solve(n):
	ans=0
	for a in range(int(n/4)+1):
		for b in range(a,int(n/3)+1):
			if b>1000:
				break
			for c in range(b,int(n/2)+1):
				if c>1000:
					break
				d=n-(a+b+c)
				if d>=c:
					if a==b==c==d:
						ans+=1#4!/4!
					elif a==b==c or b==c==d:
						ans+=4#4!/3!
					elif a==b and c==d:
						ans+=6#4!/(2!*2!)
					elif (a==b and c!=d) or (b==c) or (a!=b and c==d):
						ans+=12#4!/2!
					else:
						ans+=24#4!
				else:
					break
	return ans 


while True:
	try:
		n=int(input())
		print(solve(n))
	except EOFError:
		break

Submission status: Time Limit Exceeded

Solution code:
b=[0]*2001
for i in range(1001):
    for j in range(1001):
        b[i+j]+=1
while 1:
    try:n=int(input())
    except:break
    c=0;a=[1000,n][n<1000]
    for i in range(2*a+1):
        for j in range(2*a+1):
            if i+j==n:c+=b[i]*b[j]
    print(c)

Submission status: Time Limit Exceeded

Solution code:
while True:
    m = 0
    try:
        n = int(input().strip())
        for a in range(10001):
            if n - a <0:
                break
            for b in range(10001):
                if n - (a+b) <0:
                    break
                for c in range(10001):
                    if n - (a+b+c) <0:
                        break
                    for d in range(10001):
                        if n - (a+b+c+d) <0:
                            break
                        if a+b+c+d == n:
                            m += 1
        print(m)
    except EOFError:
        break

Submission status: Time Limit Exceeded

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

import sys
import os
import math

for s in sys.stdin:
    n = int(s)

    T = [0] * 2001
    for a in range(1001):
        for b in range(1001):
            T[a+b] += 1

    sum_n_num = 0
    for a_b_sum in range(0, n+1):
        c_d_sum = n - a_b_sum
        if a_b_sum <= 2000 and c_d_sum <= 2000:
            sum_n_num += T[a_b_sum] * T[c_d_sum]

    print(sum_n_num)

Submission status: Time Limit Exceeded

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

import sys
import os

for s in sys.stdin:
    n = int(s)

    T = [0] * 2001
    for a in range(1001):
        for b in range(1001):
            T[a+b] += 1

    sum_n_num = 0
    for a_b_sum in range(0, n+1):
        c_d_sum = n - a_b_sum
        if a_b_sum <= 2000 and c_d_sum <= 2000:
            sum_n_num += T[a_b_sum] * T[c_d_sum]

    print(sum_n_num)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools as it
line = sys.stdin.readlines()
for n in line:
    n = int(n)
    num = {i for i in range(0,n+1)}
    count = 0
    for i in it.product(num, repeat=4):
        if sum(i) == n:
            count += 1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Sum of 4 Integers II
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0096

"""
import sys
from sys import stdin
from collections import defaultdict
input = stdin.readline


def main(args):
    lut = defaultdict(list)
    for a in range(1001):
        for b in range(1001):
            lut[a+b].append((a, b))

    for line in sys.stdin:
        n = int(line)
        ans = 0
        for i in range(n+1):
            for a, b in lut[i]:
                for c, d in lut[n-i]:
                    ans += 1
        print(ans)


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

Submission status: Time Limit Exceeded

Solution code:
def get_input():
    while True:
        try:
            yield ''.join(input())
        except EOFError:
            break

ans1 = [1 for i in range(4001)]
ans2 = [0 for i in range(4001)]
ans3 = [0 for i in range(4001)]
ans4 = [0 for i in range(4001)]

for i in range(1001):
    ans1[i] = 1
for i in range(1001,4001):
    ans1[i] = 0

for i in range(4001):
    for j in range(i+1):
        ans2[i] += ans1[j]

for i in range(4001):
    for j in range(i+1):
        ans3[i] += ans2[j]

for i in range(4001):
    for j in range(i+1):
        ans4[i] += ans3[j]

N = list(get_input())
for l in range(len(N)):
    print(ans4[int(N[l])])

Submission status: Time Limit Exceeded

Solution code:
L=[0]*2001
for i in range(1001):
    for j in range(1001):
            L[i+j]+=1
while True:
    cnt=0
    try:
        n=input()
    except EOFError:
	break
    for i in range(1001):
        for j in range(1001):
            if 0<=n-i-j<=2000:
                cnt+=L[n-(i+j)]
    print cnt

Submission status: Time Limit Exceeded

Solution code:
L=[0]*2001
for i in range(1001):
    for j in range(1001):
            L[i+j]+=1
while True:
    cnt=0
    try:
        n=input()
    except EOFError:
	break
    for i in range(1001):
        if i>n:break
        for j in range(1001):
            if i+j>n:break
            if 0<=n-i-j<=2000:
                cnt+=L[n-(i+j)]
    print cnt

Submission status: Time Limit Exceeded

Solution code:
T = 1000
memo = [[0] * (2 * T + 1) for _ in range(3)] 
#Java: int[][] memo = new int[3][2*T+1];
memo[0][0] = 1
for i in range(1,3):
    for j in range(i*T+1):
        memo[i][j] = sum([memo[i-1][k] for k in range(max(0,j-T),j+1)])

while True:
    try:
        n = input() 
    except EOFError:
        break
    cnt = 0
    for a in range(T+1):
        for b in range(T+1):
            cnt += memo[2][n-(a+b)] if 0 <= n - (a + b) <= 2 * T else 0 
            # 0 <= n - (a + b) <= 2 * T ? memo[n-(a+b)] : 0
    print cnt

#O(T^2), 2.15s

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit


def main():
    for line in stdin:
        n = int(line)
        count = 0

        for a in range((n if n < 1000 else 1000) + 1):
            for ab in range(a, a+1001):
                if ab > n:
                    break

                nab = n - ab
                start = nab if nab < 1000 else 1000
                end = (nab-1001) if (nab-1001) > -1 else -1
                combi = start - end
                if combi >= 0:
                    count += combi

        print(count)
    exit()


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit


def main():
    ans = [0] * 4001
    for line in stdin:
        n = int(line)

        if not ans[n]:
            count = 0
            for a in xrange((n if n < 1000 else 1000) + 1):
                e1 = n-a-1001 if n-a-1001 >= 0 else -1
                for nab in xrange(n-a, e1, -1):
                    ran = ((nab if nab < 1000 else 1000) -
                           ((nab-1001) if nab > 1000 else -1))
                    if ran >= 0:
                        count += ran

            ans[n] = count
        print(ans[n])
    exit()


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit


def calc_combination(n):
    count = 0
    for a in xrange((n if n < 1000 else 1000) + 1):
        e1 = n-a-1001 if n-a-1001 >= 0 else -1
        for nab in xrange(n-a, e1, -1):
            ran = ((nab if nab < 1000 else 1000) -
                   ((nab-1001) if nab > 1000 else -1))
            if ran >= 0:
                count += ran
    return count


def main():
    ans = [0] * 4001
    for line in stdin:
        n = int(line)

        if not ans[n]:
            count = calc_combination(n)
            ans[n] = count
            ans[4000-n] = count
        print(ans[n])
    exit()


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
while True:
    try:
        n = int(raw_input())
        count = 0
        for a in range(1000,-1,-1):
            if 0 <= n - a <= 3000:
                for b in range(1000,-1,-1):
                    if 0 <= n - a - b <= 1000:
                        count += n-a-b+1
                    elif 1001 < n - a - b <= 2000:
                        count += (2000 - (n-a-b) + 1)
        print count
    except:
        break

Submission status: Time Limit Exceeded

Solution code:
while True:
    try:
        n = int(raw_input())
        count = 0
        for a in range(1000,-1,-1):
             if 0 <= n - a <= 3000:
                for b in range(1000,-1,-1):
                    if n - a - b > 2000:
                        break
                    elif 0 <= n - a - b <= 1000:
                        count += n-a-b+1
                    elif 1001 < n - a - b <= 2000:
                        count += (2000 - (n-a-b) + 1)
        print count
    except:
        break

Submission status: Time Limit Exceeded

Solution code:
while True:
    try:
        n = int(raw_input())
        count = 0
        for a in range(1000,-1,-1):
            na = n - a
            if 3000 < na:
                break
            elif 0 <= na <= 3000:
                for b in range(1000,-1,-1):
                    nab = n - a - b
                    if nab > 2000:
                        break
                    elif 0 <= nab <= 1000:
                        count += nab+1
                    elif 1001 < nab <= 2000:
                        count += (2000 - nab + 1)
        print count
    except:
        break

Submission status: Time Limit Exceeded

Solution code:
def count(num, s, c):
	ans = 0
	if c == 4:
		return True if s == num else False
	for i in range(num+1):
		s += i
		if s > num:
			s -= i
			continue
		ans += count(num, s, c+1)
		s -= i
	return ans

while True:
	try:
		n = input()
		print count(n, 0, 0)
	except:
		break

Submission status: Time Limit Exceeded

Solution code:
def count(num, s, c):
	ans = 0
	if c == 4:
		return True if s == num else False
	for i in range(num+1):
		s += i
		if s >= num:
			if s > num:
				s -= i
				continue
			else:
				ans += 1
				s -= i
				continue
		else:
			ans += count(num, s, c+1)
		s -= i
	return ans

while True:
	try:
		n = input()
		print count(n, 0, 0)
	except:
		break

Submission status: Time Limit Exceeded

Solution code:
def count(num, s, c):
	ans = 0
	if c == 4:
		return True if s == num else False
	for i in range(num+1 if num <= 1000 else 1001):
		s += i
		if s >= num:
			if s > num:
				s -= i
				continue
			else:
				ans += 1
				s -= i
				continue
		else:
			ans += count(num, s, c+1)
		s -= i
	return ans

while True:
	try:
		n = input()
		print count(n, 0, 0)
	except:
		break

Submission status: Accepted

Solution code:
data_map = [[0] + [1]*1001 + [0]*3000, [0]*4002, [0]*4002, [0]*4002]
for i in range(1,4):
  for j in range(1,4002):
    k = 0 if j < 1000 else j+1-1001
    data_map[i][j] = sum(data_map[i-1][k:j+1])

while True:
  try:
    line = int(raw_input())
  except:
    break
  print data_map[3][line+1]

Submission status: Accepted

Solution code:
data_map = [[0] + [1]*1001 + [0]*3000, [0]*4002, [0]*4002, [0]*4002]
for i in range(1,4):
  for j in range(1,4002):
    k = 0 if j < 1000 else j-1000
    data_map[i][j] = sum(data_map[i-1][k:j+1])

while True:
  try:
    line = int(raw_input())
  except:
    break
  print data_map[3][line+1]

Submission status: Accepted

Solution code:
import sys
a=[0]*2001
b=[0]*4001
n=1001
for i in range(n):
  for j in range(n):
    a[i+j]+=1
n=2001
for i in range(n):
  for j in range(n):
    b[i+j]+=a[i]*a[j]
for i in sys.stdin:
  print b[int(i)]

Submission status: Accepted

Solution code:
import sys

n=1001
a=[0]*2001
for i in range(n):
  for j in range(n):
    a[i+j]+=1
for n in map(int,sys.stdin):
  x=0
  for i in range(max(0,n-2000),min(n,2000)+1):
    x+=a[i]*a[n-i]
  print x

Submission status: Accepted

Solution code:
import sys
n=1001
a=range(1,n)
a+=[n]+a[::-1]
for n in map(int,sys.stdin):
  x=0
  for i in range(max(0,n-2000),min(n,2000)+1):
    x+=a[i]*a[n-i]
  print x

Submission status: Accepted

Solution code:
import sys
n=1001
a=range(1,n)
a+=[n]+a[::-1]

for n in map(int,sys.stdin):
  x=0
  b,c=0,n
  if n>2000: b,c=n-2000,2000
  for i in range(b,c+1):
    x+=a[i]*a[n-i]
  print x

Submission status: Accepted

Solution code:
import sys
n=1001
a=range(1,n)
a+=[n]+a[::-1]
for n in map(int,sys.stdin):
  print sum([a[i]*a[n-i] for i in range(max(0,n-2000),min(n,2000)+1)])

Submission status: Accepted

Solution code:
import sys
for line in sys.stdin.readlines():
  n = int(line.rstrip())
  count = 0
  for i in range(1001):
    if 0 <= n - i <= 1000:
      count += (n-i+1)*(i+1)
    elif 1001 <= n-i <= 2000 :
      count += (2000-(n-i)+1)*(i+1)
  for i in range(1001,2001):
    if 0 <= n - i <= 1000:
      count += (n-i+1)*(2000-i+1)
    elif 1001 <= n-i <= 2000:
      count += (2000-(n-i)+1)*(2000-i+1)
  print(count)

Submission status: Accepted

Solution code:
import sys
def N(n):
    return (n+1)*(n+2)*(n+3)/6
for n in sys.stdin:
    n=int(n)
    if n>2000:
        n=4000-n
    if n<1001:
        print N(n)
    else:
        m=n-1000
        print N(n)-(N(2*m)-m-1)/2
exit()

Submission status: Accepted

Solution code:
M = 1000
DM = M * 2 + 1
 
def count(n):
    global M
    res = n + 1
    if n > M:
        res -= (n-M) * 2
    return res
 
def solve(n):
    cnt = 0
    for a in range(min(DM, n), -1, -1):
        b = n - a
        if b > DM:
            break
        cnt += count(a) * count(b)
    return cnt
 
while True:
    try:
        l = input()
    except EOFError:
        break
    nm = int(l)
    print(solve(nm))

Submission status: Accepted

Solution code:
import sys

p = [0] * 4001
v = 0
for i in range(2001):
    t = (i+3)*(i+2)*(i+1)//6
    v = v + 2 * (i-999) * (i-1000) if i > 1000 else 0
    p[i] = t - v
    p[4000-i] = p[i]

for i in sys.stdin:
    print(p[int(i)])

Submission status: Accepted

Solution code:
def Kosuu(n,sak):
    if n <= sak:
        kazu = n + 1
        sta = 0
        end = n
    else:
        kazu = sak * 2 + 1 - n
        sta = n - sak
        end = sak
    return [kazu,sta,end]
    
while True:
    try:
        Sum = 0
        n = int(input())
        ABl = Kosuu(n,2000)
        
        for i in range(ABl[1],ABl[2] + 1):
            Sum += Kosuu(i,1000)[0] * Kosuu(n -i,1000)[0]
        print(Sum)
    except EOFError:
        break

Submission status: Accepted

Solution code:
A=[i+1 for i in range(1000)]
A+=reversed(A)
A.insert(1000,1001)
while(1):
    try:
        n=int(raw_input())
        ans=0
        for i in range(n+1):
            if i<2001 and n-i<2001:
                ans+=A[i]*A[n-i]
        print ans
    except:
        break

Submission status: Accepted

Solution code:
s=[0]*4001
for i in range(2001):
    a=a+2*(i-999)*(i-1000) if i>1000 else 0
    s[i]=(i+3)*(i+2)*-~i//6-a
    s[4000-i]=s[i]
while 1:
    try:print(s[int(input())])
    except:break

Submission status: Accepted

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

import sys
import os
import math

T = [0] * 2001
for a in range(1001):
    for b in range(1001):
        T[a + b] += 1

for s in sys.stdin:
    n = int(s)

    sum_n_num = 0
    for a_b_sum in range(0, n+1):
        c_d_sum = n - a_b_sum
        if a_b_sum <= 2000 and c_d_sum <= 2000:
            sum_n_num += T[a_b_sum] * T[c_d_sum]

    print(sum_n_num)

Submission status: Accepted

Solution code:
ab = [0 for _ in range(2001)]
for a in range(1001):
    for b in range(1001):
        ab[a+b] += 1

while True:
    try:
        n = int(input())
    except:
        break
    ans = sum(ab[i]*ab[n-i] for i in range(2001) if n-i >= 0 and  n-i <= 2000)
    print(ans)

Submission status: Accepted

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


Memo = [0 for _ in range(2001)]
def init_memo():
    global Memo
    # a, b??????????????°?????§ 0???2000?????????????????????????????°??????????????????
    for a in range(1000+1):
        for b in range(1000+1):
            Memo[a+b] += 1


def solve(target):
    global Memo
    ans = 0
    for ab in range(min(target+1, 2001)):
        cd = target - ab
        if 0 <= cd <= 2000:
            ans += Memo[ab] * Memo[cd]
    return ans


def main(args):
    init_memo()
    for line in sys.stdin:
        target = int(line.strip())
        result = solve(target)
        print(result)


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

Submission status: Accepted

Solution code:
import sys
hist = [0 for i in range(4001)]
for i in range(1001):
    for j in range(1001):
        hist[i + j] += 1

for line in sys.stdin:
    ans = 0
    n = int(line)
    for i in range(min(n, 2000) + 1):
        ans += (hist[i] * hist[n-i])
    print(ans)

Submission status: Accepted

Solution code:
import sys
a=[0]*4001
for i in range(2001):a[i]=a[4000-i]=(i+3)*(i+2)*(i+1)//6-a[i-1001]*4*(i>999)
for e in sys.stdin:print(a[int(e)])

Submission status: Accepted

Solution code:
import sys
m=1000;n=m*4
a=[0]*-~n
for i in range(2001):a[i]=a[n-i]=(i+3)*(i+2)*(i+1)//6-a[~-i-m]*4*(i>~-m)
for e in sys.stdin:print(a[int(e)])

Submission status: Accepted

Solution code:
import sys
a=[0]*4001
for i in range(2000):a[i]=a[4000-i]=(i+3)*(i+2)*(i+1)//6-a[i-1001]*4*(i>999)
for e in sys.stdin:print(a[int(e)])

Submission status: Accepted

Solution code:
import sys
a=[0]*4001
for i in range(1999):a[i]=a[4000-i]=(i+3)*(i+2)*(i+1)//6-a[i-1001]*4*(i>999)
for e in sys.stdin:print(a[int(e)])

Submission status: Accepted

Solution code:
import sys
a=[0]*4001
for i in range(2001):a[i]=a[4000-i]=(i+3)*-~-~i*-~i//6-a[i-1001]*4*(i>999)
for e in sys.stdin:print(a[int(e)])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Sum of 4 Integers II
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0096

"""
import sys
from sys import stdin
from collections import defaultdict
input = stdin.readline


def main(args):
    lut = defaultdict(int)
    for a in range(1001):
        for b in range(1001):
            lut[a+b] += 1

    for line in sys.stdin:
        n = int(line)
        ans = 0
        for i in range(n+1):
            ans += lut[i] * lut[n-i]
        print(ans)


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

Submission status: Accepted

Solution code:
ans = [0 for i in range(4001)]

for i in range(2001):
    if i > 1000:
        a = a + 2*(i - 999)*(i - 1000)
    else:
        a = 0
    ans[i] = (i + 3)*(i + 2)*-~i//6 - a
    ans[4000 - i] = ans[i]

while True:
    try:
        n = int(input())
        print(ans[n])
    except:
        break

Submission status: Accepted

Solution code:
a = [0] * 4001
for i in range(2001):a[i] = a[4000 - i] = (i + 3) * (i + 2) * (i + 1) // 6 - 4 * a[i - 1001] * (i > 999)
while 1:
    try:print(a[int(input())])
    except:break

Submission status: Accepted

Solution code:
from collections import Counter

two_dict = Counter()

for i in range(1001):
  for j in range(1001):
    two_dict[i + j] += 1

while True:
  try:
    n = int(input())
    ans = 0
    for i in range(n + 1):
      ans += two_dict[i] * two_dict[n - i]
    print(ans)

  except EOFError:
    break

Submission status: Accepted

Solution code:
from collections import Counter

pair_dict = Counter()
for i in range(2001):
  pair_dict[i] = min(i, 2000 - i) + 1 

while True:
  try:
    n = int(input())
    ans = 0
    for i in range(n + 1):
      ans += pair_dict[i] * pair_dict[n - i]
    print(ans)

  except EOFError:
    break

Submission status: Accepted

Solution code:
def get_input():
    while True:
        try:
            yield ''.join(input())
        except EOFError:
            break

ans1 = [0 for i in range(4001)]
ans2 = [0 for i in range(4001)]
ans3 = [0 for i in range(4001)]
ans4 = [0 for i in range(4001)]

for i in range(1001):
    ans1[i] = 1
for i in range(1001,4001):
    ans1[i] = 0

for i in range(4001):
    for j in range(1001):
        ans2[i] += ans1[i-j]

for i in range(4001):
    for j in range(1001):
        ans3[i] += ans2[i-j]

for i in range(4001):
    for j in range(1001):
        ans4[i] += ans3[i-j]

#print("DONE")
N = list(get_input())
for l in range(len(N)):
    print(ans4[int(N[l])])

Submission status: Accepted

Solution code:
import sys
ab=[0 for _ in range(2001)]
for a in range(1001):
    for b in range(1001):
        ab[a+b]+=1
for l in sys.stdin:
    n=int(l[:-1])
    ans=sum(ab[i]*ab[n-i] for i in range(2001) if 0<=n-i<=2000)
    print(ans)

Submission status: Accepted

Solution code:
# AOJ 0096 Sum of 4 Integers II
# Python3 2018.6.15 bal4u

MAX = 1000

while True:
	try: n = int(input())
	except: break
	
	if n > 2*MAX: n = 4*MAX - n
	k = n+1
	ans = k*(k+1)*(k+2)//6
	if n > MAX:
		k = n-MAX
		ans -= 2 * k*(k+1)*(k+2)//3;
	print(ans)

Submission status: Accepted

Solution code:
import sys
table = [0] * 2001
for i in xrange(1001):
    for ii in xrange(1001):
        table[i+ii] += 1
for line in sys.stdin.readlines():
    count = 0
    n = int(line.strip())
    for i in xrange(2001 if n+1 > 2001 else n+1):
        if not n-i > 2000:
            count += table[n-i] * table[i]
    print count

Submission status: Accepted

Solution code:
T = 1000
N = 4000
memo = [[0] * (N +1)  for _ in range(3)] 
#Java: int[][] memo = new int[3][2*T+1];
memo[0][0] = 1
for i in range(1,3):
    for j in range(i*T+1):
        memo[i][j] = sum([memo[i-1][k] for k in range(max(0,j-T),j+1)])

while True:
    try:
        n = input() 
    except EOFError:
        break
    cnt = 0
    for i in range(n+1):
        cnt += memo[2][i] * memo[2][n-i]
    print cnt

#O(T^2), 0.954

Submission status: Accepted

Solution code:
T = 1000
N = 4000
dp = [[0] * (N + 1) for _ in range(5)] 
#Java: int[][] dp = new int[5][N];
dp[0][0] = 1
for i in range(1,5):
    for j in range(i*T+1):
        dp[i][j] = sum([dp[i-1][k] for k in range(max(0,j-T),j+1)])

while True:
    try:
        n = input() 
    except EOFError:
        break
    print dp[4][n]

#O(T^2), 0.954

Submission status: Accepted

Solution code:
T = 1000
def f(x):
    return max(0, T + 1 - abs(T - x))
while True:
    try:
        n = input() 
    except EOFError:
        break
    cnt = 0
    for i in range(n+1):
        cnt += f(i) * f(n-i)
    print cnt

#O(n),

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit


def make_combination_list():
    cmb = [0] * 4001
    count = minus = n = s = 0

    for i in range(1001):
        count += (i+1) * (i+2) // 2
        cmb[i] = cmb[4000-i] = count

    for i in range(1001, 2001):
        count += (i+1) * (i+2) // 2
        n += 1
        s += n
        minus += 4 * s
        cmb[i] = cmb[4000-i] = count - minus

    return cmb


def main():
    ans = make_combination_list()
    for line in stdin:
        print(ans[int(line)])


if __name__ == '__main__':
    main()