Monday, January 19, 2015

Topcoder SRM 640 div2 500problem

It took about 1hour to solve this problem

In contest I failed to solve this one.

After days later I retried to solve this one
Finally I was able to solve this one.

My code is below

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package srm640;

import java.util.Arrays;

public class NumberGameAgain {

 public static void main(String[] args) {
  NumberGameAgain num = new NumberGameAgain();
  
 // int k=40;
//  long table[] = {2l,4l,8l,16l,32141531l,2324577l,1099511627775l,2222222222l,33333333333l,4444444444l,2135l}; 

  int k=3;
  long table[] = {2,4,6}; 
//  int k=5;
//  long table[] = {2,3}; 
  
  num.solve(k, table);
  
 }
 
 public long solve(int k, long[]table){
  
  // 1차 필터링
  Arrays.sort(table);
  for(int i=table.length -1; i>0; i--){
   long val = table[i];
   hello:
   while((val = val /2) > 1){
    for(int p = i-1; p>= 0; p--){
     if(table[p] == val){
      
      table[i] = -1;
      break hello;
     }
    }
   }
  }
  
  
  long total = (long) Math.pow(2, k) -2;
  
  for(int i=0; i<table.length; i++){
    long val = table[i];
    for(int j=1; j<k+1; j++){
     
     if(table[i] == -1) continue;
     //System.out.println("j : " + j + "   Math.pow(2, j) : " + Math.pow(2, j));
     
     if(Math.pow(2, j) > table[i]){
      
      //System.out.println("minus : " +(Math.pow(2, k-(j-1)) -1));
      total -= Math.pow(2, k-(j-1)) -1;
      break;
     }
    }
  }
  
  
  //System.out.println(total);
  

//  System.out.println(Arrays.toString(table));
  return total;
 }

}

No comments:

Post a Comment