-
Notifications
You must be signed in to change notification settings - Fork 0
/
wk398.java
142 lines (116 loc) · 3.41 KB
/
wk398.java
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package weekly;
import java.util.Arrays;
import java.util.HashMap;
public class wk398 {
//遍历
public boolean isArraySpecial(int[] nums) {
for (int i = 1; i < nums.length; i++) {
if (nums[i] % 2 == nums[i - 1] % 2) {
return false;
}
}
return true;
}
//前缀和
static public boolean[] isArraySpecial(int[] nums, int[][] queries) {
int[] dp = new int[nums.length + 1];
int index = 1;
dp[1] = index;
for (int i = 1; i < nums.length; i++) {
if (nums[i] % 2 == nums[i - 1] % 2) {
index++;
}
dp[i + 1] = index;
}
int[] pre = new int[nums.length + 1];
for (int i = 1; i < dp.length; i++) {
pre[i] = pre[i - 1] + dp[i];
}
boolean[] res = new boolean[queries.length];
for (int i = 0; i < queries.length; i++) {
int[] query = queries[i];
int c = pre[query[1] + 1] - pre[query[0]];
if (c == dp[query[0] + 1] * (query[1] - query[0] + 1)) {
res[i] = true;
}
}
return res;
}
//遍历
public long sumDigitDifferences(int[] nums) {
int d = 1;
long res = 0;
while (nums[0] / d > 0) {
int[] count = new int[10];
long tmp = 1;
for (int num : nums) {
int c = (num / d) % 10;
count[c]++;
}
for (int i = 0; i < count.length; i++) {
for (int j = i + 1; j < count.length; j++) {
res += (long) count[i] * count[j];
}
}
d *= 10;
}
return res;
}
// 记忆化
/* public int waysToReachStair(int k) {
return dfs(1, k, 0, 0);
}
HashMap<Long,Integer> memo=new HashMap<>();
int dfs(int cur, int k, int jump, int back) {
if (cur > k + 1) return 0;
long key=((long)cur<<32)|((long) jump<<1)|back;
if (memo.containsKey(key)) return memo.get(key);
int res = 0;
if (cur == k) res++;
if (back == 0 && cur > 0) {
res += dfs(cur - 1, k, jump, 1);
}
res += dfs(cur + (int) Math.pow(2, jump), k, jump + 1, 0);
memo.put(key,res);
return res;
}
*/
static int[][] dp = new int[32][2];
static {
int a = 1;
dp[0][1] = 1;
for (int i = 0; i < 31; i++) {
dp[i + 1][0] = dp[i][0] + a;
dp[i + 1][1] = i + 2;
a *= 2;
}
// for (int i = 0; i < dp.length; i++) {
// System.out.println(dp[i][0] + " " + dp[i][1]);
// }
}
// 组合数
public int waysToReachStair(int k) {
int res = 0;
for (int i = 0; i < dp.length; i++) {
if (k <= 1+dp[i][0] &&k>=1+dp[i][0]-dp[i][1]) {
int dis= dp[i][1]-(1+dp[i][0]-k);
res+=comnination(dp[i][1],dis);
}
}
return res;
}
// m>=n
public int comnination(int m, int n) {
if (n == 0) return 1;
long ans = 1;
//都从小的开始 防止过早溢出
for (int x = m - n + 1, y = 1; y <= n; ++x, ++y) {
ans = ans * x / y;
}
return (int) ans;
}
public static void main(String[] args) {
wk398 w = new wk398();
w.waysToReachStair(1);
}
}