<MEMORY>Project Euler NO15

//从一个2×2网格的左上角开始,有6条(不允许往回走)通往右下角的路。

//对于20×20的网格,这样的路有多少条?



遍历法15 * 15 的网格需要花费3秒以上,故不能使用!!


import java.math.BigInteger;

public class Problem15
{
	
	static int n = 20;
	public static void main(String[] args)
	{
		long start = System.currentTimeMillis();
		System.out.print("answer:  ");
		howmany();
		long end = System.currentTimeMillis();
		System.out.print("time:  ");
		System.out.println(end - start);
	}
	
	
	static void howmany()
	{
		BigInteger s = BigInteger.valueOf(1);
		for (int i = 1; i <= 2 * n; i++)
		{
			s = s.multiply(BigInteger.valueOf(2));
		}
		for (int i = 2 * n; i > n; i--)
		{
			s = s.add(BigInteger.valueOf(2).multiply( CMN(2 * n, i)).negate());
		}
		
		System.out.println(s);
	}
	
	static BigInteger CMN(int m, int n)
	{
		BigInteger s1 = BigInteger.ONE;
		BigInteger s2 = BigInteger.ONE;
		for (int i = m; i > m - n; i--)
		{
			s1 = s1.multiply(BigInteger.valueOf(i));
		}
		
		for (int i = 2; i <= n; i++)
		{
			s2 = s2.multiply(BigInteger.valueOf(i));
		}
		return s1.divide(s2);
	}

}


answer:  137846528820
time:  5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值