728x90
๐๋ฌธ์ ๋งํฌ
๐ป์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static long[] dp = new long[55];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
long A = Long.parseLong(st.nextToken());
long B = Long.parseLong(st.nextToken());
//dp[n] = dp[n-1] ร 2 + 2โฟ์ ์ด์ฉํ์ฌ ๋์ ํฉ ์ ์ฅ
dp[0] = 1;
for (int i = 1; i < 55; i++) {
dp[i] = (dp[i - 1] << 1) + (1L << i);
}
System.out.println(getCount(B) - getCount(A - 1));
}
//1~N ์ ์์ ๋ํ 1์ ๊ฐ์ ๊ตฌํ๋ ํจ์
private static long getCount(long n) {
long count = n & 1;
//N๋ณด๋ค ์์ 2โฟ์ n์ ์ต๋๊ฐ
int size = (int) (Math.log(n) / Math.log(2));
for (int i = size; i > 0; i--) {
//DP[i-1] : 000 ~ 111 ๊ฐ์
//N - (1L << i) : ์ง์ ๋ 1์ด ๋ฐ๋ณต ์ฌ์ฉ๋ ๊ฐ์
// + 1 : 1000...
if((n & (1L << i))!=0L){
count += dp[i - 1] + (n - (1L << i)) + 1;
n -= (1L << i);
}
}
return count;
}
}
//์ฐธ๊ณ : https://tussle.tistory.com/1022
โณํ๊ณ
- ์ํ์ ์ผ๋ก ํ์ด๋ธ๋ค๋ ๋๋๊น์ง๋ง ์ก๊ณ ์ ํ์ ๋์ถ๊น์ง ์คํจํ๋ค.
- ๋ธ๋ก๊ทธ๋ฅผ ์ฐพ์๋ณด๋ฉฐ ํ์ด๋ฅผ ํ๋๋ฐ๋ ์ด๋ ต๋ค.
728x90
'Algorithm > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Algorithm/Baekjoon] ๊ฒน์น๋ ๊ฑด ์ซ์ด - 20922 (S1/JAVA) (0) | 2023.06.21 |
---|---|
[Algorithm/Baekjoon] ๋ ๊ฒ์ - 9655 (S5/JAVA) (1) | 2023.06.19 |
[Algorithm/Baekjoon] ์ปจ๋ฒ ์ด์ด ๋ฒจํธ ์์ ๋ก๋ด - 20055 (G5/JAVA) (0) | 2023.06.13 |
[Algorithm/Baekjoon] ์งํฉ - 11723 (S4/JAVA) (1) | 2023.06.11 |
[Algorithm/Baekjoon] ์ฌ์ด ์ต๋จ๊ฑฐ๋ฆฌ - 14940 (S1/JAVA) (0) | 2023.06.10 |