christmas/christmas_vanb.java

  1 
  2 import java.io.*;
  3 import java.util.*;
  4 import java.awt.geom.*;
  5 
  6 /**
  7  * Solution to The n Days of Christmas
  8  * 
  9  * @author vanb
 10  */
 11 public class christmas_vanb
 12 {
 13     public Scanner sc;
 14     public PrintStream ps;
 15     
 16     
 17     /**
 18      * Driver.
 19      * @throws Exception
 20      */
 21     public void doit() throws Exception
 22     {
 23         sc = new Scanner( new File( "christmas.judge" ) );
 24         ps = new PrintStream( new FileOutputStream( "christmas.solution" ) );
 25 
 26         // Precompute all of the answers
 27         long gifts[] = new long[1000001];
 28         gifts[1] = 1L;
 29         for( int i=2; i<=1000000; i++ )
 30         {
 31             // On day i, you get Sum(1..i) gifts. That's i*(i+1)/2
 32             // Add in the accumulation of all the gifts from the day before.
 33             long ii = i;
 34             gifts[i] = ii*(ii+1)/2L + gifts[i-1]; 
 35         }
 36 
 37         for(;;)
 38         {
 39             int n = sc.nextInt();
 40             if( n==0 ) break;
 41             ps.println( gifts[n] );
 42         }
 43     }
 44     
 45     /**
 46      * @param args
 47      */
 48     public static void main( String[] args ) throws Exception
 49     {
 50         new christmas_vanb().doit();        
 51     }   
 52 }