Project Euler 78:coin Partitions

Source: Internet
Author: User

Coin Partitions

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

Coin Split

Note P (n) is the number of different ways to split n coins into piles. For example, five coins have 7 different ways of splitting piles, 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 smallest n value that enables P (n) to be divisible by 1 million.

Ideas:

How many kinds of splitting are there?

And judge whether it can be divisible by 1 million.

Reference: wiki, Partitionfunctionp

Law One:

According to this equation:

High-energy warning:

1. Here is a two-part and

2. When the first one does not meet the conditions, namely: N<k (3k-1)/2, the second one must not be established

3. The first satisfies the condition, the second may not satisfy the condition, here is said the condition is the array subscript cannot cross the bounds

4. All conditions must be calculated, only when the first one does not meet the conditions of the current cycle

5. The front ( -1) ^ (k+1), to multiply, expand the calculation, is to calculate the matching conditions of the array

Key program:

 for (k=1;k<=n;k++) {                = k* (3*k-1)/2;                 = gk1+k;                 if  Break ;                    Plist.set (N,plist.get (n)+flag*plist.get (n-gk1))                    ; if (gk2<=N) {                    plist.set (n,plist.get (n)+flag*plist.get (n-gk2));                    }                    Plist.set (N,plist.get (n)%limit);                                        Flag*=-1;            }

Here because I just saw in the above solution expression, cause I have been engaged for a long time did not get out, no culture is terrible

Law II:

See there's no problem here.

See here, directly according to the above expression solution, but here K is not from 1-n, here I understand wrong, thought to use it.

The method above does not work, the following methods are not, really wasted a lot of time

The following procedure has a process of seeking k, here is the true meaning Ah!!!

Key program:

 while (gk<=N) {                = (i%4>1)? -1:1;                Plist.set (N,plist.get (n)+flag*plist.get (n-gk));                Plist.set (N,plist.get (n)%limit);                I++                ; int  k= (i%2==0) i/2+1:-(i/2+1);                 = k* (3*k-1)/2;            }

Java Program:

 PackageLevel3;Importjava.util.ArrayList; Public classpe078{voidrun () {intLimit = 1000000;    PARTITIONS2 (limit); }    voidPartitions2 (intlimit) {ArrayList<Integer> plist =NewArraylist<integer>(); Plist.add (1); intn = 1;  while(true){            intGK1 =1; intGk2 =2; intK=1; Plist.add (0);//Initial Nth            intFlag = 1;  for(k=1;k<=n;k++) {Gk1= k* (3*k-1)/2; Gk2= gk1+K; if(gk1>n) Break; Plist.set (N,plist.get (n)+flag*plist.get (ngk1)); if(gk2<=N) {Plist.set (N,plist.get (n)+flag*plist.get (ngk2)); } plist.set (N,plist.get (n)%limit); Flag*=-1; }            if(Plist.get (n) ==0)                 Break; N++;    } System.out.println (n); }//55374//running Time=0s784ms    voidPARTITIONS1 (intlimit) {ArrayList<Integer> plist =NewArraylist<integer>(); Plist.add (1); intn = 1; intFlag;  while(true){            intGK = 1; inti = 0; Plist.add (0);  while(gk<=N) {Flag= (i%4>1)? -1:1; Plist.set (N,plist.get (n)+flag*plist.get (ngk)); Plist.set (N,plist.get (n)%limit); I++; intk= (i%2==0)? i/2+1:-(i/2+1); GK= k* (3*k-1)/2; }                        if(Plist.get (n) ==0)                 Break; N++;    } System.out.println (n); }//55374//running time=1s155ms     Public Static voidMain (string[] args) {LongT0 =System.currenttimemillis (); NewPE078 (). run (); LongT1 =System.currenttimemillis (); Longt = T1-t0; System.out.println ("Running Time=" +t/1000+ "s" +t%1000+ "MS"); }}

Fahsarm

and gives a way to ask for K.

Key program:

 while True:             = k * (3 * k-1)//2            = n- GK            If i < 0:                break            
    += ( -1) * * (k * k + 1) * p[i            ]ifelse 1 -        k p.append (PT)

Python program:

ImportTime ;defpartitions (limit): P= [1, 1, 2] n= 2 whiletrue:n+ = 1PT=0 I=0 K= 1 whileTrue:gk= k * (3 * k-1)//2I= N-GKifI <0: BreakPT+ = ( -1) * * (k * k + 1) *P[i] k=-1 * kifK > 0Else1-k P.append (PT)ifpt% Limit = =0:Print "n ="N"\ n"+"partition =", PT Break        if __name__=='__main__': T0=time.time () limit= 1000000partitions (limit) T1=time.time ()Print "running Time=", (T1-T0),"s"#n = 55374#running Time= 21.3049998283s

Description: Only the first method is my own writing, the other is seen in the Forum, self-organized

Project Euler 78: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.