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
| const MOD = 1_000_000_007
func countWays(ranges [][]int) int { sort.Slice(ranges, func(i, j int) bool { return ranges[i][0] <= ranges[j][0] }) count := 0 pre := -1 for _, r := range ranges { if pre < r[0] { count++ } pre = max(pre, r[1]) } return quickSort(2%MOD, count, MOD) }
func quickSort(num, x, mod int) int { if x == 0 { return 1 } else if x == 1 { return num % mod } half := x >> 1 halfAns := quickSort(num, half, mod) ans := halfAns * halfAns % mod if (x & 1) == 1 { ans = ans * num % mod } return ans }
|