답안 #337245

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
337245 2020-12-19T03:29:56 Z KWang31 Snake Escaping (JOI18_snake_escaping) Java 11
100 / 100
1744 ms 63128 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*((cb-btcnt[m])&1))*sub[C|m];
                    
                }
                ans+=(1-2*(cb&1))*sub[C];
                
            }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 123 ms 10260 KB Output is correct
2 Correct 120 ms 10272 KB Output is correct
3 Correct 121 ms 10360 KB Output is correct
4 Correct 124 ms 10516 KB Output is correct
5 Correct 123 ms 10388 KB Output is correct
6 Correct 122 ms 10500 KB Output is correct
7 Correct 124 ms 10388 KB Output is correct
8 Correct 119 ms 10388 KB Output is correct
9 Correct 119 ms 10480 KB Output is correct
10 Correct 119 ms 10280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 123 ms 10260 KB Output is correct
2 Correct 120 ms 10272 KB Output is correct
3 Correct 121 ms 10360 KB Output is correct
4 Correct 124 ms 10516 KB Output is correct
5 Correct 123 ms 10388 KB Output is correct
6 Correct 122 ms 10500 KB Output is correct
7 Correct 124 ms 10388 KB Output is correct
8 Correct 119 ms 10388 KB Output is correct
9 Correct 119 ms 10480 KB Output is correct
10 Correct 119 ms 10280 KB Output is correct
11 Correct 618 ms 23824 KB Output is correct
12 Correct 612 ms 23340 KB Output is correct
13 Correct 629 ms 22796 KB Output is correct
14 Correct 626 ms 22776 KB Output is correct
15 Correct 615 ms 23668 KB Output is correct
16 Correct 638 ms 22836 KB Output is correct
17 Correct 649 ms 22944 KB Output is correct
18 Correct 547 ms 24960 KB Output is correct
19 Correct 548 ms 19804 KB Output is correct
20 Correct 622 ms 23372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 123 ms 10260 KB Output is correct
2 Correct 120 ms 10272 KB Output is correct
3 Correct 121 ms 10360 KB Output is correct
4 Correct 124 ms 10516 KB Output is correct
5 Correct 123 ms 10388 KB Output is correct
6 Correct 122 ms 10500 KB Output is correct
7 Correct 124 ms 10388 KB Output is correct
8 Correct 119 ms 10388 KB Output is correct
9 Correct 119 ms 10480 KB Output is correct
10 Correct 119 ms 10280 KB Output is correct
11 Correct 618 ms 23824 KB Output is correct
12 Correct 612 ms 23340 KB Output is correct
13 Correct 629 ms 22796 KB Output is correct
14 Correct 626 ms 22776 KB Output is correct
15 Correct 615 ms 23668 KB Output is correct
16 Correct 638 ms 22836 KB Output is correct
17 Correct 649 ms 22944 KB Output is correct
18 Correct 547 ms 24960 KB Output is correct
19 Correct 548 ms 19804 KB Output is correct
20 Correct 622 ms 23372 KB Output is correct
21 Correct 680 ms 23808 KB Output is correct
22 Correct 714 ms 24176 KB Output is correct
23 Correct 728 ms 23292 KB Output is correct
24 Correct 716 ms 23164 KB Output is correct
25 Correct 718 ms 25000 KB Output is correct
26 Correct 743 ms 23612 KB Output is correct
27 Correct 758 ms 23724 KB Output is correct
28 Correct 605 ms 26312 KB Output is correct
29 Correct 624 ms 20276 KB Output is correct
30 Correct 685 ms 24092 KB Output is correct
31 Correct 728 ms 24028 KB Output is correct
32 Correct 708 ms 24316 KB Output is correct
33 Correct 680 ms 23116 KB Output is correct
34 Correct 753 ms 23244 KB Output is correct
35 Correct 747 ms 23604 KB Output is correct
36 Correct 548 ms 20500 KB Output is correct
37 Correct 673 ms 23904 KB Output is correct
38 Correct 707 ms 20244 KB Output is correct
39 Correct 725 ms 23216 KB Output is correct
40 Correct 708 ms 23044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 123 ms 10260 KB Output is correct
2 Correct 120 ms 10272 KB Output is correct
3 Correct 121 ms 10360 KB Output is correct
4 Correct 124 ms 10516 KB Output is correct
5 Correct 123 ms 10388 KB Output is correct
6 Correct 122 ms 10500 KB Output is correct
7 Correct 124 ms 10388 KB Output is correct
8 Correct 119 ms 10388 KB Output is correct
9 Correct 119 ms 10480 KB Output is correct
10 Correct 119 ms 10280 KB Output is correct
11 Correct 482 ms 40108 KB Output is correct
12 Correct 482 ms 40112 KB Output is correct
13 Correct 528 ms 40144 KB Output is correct
14 Correct 533 ms 39648 KB Output is correct
15 Correct 492 ms 40652 KB Output is correct
16 Correct 515 ms 40200 KB Output is correct
17 Correct 516 ms 40308 KB Output is correct
18 Correct 493 ms 41332 KB Output is correct
19 Correct 474 ms 39540 KB Output is correct
20 Correct 478 ms 40124 KB Output is correct
21 Correct 503 ms 39992 KB Output is correct
22 Correct 528 ms 39544 KB Output is correct
23 Correct 481 ms 39920 KB Output is correct
24 Correct 549 ms 40084 KB Output is correct
25 Correct 523 ms 40308 KB Output is correct
26 Correct 464 ms 39604 KB Output is correct
27 Correct 477 ms 39956 KB Output is correct
28 Correct 469 ms 39376 KB Output is correct
29 Correct 524 ms 40228 KB Output is correct
30 Correct 520 ms 39776 KB Output is correct
31 Correct 492 ms 39940 KB Output is correct
32 Correct 527 ms 40392 KB Output is correct
33 Correct 519 ms 40076 KB Output is correct
34 Correct 465 ms 39584 KB Output is correct
35 Correct 516 ms 40292 KB Output is correct
36 Correct 509 ms 40184 KB Output is correct
37 Correct 506 ms 40048 KB Output is correct
38 Correct 518 ms 40120 KB Output is correct
39 Correct 510 ms 40304 KB Output is correct
40 Correct 506 ms 40188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 123 ms 10260 KB Output is correct
2 Correct 120 ms 10272 KB Output is correct
3 Correct 121 ms 10360 KB Output is correct
4 Correct 124 ms 10516 KB Output is correct
5 Correct 123 ms 10388 KB Output is correct
6 Correct 122 ms 10500 KB Output is correct
7 Correct 124 ms 10388 KB Output is correct
8 Correct 119 ms 10388 KB Output is correct
9 Correct 119 ms 10480 KB Output is correct
10 Correct 119 ms 10280 KB Output is correct
11 Correct 618 ms 23824 KB Output is correct
12 Correct 612 ms 23340 KB Output is correct
13 Correct 629 ms 22796 KB Output is correct
14 Correct 626 ms 22776 KB Output is correct
15 Correct 615 ms 23668 KB Output is correct
16 Correct 638 ms 22836 KB Output is correct
17 Correct 649 ms 22944 KB Output is correct
18 Correct 547 ms 24960 KB Output is correct
19 Correct 548 ms 19804 KB Output is correct
20 Correct 622 ms 23372 KB Output is correct
21 Correct 680 ms 23808 KB Output is correct
22 Correct 714 ms 24176 KB Output is correct
23 Correct 728 ms 23292 KB Output is correct
24 Correct 716 ms 23164 KB Output is correct
25 Correct 718 ms 25000 KB Output is correct
26 Correct 743 ms 23612 KB Output is correct
27 Correct 758 ms 23724 KB Output is correct
28 Correct 605 ms 26312 KB Output is correct
29 Correct 624 ms 20276 KB Output is correct
30 Correct 685 ms 24092 KB Output is correct
31 Correct 728 ms 24028 KB Output is correct
32 Correct 708 ms 24316 KB Output is correct
33 Correct 680 ms 23116 KB Output is correct
34 Correct 753 ms 23244 KB Output is correct
35 Correct 747 ms 23604 KB Output is correct
36 Correct 548 ms 20500 KB Output is correct
37 Correct 673 ms 23904 KB Output is correct
38 Correct 707 ms 20244 KB Output is correct
39 Correct 725 ms 23216 KB Output is correct
40 Correct 708 ms 23044 KB Output is correct
41 Correct 482 ms 40108 KB Output is correct
42 Correct 482 ms 40112 KB Output is correct
43 Correct 528 ms 40144 KB Output is correct
44 Correct 533 ms 39648 KB Output is correct
45 Correct 492 ms 40652 KB Output is correct
46 Correct 515 ms 40200 KB Output is correct
47 Correct 516 ms 40308 KB Output is correct
48 Correct 493 ms 41332 KB Output is correct
49 Correct 474 ms 39540 KB Output is correct
50 Correct 478 ms 40124 KB Output is correct
51 Correct 503 ms 39992 KB Output is correct
52 Correct 528 ms 39544 KB Output is correct
53 Correct 481 ms 39920 KB Output is correct
54 Correct 549 ms 40084 KB Output is correct
55 Correct 523 ms 40308 KB Output is correct
56 Correct 464 ms 39604 KB Output is correct
57 Correct 477 ms 39956 KB Output is correct
58 Correct 469 ms 39376 KB Output is correct
59 Correct 524 ms 40228 KB Output is correct
60 Correct 520 ms 39776 KB Output is correct
61 Correct 492 ms 39940 KB Output is correct
62 Correct 527 ms 40392 KB Output is correct
63 Correct 519 ms 40076 KB Output is correct
64 Correct 465 ms 39584 KB Output is correct
65 Correct 516 ms 40292 KB Output is correct
66 Correct 509 ms 40184 KB Output is correct
67 Correct 506 ms 40048 KB Output is correct
68 Correct 518 ms 40120 KB Output is correct
69 Correct 510 ms 40304 KB Output is correct
70 Correct 506 ms 40188 KB Output is correct
71 Correct 966 ms 57168 KB Output is correct
72 Correct 997 ms 56024 KB Output is correct
73 Correct 1262 ms 49964 KB Output is correct
74 Correct 1515 ms 50108 KB Output is correct
75 Correct 1016 ms 59388 KB Output is correct
76 Correct 1527 ms 56208 KB Output is correct
77 Correct 1533 ms 56292 KB Output is correct
78 Correct 780 ms 63128 KB Output is correct
79 Correct 922 ms 51188 KB Output is correct
80 Correct 997 ms 55868 KB Output is correct
81 Correct 1232 ms 57168 KB Output is correct
82 Correct 1529 ms 54400 KB Output is correct
83 Correct 985 ms 53280 KB Output is correct
84 Correct 1744 ms 55880 KB Output is correct
85 Correct 1536 ms 56196 KB Output is correct
86 Correct 738 ms 51232 KB Output is correct
87 Correct 959 ms 57452 KB Output is correct
88 Correct 948 ms 50860 KB Output is correct
89 Correct 1211 ms 55436 KB Output is correct
90 Correct 1224 ms 54628 KB Output is correct
91 Correct 1012 ms 53560 KB Output is correct
92 Correct 1624 ms 56684 KB Output is correct
93 Correct 1539 ms 55992 KB Output is correct
94 Correct 712 ms 51488 KB Output is correct
95 Correct 1353 ms 55960 KB Output is correct
96 Correct 1365 ms 56016 KB Output is correct
97 Correct 1355 ms 55932 KB Output is correct
98 Correct 1349 ms 55796 KB Output is correct
99 Correct 1352 ms 55920 KB Output is correct
100 Correct 1354 ms 56008 KB Output is correct