Project Euler/158
From charlesreid1
We consider strings of n <= 26 diff chars from the alphabet.
For every n, p(n) is the number of strings of length n for which exactly one character comes lexicographically AFTER its neighbour to the left.
What is the maximum value of p(n)?
Overview of approach
For a string of length n, we can compute p(n) using the number of positions in which we can substitute characters, which we denote C, for a given string of length n:
plus the base case,
(This means that when we have a string of length 2, there is only 1 position in which the one character in question, that's lexicographically out of order, can possibly occupy.)
Now the number of choices for these letters depends on the length of the string via the binomial term :
The recurrence for suggests a recursive or dynamic approach might work.
Code
Code: https://git.charlesreid1.com/cs/euler/src/master/scratch/Round5_150-160/158
$ cat Problem158.java public class Problem158 { public static void main(String[] args) { System.out.println(solve()); } public static int pow(int b, int p) { int result = b; for(int i=2; i<=p; i++) { result *= b; } return result; } public static String solve() { int m = 26; BinomialDP b = new BinomialDP(m); long p = 0; long priorconfigs = 1; for(int n=3; n<=26; n++) { long binom = (long)( b.binomial(26,n) ); long configs = (long)(priorconfigs + (pow(2,n-1)-1)); long pnew = binom*configs; //System.out.printf("%12d\t%12d\t%12d\t%12d\n",n,binom,configs,pnew); p = Math.max(p,pnew); priorconfigs = configs; } return Long.toString(p); } }
Flags
Project Euler project euler notes
Round 1: Problems 1-20 Problem 1 · Problem 2 · Problem 3 · Problem 4 · Problem 5 · Problem 6 · Problem 7 · Problem 8 · Problem 9 · Problem 10 Problem 11 · Problem 12 · Problem 13 · Problem 14 · Problem 15 · Problem 16 · Problem 17 · Problem 18 · Problem 19 · Problem 20 Round 2: Problems 50-70 Problem 51 · Problem 52 · Problem 53 · Problem 54 · Problem 55 · Problem 56 · Problem 57 · Problem 58 · ... · Problem 61 · Problem 62 · Problem 63 · Problem 64 · Problem 65 · Problem 66 · Problem 67 Round 3: Problems 100-110 Problem 100 · Problem 101 · Problem 102 Round 4: Problems 500-510 Problem 500 · Problem 501 * · Problem 502 * Round 5: Problems 150-160 Round 6: Problems 250-260 Round 7: Problems 170-180
|
Combinatorics
Combinatorial Structures - Order Does Not Matter Ordinary generating functions
Labelled Structures - Order Matters Enumerating Permutations: String Permutations Generating Permutations: Cool · Algorithm M (add-one) · Algorithm G (Gray binary code)
Combinatorics Problems Longest Increasing Subsequence · Maximum Value Contiguous Subsequence · Racing Gems Cards (poker hands with a deck of 52 playing cards)
|