# biweekly-contest-24

## A

### Statement

``````输入：nums = [-3,2,-3,4,2]

累加求和
startValue = 4 | startValue = 5 | nums
(4 -3 ) = 1  | (5 -3 ) = 2    |  -3
(1 +2 ) = 3  | (2 +2 ) = 4    |   2
(3 -3 ) = 0  | (4 -3 ) = 1    |  -3
(0 +4 ) = 4  | (1 +4 ) = 5    |   4
(4 +2 ) = 6  | (5 +2 ) = 7    |   2
``````

``````输入：nums = [1,2]

``````

``````输入：nums = [1,-2,-3]

``````

• `1 <= nums.length <= 100`
• `-100 <= nums[i] <= 100`

Given an array of integers `nums`, you start with an initial positive value startValue.

In each iteration, you calculate the step by step sum of startValue plus elements in `nums` (from left to right).

Return the minimum positive value of startValue such that the step by step sum is never less than 1.

Example 1:

``````Input: nums = [-3,2,-3,4,2]
Output: 5
Explanation: If you choose startValue = 4, in the third iteration your step by step sum is less than 1.
step by step sum
startValue = 4 | startValue = 5 | nums
(4 -3 ) = 1  | (5 -3 ) = 2    |  -3
(1 +2 ) = 3  | (2 +2 ) = 4    |   2
(3 -3 ) = 0  | (4 -3 ) = 1    |  -3
(0 +4 ) = 4  | (1 +4 ) = 5    |   4
(4 +2 ) = 6  | (5 +2 ) = 7    |   2
``````

Example 2:

``````Input: nums = [1,2]
Output: 1
Explanation: Minimum start value should be positive.
``````

Example 3:

``````Input: nums = [1,-2,-3]
Output: 5
``````

Constraints:

• `1 <= nums.length <= 100`
• `-100 <= nums[i] <= 100`

## B

### Statement

• F1 = 1
• F2 = 1
• Fn = Fn-1 + Fn-2 ， 其中 n > 2 。

``````输入：k = 7

``````输入：k = 10

``````

``````输入：k = 19

``````

• `1 <= k <= 10^9`

Given an integer `k`, return the minimum number of Fibonacci numbers whose sum is equal to `k`. The same Fibonacci number can be used multiple times.

The Fibonacci numbers are defined as:

• `F1 = 1`
• `F2 = 1`
• `Fn = Fn-1 + Fn-2` for `n > 2.`
It is guaranteed that for the given constraints we can always find such Fibonacci numbers that sum up to `k`.

Example 1:

``````Input: k = 7
Output: 2
Explanation: The Fibonacci numbers are: 1, 1, 2, 3, 5, 8, 13, …
For k = 7 we can use 2 + 5 = 7.``````

Example 2:

``````Input: k = 10
Output: 2
Explanation: For k = 10 we can use 2 + 8 = 10.
``````

Example 3:

``````Input: k = 19
Output: 3
Explanation: For k = 19 we can use 1 + 5 + 13 = 19.
``````

Constraints:

• `1 <= k <= 109`

## C

### Statement

• 仅包含小写字母 `['a', 'b', 'c']`.
• 对所有在 `1` 到 `s.length - 1` 之间的 `i` ，满足 `s[i] != s[i + 1]` （字符串的下标从 1 开始）。

``````输入：n = 1, k = 3

``````

``````输入：n = 1, k = 4

``````

``````输入：n = 3, k = 9

``````

``````输入：n = 2, k = 7

``````

``````输入：n = 10, k = 100

``````

• `1 <= n <= 10`
• `1 <= k <= 100`

A happy string is a string that:

• consists only of letters of the set `['a', 'b', 'c']`.
• `s[i] != s[i + 1]` for all values of `i` from `1` to `s.length - 1` (string is 1-indexed).

For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings.

Given two integers `n` and `k`, consider a list of all happy strings of length `n` sorted in lexicographical order.

Return the kth string of this list or return an empty string if there are less than `k` happy strings of length `n`.

Example 1:

``````Input: n = 1, k = 3
Output: "c"
Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c".
``````

Example 2:

``````Input: n = 1, k = 4
Output: ""
Explanation: There are only 3 happy strings of length 1.
``````

Example 3:

``````Input: n = 3, k = 9
Output: "cab"
Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab"
``````

Constraints:

• `1 <= n <= 10`
• `1 <= k <= 100`

### Solution

``````#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define SZ(x) int((x).size())
#define endl "\n"
using db = double;
using ll = long long;
using ull = unsigned long long;
using pII = pair<int, int>;
using pLL = pair<ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2>
inline void chadd(T1 &x, T2 y, int Mod = mod) {
x += y;
while (x >= Mod) x -= Mod;
while (x < 0) x += Mod;
}
template <class T1, class T2>
inline void chmax(T1 &x, T2 y) {
if (x < y)
x = y;
}
template <class T1, class T2>
inline void chmin(T1 &x, T2 y) {
if (x > y)
x = y;
}
inline int nextInt() {
int x;
cin >> x;
return x;
}
void rd() {}
template <class T, class... Ts>
void rd(T &arg, Ts &...args) {
cin >> arg;
rd(args...);
}
#define dbg(x...)                             \
do {                                      \
cout << "\033[32;1m" << #x << " -> "; \
err(x);                               \
} while (0)
void err() {
cout << "\033[39;0m" << endl;
}
template <class T, class... Ts>
void err(const T &arg, const Ts &...args) {
cout << arg << ' ';
err(args...);
}
template <template <typename...> class T, typename t, typename... A>
void err(const T<t> &arg, const A &...args) {
for (auto &v : arg) cout << v << ' ';
err(args...);
}
void ptt() {
cout << endl;
}
template <class T, class... Ts>
void ptt(const T &arg, const Ts &...args) {
cout << ' ' << arg;
ptt(args...);
}
template <class T, class... Ts>
void pt(const T &arg, const Ts &...args) {
cout << arg;
ptt(args...);
}
void pt() {}
template <template <typename...> class T, typename t, typename... A>
void pt(const T<t> &arg, const A &...args) {
for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1];
pt(args...);
}
inline ll qpow(ll base, ll n) {
assert(n >= 0);
ll res = 1;
while (n) {
if (n & 1)
res = res * base % mod;
base = base * base % mod;
n >>= 1;
}
return res;
}
constexpr int N = 1e5 + 10;

