Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions LiiNi-coder/202507/11 BOJ 1의 개수 세기.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# 코드
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class Main {
private static BufferedReader br;

public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
String[] temp1 = br.readLine().split(" ");
BigInteger l = new BigInteger(temp1[0]);
BigInteger r = new BigInteger(temp1[1]);

BigInteger result = BigInteger.ZERO;

for (int exponent = 0; exponent <= 63; exponent++) {
BigInteger blockSize = BigInteger.ONE.shiftLeft(exponent);
if (blockSize.compareTo(r) > 0) break;

BigInteger lBlockIdx = l.divide(blockSize);
BigInteger rBlockIdx = r.divide(blockSize);

if (lBlockIdx.equals(rBlockIdx)) {
if (getBitAtBlock(lBlockIdx)) {
result = result.add(r.subtract(l).add(BigInteger.ONE));
}
} else {
if (getBitAtBlock(lBlockIdx)) {
BigInteger lEnd = blockSize.multiply(lBlockIdx.add(BigInteger.ONE));
result = result.add(lEnd.subtract(l));
}
if (getBitAtBlock(rBlockIdx)) {
BigInteger rStart = blockSize.multiply(rBlockIdx);
result = result.add(r.subtract(rStart).add(BigInteger.ONE));
}

// C
BigInteger offsetBlock = rBlockIdx.subtract(lBlockIdx).subtract(BigInteger.ONE);
BigInteger half = offsetBlock.divide(BigInteger.TWO);
if (offsetBlock.mod(BigInteger.TWO).equals(BigInteger.ZERO)) {
result = result.add(blockSize.multiply(half));
} else {
if (getBitAtBlock(rBlockIdx)) {
result = result.add(blockSize.multiply(half));
} else {
result = result.add(blockSize.multiply(half).add(blockSize));
}
}
}
}

System.out.println(result);
br.close();
}

private static boolean getBitAtBlock(BigInteger idx) {
return idx.mod(BigInteger.TWO).equals(BigInteger.ONE);
}
}

```

#결과
맞았습니다!! 14484kB 104ms