Guest User

Untitled

a guest
Jan 16th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. import static java.lang.Integer.max;
  6.  
  7. /**
  8.  * Created by bugkiller on 12/01/18.
  9.  */
  10.  
  11. public class PayuKilljeeAndSubsets {
  12.  
  13.     static int[][] dp = new int[1001][1025];
  14.     static int a[] = new int[10000];
  15.     static final int M = 1000000007;
  16.  
  17.     public static void main(String[] args) throws IOException {
  18.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  19.         int n;
  20.         String s[];
  21.         n = Integer.parseInt(br.readLine());
  22.         s = br.readLine().split("\\s");
  23.         for (int i = 0; i < n; i++) {
  24.             a[i] = Integer.parseInt(s[i]);
  25.         }
  26.         System.out.println(solve(a, n));
  27.     }
  28.  
  29.     private static long solve(int[] a, int n) {
  30.         int l = 0;
  31.         long ans = 0;
  32.         for (int i = 0; i <n; i++) {
  33.             l = max(a[i], l);
  34.         }
  35.         fillDp(n, l);
  36.         /*for (int[] temp :dp) {
  37.             System.out.println(Arrays.toString(temp));
  38.         }*/
  39.         for (int i = 0; i <= l; i++) {
  40.             ans += (dp[n][i] * exp(31, i))%M;
  41.         }
  42.         return ans;
  43.     }
  44.     static long exp(int a, int n) {
  45.         long ans;
  46.         if (n > 0) {
  47.             long temp = exp(a, n / 2) % M;
  48.             ans = (temp * temp) % M;
  49.             if (n % 2 == 1) {
  50.                 return (a * ans) % M;
  51.             }
  52.             return ans;
  53.         }
  54.         return 1;
  55.     }
  56.  
  57.     private static void fillDp(int n, int l) {
  58.         dp[0][0] = 1;
  59.         for (int i = 1; i <= n; i++) {
  60.             for (int j = 0; j <=l; j++) {
  61.                 if ((j ^ a[i - 1]) == 0) {
  62.                     if (dp[i - 1][j ^ a[i - 1]] == 1) {
  63.                         dp[i][j] = max(dp[i - 1][j], 1);
  64.                     }
  65.                     else {
  66.                         dp[i][j] = max(dp[i - 1][j], 1 + dp[i - 1][j ^ a[i - 1]]);
  67.                     }
  68.                 }
  69.                 else {
  70.                     int temp = dp[i - 1][j ^ a[i - 1]];
  71.                     dp[i][j] = max(dp[i - 1][j], 1 + (temp == 0 ? -1 : temp));
  72.                 }
  73.  
  74.             }
  75.         }
  76.     }
  77. }
Add Comment
Please, Sign In to add comment