|
| 1 | +# 코드 |
| 2 | +```java |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.math.BigInteger; |
| 7 | + |
| 8 | +public class Main { |
| 9 | + private static BufferedReader br; |
| 10 | + |
| 11 | + public static void main(String[] args) throws IOException { |
| 12 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 13 | + String[] temp1 = br.readLine().split(" "); |
| 14 | + BigInteger l = new BigInteger(temp1[0]); |
| 15 | + BigInteger r = new BigInteger(temp1[1]); |
| 16 | + |
| 17 | + BigInteger result = BigInteger.ZERO; |
| 18 | + |
| 19 | + for (int exponent = 0; exponent <= 63; exponent++) { |
| 20 | + BigInteger blockSize = BigInteger.ONE.shiftLeft(exponent); |
| 21 | + if (blockSize.compareTo(r) > 0) break; |
| 22 | + |
| 23 | + BigInteger lBlockIdx = l.divide(blockSize); |
| 24 | + BigInteger rBlockIdx = r.divide(blockSize); |
| 25 | + |
| 26 | + if (lBlockIdx.equals(rBlockIdx)) { |
| 27 | + if (getBitAtBlock(lBlockIdx)) { |
| 28 | + result = result.add(r.subtract(l).add(BigInteger.ONE)); |
| 29 | + } |
| 30 | + } else { |
| 31 | + if (getBitAtBlock(lBlockIdx)) { |
| 32 | + BigInteger lEnd = blockSize.multiply(lBlockIdx.add(BigInteger.ONE)); |
| 33 | + result = result.add(lEnd.subtract(l)); |
| 34 | + } |
| 35 | + if (getBitAtBlock(rBlockIdx)) { |
| 36 | + BigInteger rStart = blockSize.multiply(rBlockIdx); |
| 37 | + result = result.add(r.subtract(rStart).add(BigInteger.ONE)); |
| 38 | + } |
| 39 | + |
| 40 | + // C |
| 41 | + BigInteger offsetBlock = rBlockIdx.subtract(lBlockIdx).subtract(BigInteger.ONE); |
| 42 | + BigInteger half = offsetBlock.divide(BigInteger.TWO); |
| 43 | + if (offsetBlock.mod(BigInteger.TWO).equals(BigInteger.ZERO)) { |
| 44 | + result = result.add(blockSize.multiply(half)); |
| 45 | + } else { |
| 46 | + if (getBitAtBlock(rBlockIdx)) { |
| 47 | + result = result.add(blockSize.multiply(half)); |
| 48 | + } else { |
| 49 | + result = result.add(blockSize.multiply(half).add(blockSize)); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + System.out.println(result); |
| 56 | + br.close(); |
| 57 | + } |
| 58 | + |
| 59 | + private static boolean getBitAtBlock(BigInteger idx) { |
| 60 | + return idx.mod(BigInteger.TWO).equals(BigInteger.ONE); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +``` |
| 65 | + |
| 66 | +#결과 |
| 67 | +맞았습니다!! 14484kB 104ms |
0 commit comments