-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDH_148652.java
More file actions
32 lines (26 loc) ยท 1.13 KB
/
DH_148652.java
File metadata and controls
32 lines (26 loc) ยท 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
* ์ ์ฌ ์นธํ ์ด ๋นํธ์ด
*/
public class DH_148652 {
static long solution(int n, long l, long r) {
return getOneCnt(r, n) - getOneCnt(l - 1, n);
}
static long getOneCnt(long e, int depth) {
if(depth == 0) return 1; // 0๋ฒ์งธ ๋นํธ์ด: 1
long digit = (long) Math.pow(5, depth - 1); // depth๋ฒ์งธ์์ ํ ๊ทธ๋ฃน๋น ๋ช ๊ฐ์ ์๋ฆฟ์๋ฅผ ๊ฐ์ง๊ณ ์๋์ง
// e๊ฐ ์ํ๋ ๊ตฌ๊ฐ์ด ์ด๋์ธ์ง ๊ตฌํ๊ธฐ (0, 1, 2, 3, 4)
int area = (int) (e / digit);
if(e % digit == 0) area -= 1; // ๊ตฌ๊ฐ ๊ฒฝ๊ณ์ ์๋๊ฑฐ ์ฌ๋ฐ๋ฅธ ๊ตฌ๊ฐ์ ํฌํจ๋ ์ ์๋๋ก -1 ํด์ฃผ๊ธฐ
int prevOneCnt = (int) Math.pow(4, depth - 1);
// 0๊ธฐ์ค ์ผ์ชฝ (1์ธ ๊ตฌ์ญ์ ๊ฐ์๊ฐ ์ ์ฒด ์๋ฆฌ์)๊ฐ
if(area < 2) return prevOneCnt * area + getOneCnt(e - (digit * area), depth - 1);
// 0์๋ ๋ถ๋ถ
else if(area == 2) return prevOneCnt * area;
// 0๊ธฐ์ค ์ค๋ฅธ์ชฝ (1์ธ ๊ตฌ์ญ์ ๊ฐ์๊ฐ ์ ์ฒด ์๋ฆฌ์ - 1)๊ฐ
return prevOneCnt * (area - 1) + getOneCnt(e - (digit * area), depth - 1);
}
public static void main(String[] args) {
int n = 2, l = 4, r = 17;
System.out.println(solution(n, l, r));
}
}