import java.io.*; import java.util.*; import java.awt.geom.*; /** * Solution to Shuffle * * Consider a deck of n cards, ordered 1, 2, 3, ..., n. * That's a single sequence, 1..n. * * Split the deck at some point k, 1<=k=x. * * Counting the sequences can be done in O(n). Here's how: * Every time we see a card, we should, some time later, expect to see the next card in order. * If a card is unexpected, then it must be the start of a new sequence. * * Maintain a boolean array called "expected[]". Start with it all false. * Go through the deck, in order. For every card, if expected[card] is false, * increment the number of sequences. * Then, in any case, set expected[card+1] to true. * * @author vanb */ public class shuffles_vanb { public Scanner sc; public PrintStream ps; /** * Driver. * @throws Exception */ public void doit() throws Exception { sc = new Scanner( System.in ); ps = System.out; int n = sc.nextInt(); boolean expected[] = new boolean[n+2]; Arrays.fill( expected, false ); // Count the number of sequences int nsequences = 0; for( int i=0; i= nsequences // That's our answer. Since n<=1,000,000 then nshuffles<=20 int nshuffles = 0; while( (1<