-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
36 lines (34 loc) · 840 Bytes
/
Solution.cs
File metadata and controls
36 lines (34 loc) · 840 Bytes
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
public class Solution
{
int mod = (int)1e9 + 7;
public int NumSubseq(int[] nums, int target)
{
long ret = 0;
int n = nums.Length;
Array.Sort(nums);
for (int i = 0, j = n - 1; i < n; i++)
{
while (j >= i && nums[j] + nums[i] > target) j--;
// can choose from [i..j]
// total = 0Ck + 1Ck + 2Ck +.. +kCk = 2^k
long val = FastPower(2, j - i);
ret = (ret + val) % mod;
}
return (int)((ret + mod) % mod);
}
long FastPower(long a, long b)
{
if (b < 0) return 0;
long ans = 1;
while (b > 0)
{
if ((b & 1) > 0)
{
ans = ans * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return ans;
}
}