Project Euler:problem Coin Partitions

Source: Internet
Author: User

Let P (n) represent the number of different ways in which n coins can is separated into piles. For example, five coins can be separated to 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

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



Did not expect the numerical division results will be so large, according to the previous method half a day without results ...

Look at the other people's puzzle, okay?


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

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

(1 +  x  +  x 2 +  x 3 + ...) (1 +  x 2 +  x 4 +  x 6 + ...) (1 +  x 3 +  x 6 +  x 9 + ....

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

N = a 1 + 2a2 + 3a3 + ... = (1 + 1 + ... + 1) + (2 + 2 + ... + 2) + (3 + 3 + ... + 3) + ...,

where each number  i  appears  a i  times. This was precisely the definition of a partition of  n , so we have 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-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 are similar to the product fo Rmulation 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 is the generalized pentagonal nu Mbers; i.e., numbers of the form? m (3 m  ? 1), where  m  is an integer. The signs in the summation alternate as . This theorem can is 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 .) + P( k ? 15)? P ( K . 22)? ...

where P (0) is taken to equal 1, and P (k) are taken to being 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  ; Elsemark = -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;elsek = (3 * j*j-j)/2;} if (p[n] = = 0) break;n++;} cout << n << endl;system ("pause"); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Project Euler:problem Coin Partitions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.