AOJ0096 Sum of 4 Integers II

問題リンク Sum of 4 Integers II

  • 解法

よくあるタイプの数え上げ問題です。
(a, b)と(c, d)の組に分け、(a, b)で和sを作る組み合わせ数、(c, d)で和n-sを作る組み合わせ数を掛け合わせていけば解けます。

  • ソース
import java.util.Scanner;

//Sum of 4 Integers II
public class AOJ0096 {

	void run(){
		Scanner sc = new Scanner(System.in);
		int[] a = new int[2001];
		for(int i=0;i<=1000;i++)for(int j=0;j<=1000;j++)a[i+j]++;
		while(sc.hasNext()){
			int s = sc.nextInt(), res = 0;
			for(int i=Math.min(s, 2000);i>=0;i--)res+=s-i<=2000?a[i]*a[s-i]:0;
			System.out.println(res);
		}
	}
	
	public static void main(String[] args) {
		new AOJ0096().run();
	}
}