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   O
O   O   O   O   O

Find the least value of n for which p(n) is divisible by one million.

没想到数值划分结果会那么大,按照前面的方法半天得不到结果。。。

看别人的题解知道了这个

The generating function for p(n) is given by:[5]

Expanding each factor on the right-hand side as a geometric series, we can rewrite it as

(1 + x + x2 + x3 + ...)(1 + x2 + x4 + x6 +
...)(1 + x3 + x6 + x9 + ...) ....

The xn term in this product counts the number of ways to write

n = a1 + 2a2 + 3a3 +
... = (1 + 1 + ... + 1) + (2 + 2 + ... + 2) + (3 + 3 + ... + 3) + ...,

where each number i appears ai times. This is precisely the definition of a partition of n, so our product is the desired generating function. More generally, the generating
function for the partitions of n into numbers from a set A can be found by taking only those terms in the product where k is an element of A. This result is due to Euler.

The formulation of Euler‘s generating function is a special case of a q-Pochhammer symbol and
is similar to the product formulation of many modular forms, and specifically the Dedekind
eta function
.

The denominator of the product is Euler‘s function and can be written,
by the pentagonal number theorem, as

where the exponents of x on the right hand side are the generalized pentagonal numbers;
i.e., numbers of the form ?m(3m ? 1), where m is an integer. The signs in the summation alternate as .
This theorem can be used to derive a recurrence for the partition function:

p(k) = p(k ? 1) + p(k ? 2) ? p(k ? 5) ? p(k ? 7) + p(k ? 12) + p(k ?
15) ? p(k ? 22) ? ...

where p(0) is taken to equal 1, and p(k) is taken to be zero for negative k.

#include <iostream>
#include <vector>
using namespace std;

int main()
{
	vector<int>p;
	p.push_back(1);
	int n = 1;
	while (1)
	{
		int i = 0;
		int k = 1;
		p.push_back(0);

		while (k <= n)
		{
			int mark;
			if (i % 4 == 0 || i % 4 == 1)
				mark = 1;
			else
				mark = -1;
			p[n] += mark*p[n - k];
			p[n] %= 1000000;
			i++;

			int j = i / 2 + 1;
			if (i % 2 != 0)
				k = (3 * j*j + j) / 2;
			else
				k = (3 * j*j - j) / 2;
		}
		if (p[n] == 0)
			break;
		n++;
	}
	cout << n << endl;
	system("pause");
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-24 00:06:33

Project Euler:Problem 78 Coin partitions的相关文章

Project Euler:Problem 31 Coin sums

In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation: 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p). It is possible to make £2 in the following way: 1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3

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 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 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 74 Digit factorial chains

The number 145 is well known for the property that the sum of the factorial of its digits is equal to 145: 1! + 4! + 5! = 1 + 24 + 120 = 145 Perhaps less well known is 169, in that it produces the longest chain of numbers that link back to 169; it tu

Project Euler:Problem 67 Maximum path sum II

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 in triangle.txt (righ

Project Euler:Problem 69 Totient maximum

Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number of numbers less than n which are relatively prime to n. For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, φ(9