답안 #337234

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
337234 2020-12-19T02:03:34 Z KWang31 Snake Escaping (JOI18_snake_escaping) Java 11
100 / 100
1752 ms 65536 KB
import java.io.*; import java.util.*;
public class snake_escaping{
  static class FastReader 
    { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader() 
        { 
            br = new BufferedReader(new
                     InputStreamReader(System.in)); 
        } 
  
        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 
  
        
    } 
  
    public static class Pair implements Comparable<Pair>{
        int vtx; int val;
        public Pair(int a, int b){
            this.vtx=a; this.val=b;
        }
        public int compareTo(Pair other){
            if(this.val<other.val)return -1;
            if(this.val>other.val)return 1;
            if(this.vtx<other.vtx)return -1;
            return 1;
        }
    }
    static int MOD=998244353;
    
    public static void main(String[] args){
        FastReader br=new FastReader();
        int L=br.nextInt(); int Q=br.nextInt();
        final int[] S=new int[1<<L]; final int[] sup=new int[1<<L]; final int[] sub=new int[1<<L];
        final int[] btcnt=new int[1<<L];
        String s=br.next();
        for(int i=0; i<(1<<L); i++){
            S[i]=(int) s.charAt(i)-'0'; sup[i]=sub[i]=S[i];
            btcnt[i]=Integer.bitCount(i);
        }
        //SOS DP!!
        for(int b=0; b<L; b++){
            for(int m=0;m<(1<<L); m++){
                if(((m>>>b)&1) ==0){
                    sup[m]+=sup[m^(1<<b)];
                }else{
                    sub[m]+=sub[m^(1<<b)];
                }
            }
        }
        //System.out.println(Arrays.toString(sub));
        //System.out.println(Arrays.toString(sup));
        StringBuilder sb=new StringBuilder();
        //sb can have a lot of memory
        int A; int B; int C;
        int ca; int cb; int cc; long ans;
        for(int i=0;i<Q;i++){
            s=br.next(); A=0; B=0; C=0;
            ca=0; cb=0; cc=0; ans=0;
            for(int j=0;j<L;j++){
                if(s.charAt(j)=='0'){A|=(1<<(L-j-1)); ca++;}
                else if(s.charAt(j)=='1') {B|=(1<<(L-j-1)); cb++;}
                else {C|=(1<<(L-j-1)); cc++;}
            }
            //System.out.println(ca+" "+cb+" "+cc);
            if(ca<=cb && ca<=cc){
                for(int m=A; m!=0; m=(m-1)&A){
                    ans+=(1-2*((btcnt[m])&1))*sup[B|m];
                }
                ans+=sup[B];
            }else if(cb<=ca && cb<=cc){
                for(int m=B; m!=0; m=(m-1)&B){
                    
                    ans+=(1-2*((btcnt[m])&1))*sub[C|(B^m)];
                    
                }
                ans+=sub[C|B];
                
            }else{
                for(int m=C; m!=0; m=(m-1)&C){
                    
                    ans+=S[m|B];
                }
                //System.out.println(A+" "+B);
                ans+=S[B];
            }
            sb.append(ans).append("\n");
            if((i&((1<<17)-1))==0){
                System.out.println(sb.toString());
                sb=new StringBuilder();
            }
        }
        System.out.println(sb.toString());
    }
    
    
    
}
//Debugging:
//Are you sure your algorithm is correct?
//Bounds: long
//Special cases: n=0,1?
//Make sure you remove your debugging code before you submit!
# 결과 실행 시간 메모리 Grader output
1 Correct 119 ms 10388 KB Output is correct
2 Correct 124 ms 10504 KB Output is correct
3 Correct 131 ms 10516 KB Output is correct
4 Correct 125 ms 10444 KB Output is correct
5 Correct 119 ms 10388 KB Output is correct
6 Correct 128 ms 10356 KB Output is correct
7 Correct 128 ms 10244 KB Output is correct
8 Correct 123 ms 10352 KB Output is correct
9 Correct 119 ms 10388 KB Output is correct
10 Correct 125 ms 10260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 119 ms 10388 KB Output is correct
2 Correct 124 ms 10504 KB Output is correct
3 Correct 131 ms 10516 KB Output is correct
4 Correct 125 ms 10444 KB Output is correct
5 Correct 119 ms 10388 KB Output is correct
6 Correct 128 ms 10356 KB Output is correct
7 Correct 128 ms 10244 KB Output is correct
8 Correct 123 ms 10352 KB Output is correct
9 Correct 119 ms 10388 KB Output is correct
10 Correct 125 ms 10260 KB Output is correct
11 Correct 619 ms 24716 KB Output is correct
12 Correct 607 ms 24408 KB Output is correct
13 Correct 627 ms 23600 KB Output is correct
14 Correct 621 ms 23940 KB Output is correct
15 Correct 618 ms 24760 KB Output is correct
16 Correct 632 ms 23968 KB Output is correct
17 Correct 647 ms 23956 KB Output is correct
18 Correct 540 ms 25724 KB Output is correct
19 Correct 561 ms 20832 KB Output is correct
20 Correct 606 ms 24412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 119 ms 10388 KB Output is correct
2 Correct 124 ms 10504 KB Output is correct
3 Correct 131 ms 10516 KB Output is correct
4 Correct 125 ms 10444 KB Output is correct
5 Correct 119 ms 10388 KB Output is correct
6 Correct 128 ms 10356 KB Output is correct
7 Correct 128 ms 10244 KB Output is correct
8 Correct 123 ms 10352 KB Output is correct
9 Correct 119 ms 10388 KB Output is correct
10 Correct 125 ms 10260 KB Output is correct
11 Correct 619 ms 24716 KB Output is correct
12 Correct 607 ms 24408 KB Output is correct
13 Correct 627 ms 23600 KB Output is correct
14 Correct 621 ms 23940 KB Output is correct
15 Correct 618 ms 24760 KB Output is correct
16 Correct 632 ms 23968 KB Output is correct
17 Correct 647 ms 23956 KB Output is correct
18 Correct 540 ms 25724 KB Output is correct
19 Correct 561 ms 20832 KB Output is correct
20 Correct 606 ms 24412 KB Output is correct
21 Correct 669 ms 25164 KB Output is correct
22 Correct 709 ms 25272 KB Output is correct
23 Correct 748 ms 24024 KB Output is correct
24 Correct 717 ms 24140 KB Output is correct
25 Correct 711 ms 26192 KB Output is correct
26 Correct 743 ms 24624 KB Output is correct
27 Correct 756 ms 24636 KB Output is correct
28 Correct 636 ms 27480 KB Output is correct
29 Correct 629 ms 21308 KB Output is correct
30 Correct 695 ms 25216 KB Output is correct
31 Correct 738 ms 25288 KB Output is correct
32 Correct 728 ms 25268 KB Output is correct
33 Correct 693 ms 24064 KB Output is correct
34 Correct 759 ms 24096 KB Output is correct
35 Correct 754 ms 24784 KB Output is correct
36 Correct 556 ms 21324 KB Output is correct
37 Correct 672 ms 25260 KB Output is correct
38 Correct 704 ms 21428 KB Output is correct
39 Correct 717 ms 24244 KB Output is correct
40 Correct 704 ms 24128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 119 ms 10388 KB Output is correct
2 Correct 124 ms 10504 KB Output is correct
3 Correct 131 ms 10516 KB Output is correct
4 Correct 125 ms 10444 KB Output is correct
5 Correct 119 ms 10388 KB Output is correct
6 Correct 128 ms 10356 KB Output is correct
7 Correct 128 ms 10244 KB Output is correct
8 Correct 123 ms 10352 KB Output is correct
9 Correct 119 ms 10388 KB Output is correct
10 Correct 125 ms 10260 KB Output is correct
11 Correct 493 ms 40892 KB Output is correct
12 Correct 488 ms 41260 KB Output is correct
13 Correct 537 ms 41156 KB Output is correct
14 Correct 533 ms 40656 KB Output is correct
15 Correct 499 ms 41884 KB Output is correct
16 Correct 521 ms 41016 KB Output is correct
17 Correct 519 ms 40952 KB Output is correct
18 Correct 488 ms 42136 KB Output is correct
19 Correct 473 ms 40492 KB Output is correct
20 Correct 480 ms 40892 KB Output is correct
21 Correct 504 ms 41148 KB Output is correct
22 Correct 543 ms 40388 KB Output is correct
23 Correct 491 ms 40632 KB Output is correct
24 Correct 535 ms 40684 KB Output is correct
25 Correct 523 ms 40844 KB Output is correct
26 Correct 465 ms 40208 KB Output is correct
27 Correct 493 ms 40628 KB Output is correct
28 Correct 476 ms 40088 KB Output is correct
29 Correct 538 ms 40652 KB Output is correct
30 Correct 531 ms 40260 KB Output is correct
31 Correct 499 ms 40292 KB Output is correct
32 Correct 535 ms 40504 KB Output is correct
33 Correct 525 ms 40408 KB Output is correct
34 Correct 474 ms 40180 KB Output is correct
35 Correct 512 ms 40652 KB Output is correct
36 Correct 512 ms 40640 KB Output is correct
37 Correct 510 ms 40440 KB Output is correct
38 Correct 508 ms 40364 KB Output is correct
39 Correct 506 ms 40368 KB Output is correct
40 Correct 520 ms 40352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 119 ms 10388 KB Output is correct
2 Correct 124 ms 10504 KB Output is correct
3 Correct 131 ms 10516 KB Output is correct
4 Correct 125 ms 10444 KB Output is correct
5 Correct 119 ms 10388 KB Output is correct
6 Correct 128 ms 10356 KB Output is correct
7 Correct 128 ms 10244 KB Output is correct
8 Correct 123 ms 10352 KB Output is correct
9 Correct 119 ms 10388 KB Output is correct
10 Correct 125 ms 10260 KB Output is correct
11 Correct 619 ms 24716 KB Output is correct
12 Correct 607 ms 24408 KB Output is correct
13 Correct 627 ms 23600 KB Output is correct
14 Correct 621 ms 23940 KB Output is correct
15 Correct 618 ms 24760 KB Output is correct
16 Correct 632 ms 23968 KB Output is correct
17 Correct 647 ms 23956 KB Output is correct
18 Correct 540 ms 25724 KB Output is correct
19 Correct 561 ms 20832 KB Output is correct
20 Correct 606 ms 24412 KB Output is correct
21 Correct 669 ms 25164 KB Output is correct
22 Correct 709 ms 25272 KB Output is correct
23 Correct 748 ms 24024 KB Output is correct
24 Correct 717 ms 24140 KB Output is correct
25 Correct 711 ms 26192 KB Output is correct
26 Correct 743 ms 24624 KB Output is correct
27 Correct 756 ms 24636 KB Output is correct
28 Correct 636 ms 27480 KB Output is correct
29 Correct 629 ms 21308 KB Output is correct
30 Correct 695 ms 25216 KB Output is correct
31 Correct 738 ms 25288 KB Output is correct
32 Correct 728 ms 25268 KB Output is correct
33 Correct 693 ms 24064 KB Output is correct
34 Correct 759 ms 24096 KB Output is correct
35 Correct 754 ms 24784 KB Output is correct
36 Correct 556 ms 21324 KB Output is correct
37 Correct 672 ms 25260 KB Output is correct
38 Correct 704 ms 21428 KB Output is correct
39 Correct 717 ms 24244 KB Output is correct
40 Correct 704 ms 24128 KB Output is correct
41 Correct 493 ms 40892 KB Output is correct
42 Correct 488 ms 41260 KB Output is correct
43 Correct 537 ms 41156 KB Output is correct
44 Correct 533 ms 40656 KB Output is correct
45 Correct 499 ms 41884 KB Output is correct
46 Correct 521 ms 41016 KB Output is correct
47 Correct 519 ms 40952 KB Output is correct
48 Correct 488 ms 42136 KB Output is correct
49 Correct 473 ms 40492 KB Output is correct
50 Correct 480 ms 40892 KB Output is correct
51 Correct 504 ms 41148 KB Output is correct
52 Correct 543 ms 40388 KB Output is correct
53 Correct 491 ms 40632 KB Output is correct
54 Correct 535 ms 40684 KB Output is correct
55 Correct 523 ms 40844 KB Output is correct
56 Correct 465 ms 40208 KB Output is correct
57 Correct 493 ms 40628 KB Output is correct
58 Correct 476 ms 40088 KB Output is correct
59 Correct 538 ms 40652 KB Output is correct
60 Correct 531 ms 40260 KB Output is correct
61 Correct 499 ms 40292 KB Output is correct
62 Correct 535 ms 40504 KB Output is correct
63 Correct 525 ms 40408 KB Output is correct
64 Correct 474 ms 40180 KB Output is correct
65 Correct 512 ms 40652 KB Output is correct
66 Correct 512 ms 40640 KB Output is correct
67 Correct 510 ms 40440 KB Output is correct
68 Correct 508 ms 40364 KB Output is correct
69 Correct 506 ms 40368 KB Output is correct
70 Correct 520 ms 40352 KB Output is correct
71 Correct 998 ms 65536 KB Output is correct
72 Correct 1027 ms 65536 KB Output is correct
73 Correct 1280 ms 62396 KB Output is correct
74 Correct 1537 ms 63908 KB Output is correct
75 Correct 1044 ms 65536 KB Output is correct
76 Correct 1571 ms 65536 KB Output is correct
77 Correct 1564 ms 65536 KB Output is correct
78 Correct 789 ms 65536 KB Output is correct
79 Correct 943 ms 65536 KB Output is correct
80 Correct 1043 ms 65536 KB Output is correct
81 Correct 1317 ms 65536 KB Output is correct
82 Correct 1660 ms 65536 KB Output is correct
83 Correct 994 ms 65536 KB Output is correct
84 Correct 1752 ms 65536 KB Output is correct
85 Correct 1542 ms 65536 KB Output is correct
86 Correct 720 ms 65536 KB Output is correct
87 Correct 963 ms 65536 KB Output is correct
88 Correct 967 ms 65536 KB Output is correct
89 Correct 1242 ms 65536 KB Output is correct
90 Correct 1314 ms 65536 KB Output is correct
91 Correct 1026 ms 65536 KB Output is correct
92 Correct 1639 ms 65536 KB Output is correct
93 Correct 1547 ms 65536 KB Output is correct
94 Correct 714 ms 65536 KB Output is correct
95 Correct 1375 ms 65536 KB Output is correct
96 Correct 1361 ms 65536 KB Output is correct
97 Correct 1363 ms 65536 KB Output is correct
98 Correct 1357 ms 65536 KB Output is correct
99 Correct 1370 ms 65536 KB Output is correct
100 Correct 1350 ms 65536 KB Output is correct