int n, k;

vector<string> vec;
void dfs(int cur, char pre, string now) {
if (cur == n) {
vec.push_back(now);
return;
}
for (auto &it : {'a', 'b', 'c'}) {
if (it != pre) {
string tmp = now;
tmp += it;
dfs(cur + 1, it, tmp);
if (SZ(vec) > k)
return;
}
}
}

class Solution {
public:
string getHappyString(int _n, int _k) {
n = _n, k = _k;
vec.clear();
dfs(0, 0, "");
sort(vec.begin(), vec.end());
if (k > SZ(vec))
return "";
return vec[k - 1];
}
};

#ifdef LOCAL

int main() {
return 0;
}

#endif
``````

## D

### Statement

• Difficulty: Hard
• Tag: `字符串` `动态规划`

``````输入：s = "1000", k = 10000

``````

``````输入：s = "1000", k = 10

``````

``````输入：s = "1317", k = 2000

``````

``````输入：s = "2020", k = 30

``````

``````输入：s = "1234567890", k = 90

``````

• `1 <= s.length <= 10^5`.
• `s` 只包含数字且不包含前导 0 。
• `1 <= k <= 10^9`.

• Difficulty: Hard
• Tag: `String` `Dynamic Programming`

A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of digits `s` and all we know is that all integers in the array were in the range `[1, k]` and there are no leading zeros in the array.

Given the string `s` and the integer `k`, return the number of the possible arrays that can be printed as `s` using the mentioned program. Since the answer may be very large, return it modulo `109 + 7`.

Example 1:

``````Input: s = "1000", k = 10000
Output: 1
Explanation: The only possible array is [1000]
``````

Example 2:

``````Input: s = "1000", k = 10
Output: 0
Explanation: There cannot be an array that was printed this way and has all integer >= 1 and <= 10.
``````

Example 3:

``````Input: s = "1317", k = 2000
Output: 8
Explanation: Possible arrays are [1317],[131,7],[13,17],[1,317],[13,1,7],[1,31,7],[1,3,17],[1,3,1,7]
``````

Constraints:

• `1 <= s.length <= 105`
• `s` consists of only digits and does not contain leading zeros.
• `1 <= k <= 109`

### Solution

``````#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define SZ(x) int((x).size())
#define endl "\n"
using db = double;
using ll = long long;
using ull = unsigned long long;
using pII = pair<int, int>;
using pLL = pair<ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2>
inline void chadd(T1 &x, T2 y, int Mod = mod) {
x += y;
while (x >= Mod) x -= Mod;
while (x < 0) x += Mod;
}
template <class T1, class T2>
inline void chmax(T1 &x, T2 y) {
if (x < y)
x = y;
}
template <class T1, class T2>
inline void chmin(T1 &x, T2 y) {
if (x > y)
x = y;
}
inline int nextInt() {
int x;
cin >> x;
return x;
}
void rd() {}
template <class T, class... Ts>
void rd(T &arg, Ts &...args) {
cin >> arg;
rd(args...);
}
#define dbg(x...)                             \
do {                                      \
cout << "\033[32;1m" << #x << " -> "; \
err(x);                               \
} while (0)
void err() {
cout << "\033[39;0m" << endl;
}
template <class T, class... Ts>
void err(const T &arg, const Ts &...args) {
cout << arg << ' ';
err(args...);
}
template <template <typename...> class T, typename t, typename... A>
void err(const T<t> &arg, const A &...args) {
for (auto &v : arg) cout << v << ' ';
err(args...);
}
void ptt() {
cout << endl;
}
template <class T, class... Ts>
void ptt(const T &arg, const Ts &...args) {
cout << ' ' << arg;
ptt(args...);
}
template <class T, class... Ts>
void pt(const T &arg, const Ts &...args) {
cout << arg;
ptt(args...);
}
void pt() {}
template <template <typename...> class T, typename t, typename... A>
void pt(const T<t> &arg, const A &...args) {
for (int i = 0, sze = arg.size(); i < sze; ++i) cout << arg[i] << " \n"[i == sze - 1];
pt(args...);
}
inline ll qpow(ll base, ll n) {
assert(n >= 0);
ll res = 1;
while (n) {
if (n & 1)
res = res * base % mod;
base = base * base % mod;
n >>= 1;
}
return res;
}
const int N = 1e5 + 10;
string s;
ll k, f[N];

class Solution {
public:
int numberOfArrays(string s, int k) {
//		cout << s << " " << k << endl;
int n = SZ(s);
for (int i = 0; i <= n; ++i) f[i] = 0;
f[0] = 1;
for (int i = 1; i <= n; ++i) {
ll num = 0, bit = 1;
for (int j = i; j >= max(1, i - 12); --j) {
num = bit * (s[j - 1] - '0') + num;
bit *= 10;
//	dbg(i, j, num, (s[j] - '0'), s[j]);
if (s[j - 1] != '0' && num >= 1 && num <= k) {
// dbg(i, j, num);
}
if (num > k)
break;
}
}
return f[n];
}
};

#ifdef LOCAL

int main() {
return 0;
}

#endif
``````