Guest User

SummingPieces

a guest
Sep 28th, 2016
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.38 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. public class SummingPieces
  4. {
  5.  
  6.  
  7.     /************************ SOLUTION STARTS HERE ***********************/
  8.  
  9.     static final long mod = (long)1e9 + 7;
  10.     static final int modInt = (int)1e9 + 7;
  11.     static long sub(long a, long b) {return ((a % mod) - (b % mod) + mod) % mod;}
  12.     static long mul(long a, long b) {return ((a % mod) * (b % mod)) % mod;}
  13.     static long add(long a, long b) {return ((a % mod) + (b % mod)) % mod;}
  14.  
  15.     static long arr[];
  16.     static long total = 0;
  17.     static int cnt = 0;
  18.     static int pow2[];
  19.     static {  // Calculate the powers of 2
  20.         int max = (int)(1e6);
  21.         pow2 = new int[max + 1];
  22.         pow2[0] = 1;
  23.         for(int i=1;i <= max;i++)
  24.             pow2[i] = (pow2[i - 1] << 1) % modInt;
  25.     }
  26.     static HashMap<Integer,HashMap<String , Integer>> freq;
  27.     static HashMap<Integer ,HashMap<Integer,Integer>> game;
  28.     static HashMap<Integer,HashMap<Sub,Integer>> subFreq;
  29.     static class Sub {   // Wrapper class for holding the end points of a  sub array
  30.         int s , e , len;
  31.         Sub(int s , int e) {
  32.             this.s = s;
  33.             this.e = e;
  34.             this.len = e - s + 1;
  35.         }
  36.         @Override
  37.         public boolean equals(Object obj) {
  38.             Sub o = (Sub) obj;
  39.             return this.s == o.s && this.e == o.e;
  40.         }
  41.         @Override
  42.         public int hashCode() {
  43.             return Objects.hash(s,e);
  44.         }
  45.         @Override
  46.         public String toString() {
  47.             return "[" + s + ", " + e +"]";
  48.         }
  49.     }
  50.     static void rec(int idx , long sum , HashSet<Sub> set) {
  51.         if(idx >= arr.length) {
  52.             total = add(total, sum);
  53.             for(Sub s : set) {
  54.                 HashMap<Sub , Integer> hm = subFreq.getOrDefault(s.len, new HashMap<>());
  55.                 hm.put(s, hm.getOrDefault(s, 0) + 1);
  56.                 subFreq.put(s.len, hm);
  57.             }
  58.         }
  59.         else {
  60.             long curr = 0;
  61.             for(int i=idx;i<arr.length;i++) {
  62.                 curr = add(curr, arr[i]);
  63.                 // System.out.println("start " + idx + " end " + i);
  64.                 // String key = "start " + idx + " end " + i;
  65.                 Sub key = new Sub(idx, i);
  66.                 set.add(key);
  67.                 rec(i + 1, add(sum, mul(curr, i - idx + 1)) , set);
  68.                 set.remove(key);
  69.             }
  70.         }
  71.     }
  72.     static void research(int N) {
  73.         arr = new long[N];
  74.         subFreq  = new HashMap<>();
  75.         Arrays.fill(arr, 1);
  76.         cnt = 0;
  77.         rec(0, 0 , new HashSet<>());
  78.         System.out.println("Array used : " + Arrays.toString(arr));
  79.         for(Map.Entry<Integer , HashMap<Sub , Integer>> e : subFreq.entrySet())
  80.             System.out.println(e.getKey() + " ==> " + e.getValue());
  81.  
  82.         System.out.println("SUM " + total);
  83.  
  84.     }
  85.     private static void solve(FastScanner s1, PrintWriter out){
  86.         int N = s1.nextInt();
  87.         // research(N);
  88.         arr = s1.nextLongArray(N);
  89.  
  90.         long prefix[] = new long[N + 1];
  91.         for(int i=1;i<=N;i++)
  92.             prefix[i] = prefix[i - 1] + arr[i - 1];
  93.         long sum = mul(prefix[N], N);  // Last sum (whole subarray)
  94.         if(N >= 2) {
  95.             sum = add(sum, mul(N - 1, add(prefix[N - 1] - prefix[0], prefix[N] - prefix[1])));
  96.         }
  97.         if(N >= 3) {
  98.             long DP[] = new long[((N-2)/2) + 1]; // One based index
  99.             int DP_len = DP.length - 1;
  100.             long tempSum = 0;
  101.             for(int i=1;i<=(N-2);i++) {
  102.                
  103.                 sum = add(sum, mul(add(prefix[i] - prefix[0], prefix[N] - prefix[N - i]), mul(i, pow2[N - i - 1])));
  104.                 if((N - 2) % 2 == 1 && i == (((N - 2) / 2) + 1))
  105.                     tempSum = add(tempSum, sub(prefix[i + 1], prefix[i]));
  106.                 else {
  107.                     if (i <= (((N - 2) / 2))) {
  108.                         tempSum = add(tempSum, sub(prefix[N - i], prefix[i]));
  109.                         DP[i] = tempSum;
  110.                     }
  111.                     else {
  112.                         if((N - 2) % 2 == 0)
  113.                             tempSum = DP[DP_len - (i - DP_len) + 1];
  114.                         else
  115.                             tempSum = DP[DP_len - (i - DP_len - 2)];
  116.                     }
  117.                 }
  118.                 sum = add(sum, mul(tempSum, mul(i, pow2[N - i - 2])));
  119.             }
  120.         }
  121.  
  122.         out.println(sum);
  123.     }
  124.  
  125.  
  126.  
  127.     /************************ SOLUTION ENDS HERE ************************/
  128.  
  129.  
  130.  
  131.  
  132.  
  133.     /************************ TEMPLATE STARTS HERE *********************/
  134.  
  135.     public static void main(String []args) throws IOException {
  136.         FastScanner in  = new FastScanner(System.in);
  137.         PrintWriter out =
  138.                 new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), false);
  139.         solve(in, out);
  140.         in.close();
  141.         out.close();
  142.     }    
  143.  
  144.     static class FastScanner{
  145.         BufferedReader reader;
  146.         StringTokenizer st;
  147.         FastScanner(InputStream stream){reader=new BufferedReader(new InputStreamReader(stream));st=null;} 
  148.         String next()
  149.         {while(st == null || !st.hasMoreTokens()){try{String line = reader.readLine();if(line == null){return null;}           
  150.         st = new StringTokenizer(line);}catch (Exception e){throw new RuntimeException();}}return st.nextToken();}
  151.         String nextLine()  {String s=null;try{s=reader.readLine();}catch(IOException e){e.printStackTrace();}return s;}            
  152.         int    nextInt()   {return Integer.parseInt(next());}
  153.         long   nextLong()  {return Long.parseLong(next());}    
  154.         double nextDouble(){return Double.parseDouble(next());}
  155.         char   nextChar()  {return next().charAt(0);}
  156.         int[]  nextIntArray(int n)         {int[] arr= new int[n];   int i=0;while(i<n){arr[i++]=nextInt();}  return arr;}
  157.         long[] nextLongArray(int n)        {long[]arr= new long[n];  int i=0;while(i<n){arr[i++]=nextLong();} return arr;} 
  158.         int[]  nextIntArrayOneBased(int n) {int[] arr= new int[n+1]; int i=1;while(i<=n){arr[i++]=nextInt();} return arr;}         
  159.         long[] nextLongArrayOneBased(int n){long[]arr= new long[n+1];int i=1;while(i<=n){arr[i++]=nextLong();}return arr;}         
  160.         void   close(){try{reader.close();}catch(IOException e){e.printStackTrace();}}             
  161.     }
  162.  
  163.     /************************ TEMPLATE ENDS HERE ************************/
  164. }
Add Comment
Please, Sign In to add comment