Project Euler:Problem 15 Lattice paths

Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.

How many such routes are there through a 20×20 grid?

很简单嘛~C(20,40)

但是在计算的时候遇到了麻烦,这个结果是有多大啊(╯‵□′)╯︵┻━┻

看来需要一边乘的同时一边除,同时还有注意精度问题

发现11~20间的数都能在上面找到对应的两倍数,化简一下:

#include <iostream>
using namespace std;

unsigned long long p(int a)
{
	unsigned long long res = 1;
	for (int i = 1; i <= a; i++)
	{
		res *= i;
	}
	return res;
}

int main()
{
	unsigned long long res = 1;
	for (int i = 39; i >= 21; i--)
	{
		res = res*i;
		i--;
	}
	res = res * 1024;
	res = res / p(10);
	cout << res << endl;
	system("pause");
	return 0;
}

上面是比较呆滞的解法,下面用递归来解决:

从点(1,1)到点(m,n)的路径数为:res[m][n]=res[m-1][n]+res[m][n-1]    res[1][1]=1  res[1][0]=0  res[0][1]=0

因为到达点(m,n),可以是从点(m-1,n)来的,也可以是从(m,n-1)来的

初始点的坐标为(1,1),终点的坐标点应该为(m+1,n+1)

所以初始的res应该为22*22的二维数组。

#include <iostream>
using namespace std;

int main()
{
	unsigned long long res[22][22];
	memset(res, 0, sizeof(res));
	for (int i = 1; i <= 21; i++)
	{
		for (int j = 1; j <= 21; j++)
		{
			if (i == 1 && j == 1)
				res[i][j] = 1;
			else
				res[i][j] = res[i - 1][j] + res[i][j - 1];
		}
	}
	cout << res[21][21] << endl;
	system("pause");
	return 0;
}
时间: 2024-10-25 09:52:25

Project Euler:Problem 15 Lattice paths的相关文章

Project Euler:Problem 46 Goldbach&#39;s other conjecture

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2×12 15 = 7 + 2×22 21 = 3 + 2×32 25 = 7 + 2×32 27 = 19 + 2×22 33 = 31 + 2×12 It turns out that the conjecture was f

Project Euler:Problem 40 Champernowne&#39;s constant

An irrational decimal fraction is created by concatenating the positive integers: 0.123456789101112131415161718192021... It can be seen that the 12th digit of the fractional part is 1. If dn represents the nth digit of the fractional part, find the v

Project Euler:Problem 78 Coin partitions

Let p(n) represent the number of different ways in which n coins can be separated into piles. For example, five coins can be separated into piles in exactly seven different ways, so p(5)=7. OOOOO OOOO   O OOO   OO OOO   O   O OO   OO   O OO   O   O 

Project Euler:Problem 18 Maximum path sum I

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom of the triangle below

Project Euler:Problem 84 Monopoly odds

In the game, Monopoly, the standard board is set up in the following way: GO A1 CC1 A2 T1 R1 B1 CH1 B2 B3 JAIL H2   C1 T2   U1 H1   C2 CH3   C3 R4   R2 G3   D1 CC3   CC2 G2   D2 G1   D3 G2J F3 U2 F2 F1 R3 E3 E2 CH2 E1 FP A player starts on the GO squ

Project Euler:Problem 90 Cube digit pairs

Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers. For example, the square number 64

Project Euler:Problem 89 Roman numerals

For a number written in Roman numerals to be considered valid there are basic rules which must be followed. Even though the rules allow some numbers to be expressed in more than one way there is always a "best" way of writing a particular number

Project Euler:Problem 88 Product-sum numbers

A natural number, N, that can be written as the sum and product of a given set of at least two natural numbers, {a1, a2, ... , ak} is called a product-sum number: N = a1 + a2 + ... + ak = a1 × a2 × ... × ak. For example, 6 = 1 + 2 + 3 = 1 × 2 × 3. Fo

Project Euler:Problem 58 Spiral primes

Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed. 37 36 35 34 33 32 31 38 17 16 15 14 13 30 39 18  5  4  3 12 29 40 19  6  1  2 11 28 41 20  7  8  9 10 27 42 21 22 23 24 25 26 43 44 45 46