|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +class Main { |
| 7 | + |
| 8 | + // IO field |
| 9 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 11 | + static StringTokenizer st; |
| 12 | + |
| 13 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 14 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 15 | + static long nextLong() {return Long.parseLong(st.nextToken());} |
| 16 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 17 | + |
| 18 | + // Additional field |
| 19 | + static int N, L, R, O; |
| 20 | + static long[][] S; |
| 21 | + static long[] sz; |
| 22 | + |
| 23 | + public static void main(String[] args) throws Exception { |
| 24 | + |
| 25 | + ready(); |
| 26 | + solve(); |
| 27 | + |
| 28 | + bwEnd(); |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + static void ready() throws Exception{ |
| 33 | + |
| 34 | + O = Integer.parseInt(br.readLine()); |
| 35 | + L = Integer.parseInt(br.readLine()); |
| 36 | + R = Integer.parseInt(br.readLine()); |
| 37 | + N = Integer.parseInt(br.readLine()); |
| 38 | + S = new long[4][N+1]; |
| 39 | + sz = new long[N+1]; |
| 40 | + sz[0] = 1; |
| 41 | + for(int i=1;i<=N;i++) sz[i] = sz[i-1] * 3; |
| 42 | + |
| 43 | + } |
| 44 | + |
| 45 | + static void solve() throws Exception{ |
| 46 | + |
| 47 | + for(int c=1;c<=3;c++) { |
| 48 | + S[1][0] = c == 1 ? 1 : 0; |
| 49 | + S[2][0] = c == 2 ? 1 : 0; |
| 50 | + S[3][0] = c == 3 ? 1 : 0; |
| 51 | + for(int n=1;n<=N ;n++) { |
| 52 | + S[1][n] = S[1][n-1] + S[3][n-1] + S[2][n-1]; |
| 53 | + S[2][n] = S[2][n-1] + S[1][n-1]*2; |
| 54 | + S[3][n] = S[2][n-1]*2 + S[3][n-1]; |
| 55 | + } |
| 56 | + bw.write(dnc(O, N, L, R) + " "); |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + // S[k][dep]에서 s~e의 합 구하기 |
| 63 | + static long dnc(int k, int dep, int s, int e) { |
| 64 | + |
| 65 | + long len = e-s+1; |
| 66 | + if(s>e) return 0; |
| 67 | + if(len == sz[dep]) return S[k][dep]; |
| 68 | + |
| 69 | + long m = sz[dep]/3; |
| 70 | + long temp = 0; |
| 71 | + if(k == 1) { |
| 72 | + |
| 73 | + long r = Math.min(m-1, e); |
| 74 | + temp += dnc(1,dep-1,s,(int)r); |
| 75 | + s -= m; |
| 76 | + e -= m; |
| 77 | + r = Math.min(m-1, e); |
| 78 | + temp += dnc(3,dep-1,Math.max(s, 0),(int)r); |
| 79 | + s -= m; |
| 80 | + e -= m; |
| 81 | + r = Math.min(m-1, e); |
| 82 | + temp += dnc(2,dep-1,Math.max(s, 0),(int)r); |
| 83 | + |
| 84 | + } |
| 85 | + else if(k == 2) { |
| 86 | + |
| 87 | + long r = Math.min(m-1, e); |
| 88 | + temp += dnc(2,dep-1,s,(int)r); |
| 89 | + s -= m; |
| 90 | + e -= m; |
| 91 | + r = Math.min(m-1, e); |
| 92 | + temp += dnc(1,dep-1,Math.max(s, 0),(int)r); |
| 93 | + s -= m; |
| 94 | + e -= m; |
| 95 | + r = Math.min(m-1, e); |
| 96 | + temp += dnc(1,dep-1,Math.max(s, 0),(int)r); |
| 97 | + |
| 98 | + } |
| 99 | + else { |
| 100 | + |
| 101 | + long r = Math.min(m-1, e); |
| 102 | + temp += dnc(2,dep-1,s,(int)r); |
| 103 | + s -= m; |
| 104 | + e -= m; |
| 105 | + r = Math.min(m-1, e); |
| 106 | + temp += dnc(3,dep-1,Math.max(s, 0),(int)r); |
| 107 | + s -= m; |
| 108 | + e -= m; |
| 109 | + r = Math.min(m-1, e); |
| 110 | + temp += dnc(2,dep-1,Math.max(s, 0),(int)r); |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + return temp; |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | +} |
| 123 | + |
| 124 | +``` |
0 commit comments