声明:A-C原创,D、E借鉴作者cup-pyy
借鉴网址:https://zhuanlan.zhihu.com/p/582922871
一个不知名大学生,江湖人称菜狗
original author: jacky Li
Email : 3435673055@qq.com
Last edited: 2022.11.13
"""
大佬绕路,这里菜狗
目录
E. Yet Another Array Counting Problem
A. The Ultimate Square
You have nn rectangular wooden blocks, which are numbered from 11 to nn. The ii-th block is 11 unit high and ⌈i2⌉⌈i2⌉ units long.
Here, ⌈x2⌉⌈x2⌉ denotes the result of division of xx by 22, rounded up. For example, ⌈42⌉=2⌈42⌉=2 and ⌈52⌉=⌈2.5⌉=3⌈52⌉=⌈2.5⌉=3.
For example, if n=5n=5, then the blocks have the following sizes: 1×11×1, 1×11×1, 1×21×2, 1×21×2, 1×31×3.
The available blocks for n=5n=5
Find the maximum possible side length of a square you can create using these blocks, without rotating any of them. Note that you don't have to use all of the blocks.
One of the ways to create 3×33×3 square using blocks 11 through 55
Input
Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.
The first line of each test case contains a single integer nn (1≤n≤1091≤n≤109) — the number of blocks.
Output
For each test case, print one integer — the maximum possible side length of a square you can create.
Example
input
3
2
5
197654321
output
1
3
98827161
Note
In the first test case, you can create a 1×11×1 square using only one of the blocks.
In the second test case, one of the possible ways to create a 3×33×3 square is shown in the statement. It is impossible to create a 4×44×4 or larger square, so the answer is 33.
Code
#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstring>
#define IOS std::ios::sync_with_stdio(false)
using namespace std;
typedef long long LL;
const int N = 2e5;
char ch[N];
void solve()
{
IOS; int n; cin >> n;
if(n % 2 == 0) cout << n / 2 << endl;
else cout << n / 2 + 1 << endl;
return;
}
signed main()
{
IOS; int T; cin >> T;
while( T -- ) solve();
return 0;
}
B. Diverse Substrings
A non-empty digit string is diverse if the number of occurrences of each character in it doesn't exceed the number of distinct characters in it.
For example:
- string "7" is diverse because 7 appears in it 11 time and the number of distinct characters in it is 11;
- string "77" is not diverse because 7 appears in it 22 times and the number of distinct characters in it is 11;
- string "1010" is diverse because both 0 and 1 appear in it 22 times and the number of distinct characters in it is 22;
- string "6668" is not diverse because 6 appears in it 33 times and the number of distinct characters in it is 22.
You are given a string ss of length nn, consisting of only digits 00 to 99. Find how many of its n(n+1)2n(n+1)2 substrings are diverse.
A string aa is a substring of a string bb if aa can be obtained from bb by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
Note that if the same diverse string appears in ss multiple times, each occurrence should be counted independently. For example, there are two diverse substrings in "77" both equal to "7", so the answer for the string "77" is 22.
Input
Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.
The first line of each test case contains a single integer nn (1≤n≤1051≤n≤105) — the length of the string ss.
The second line of each test case contains a string ss of length nn. It is guaranteed that all characters of ss are digits from 00 to 99.
It is guaranteed that the sum of nn over all test cases does not exceed 105105.
Output
For each test case print one integer — the number of diverse substrings of the given string ss.
Example
input
7
1
7
2
77
4
1010
5
01100
6
399996
5
23456
18
789987887987998798
output
1
2
10
12
10
15
106
Note
In the first test case, the diverse substring is "7".
In the second test case, the only diverse substring is "7", which appears twice, so the answer is 22.
In the third test case, the diverse substrings are "0" (22 times), "01", "010", "1" (22 times), "10" (22 times), "101" and "1010".
In the fourth test case, the diverse substrings are "0" (33 times), "01", "011", "0110", "1" (22 times), "10", "100", "110" and "1100".
In the fifth test case, the diverse substrings are "3", "39", "399", "6", "9" (44 times), "96" and "996".
In the sixth test case, all 1515 non-empty substrings of "23456" are diverse.
Code
#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstring>
#define IOS std::ios::sync_with_stdio(false)
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
char ch[N];
int str[10];
void solve()
{
IOS; int n, op = 0; cin >> n >> (ch + 1);
for(int i = 1; i <= n; i ++)
{
map<char, int> h;
int maxx = 0, sst = min(n, i + 100);
for(int j = i; j <= sst; j ++)
{
h[ch[j]] ++;
maxx = max(maxx, h[ch[j]]);
int t = h.size();
if(maxx <= t) op ++;
}
}
cout << op << endl;
return ;
}
signed main()
{
IOS; int T; cin >> T;
while( T -- ) solve();
return 0;
}
C. Zero-Sum Prefixes
The score of an array v1,v2,…,vnv1,v2,…,vn is defined as the number of indices ii (1≤i≤n1≤i≤n) such that v1+v2+…+vi=0v1+v2+…+vi=0.
You are given an array a1,a2,…,ana1,a2,…,an of length nn. You can perform the following operation multiple times:
- select an index ii (1≤i≤n1≤i≤n) such that ai=0ai=0;
- then replace aiai by an arbitrary integer.
What is the maximum possible score of aa that can be obtained by performing a sequence of such operations?
Input
Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.
The first line of each test case contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of the array aa.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109) — array aa.
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.
Output
For each test case, print the maximum possible score of the array aa after performing a sequence of operations.
Example
input
5
5
2 0 1 -1 0
3
1000000000 1000000000 0
4
0 0 0 0
8
3 0 2 -10 10 -30 30 0
9
1 0 0 1 -1 0 1 0 -1
output
1
4
4
5
Note
In the first test case, it is optimal to change the value of a2a2 to −2−2 in one operation.
The resulting array aa will be [2,−2,1,−1,0][2,−2,1,−1,0], with a score of 33:
- a1+a2=2−2=0a1+a2=2−2=0;
- a1+a2+a3+a4=2−2+1−1=0a1+a2+a3+a4=2−2+1−1=0;
- a1+a2+a3+a4+a5=2−2+1−1+0=0a1+a2+a3+a4+a5=2−2+1−1+0=0.
In the second test case, it is optimal to change the value of a3a3 to −2000000000−2000000000, giving us an array with a score of 11.
In the third test case, it is not necessary to perform any operations.
Code
#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstring>
#define IOS std::ios::sync_with_stdio(false)
using namespace std;
typedef long long LL;
void add(int x)
{
mp[x] ++ ;
mx = max(mp[x], mx);
}
void clear(int x)
{
mx = cnt = 0;
for(int i = x; i <= n ; i ++ )
if(st[i]) return ;
else mp[s[i]] -- , st[i] = true;
}
void solve()
{
IOS; cin >> n;
mp.clear();
mx = cnt = 0;
for(int i = 1; i <= n ; i ++ ) {
cin >> a[i];
s[i] = s[i - 1] + a[i];
st[i] = 0;
}
int ans = 0, last = n + 1;
for(int i = n; i >= 1 ; i -- ) {
add(s[i]);
if(a[i] == 0) {
ans += max(1ll, mx);
last = i;
clear(i);
}
}
for(int i = 1; i <= n ; i ++ ) {
if(a[i] == 0) break;
else if(s[i] == 0) ans ++ ;
}
cout << ans << endl;
}
signed main()
{
IOS; int T; cin >> T;
while(T --) solve();
return 0;
}
D. ConstructOR
You are given three integers aa, bb, and dd. Your task is to find any integer xx which satisfies all of the following conditions, or determine that no such integers exist:
- 0≤x<2600≤x<260;
- a|xa|x is divisible by dd;
- b|xb|x is divisible by dd.
Here, || denotes the bitwise OR operation.
Input
Each test contains multiple test cases. The first line of input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases.
Each test case consists of one line, containing three integers aa, bb, and dd (1≤a,b,d<2301≤a,b,d<230).
Output
For each test case print one integer. If there exists an integer xx which satisfies all of the conditions from the statement, print xx. Otherwise, print −1−1.
If there are multiple solutions, you may print any of them.
Example
input
8
12 39 5
6 8 14
100 200 200
3 4 6
2 2 2
18 27 3
420 666 69
987654321 123456789 999999999
output
18
14
-1
-1
0
11
25599
184470016815529983
Note
In the first test case, x=18x=18 is one of the possible solutions, since 39|18=5539|18=55 and 12|18=3012|18=30, both of which are multiples of d=5d=5.
In the second test case, x=14x=14 is one of the possible solutions, since 8|14=6|14=148|14=6|14=14, which is a multiple of d=14d=14.
In the third and fourth test cases, we can show that there are no solutions.
Code
#include<iostream>
#include<cstring>
using namespace std;
using LL = long long;
int main(){
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int T;
cin >> T;
while(T--){
LL a, b, d;
cin >> a >> b >> d;
LL k = a | b;
bool success = 1;
for(int i = 0; i < 30; i++){
if (d >> i & 1) break;
if (k >> i & 1){
success = 0;
break;
}
}
if (!success){
cout << -1 << '\n';
continue;
}
LL ans = k;
LL zeros = __builtin_ctzll(d);
LL t = 0;
for(int i = 0; i < 60; i++){
if (t >> i & 1) ans |= 1LL << i;
else if (k >> i & 1){
ans |= 1LL << i;
t += (d << (i - zeros));
}
}
cout << ans << '\n';
}
}
E. Yet Another Array Counting Problem
The position of the leftmost maximum on the segment [l;r][l;r] of array x=[x1,x2,…,xn]x=[x1,x2,…,xn] is the smallest integer ii such that l≤i≤rl≤i≤r and xi=max(xl,xl+1,…,xr)xi=max(xl,xl+1,…,xr).
You are given an array a=[a1,a2,…,an]a=[a1,a2,…,an] of length nn. Find the number of integer arrays b=[b1,b2,…,bn]b=[b1,b2,…,bn] of length nn that satisfy the following conditions:
- 1≤bi≤m1≤bi≤m for all 1≤i≤n1≤i≤n;
- for all pairs of integers 1≤l≤r≤n1≤l≤r≤n, the position of the leftmost maximum on the segment [l;r][l;r] of the array bb is equal to the position of the leftmost maximum on the segment [l;r][l;r] of the array aa.
Since the answer might be very large, print its remainder modulo 109+7109+7.
Input
Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1031≤t≤103) — the number of test cases.
The first line of each test case contains two integers nn and mm (2≤n,m≤2⋅1052≤n,m≤2⋅105, n⋅m≤106n⋅m≤106).
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤m1≤ai≤m) — the array aa.
It is guaranteed that the sum of n⋅mn⋅m over all test cases doesn't exceed 106106.
Output
For each test case print one integer — the number of arrays bb that satisfy the conditions from the statement, modulo 109+7109+7.
Example
input
4
3 3
1 3 2
4 2
2 2 2 2
6 9
6 9 6 9 6 9
9 100
10 40 20 20 100 60 80 60 60
output
8
5
11880
351025663
Note
In the first test case, the following 88 arrays satisfy the conditions from the statement:
- [1,2,1][1,2,1];
- [1,2,2][1,2,2];
- [1,3,1][1,3,1];
- [1,3,2][1,3,2];
- [1,3,3][1,3,3];
- [2,3,1][2,3,1];
- [2,3,2][2,3,2];
- [2,3,3][2,3,3].
In the second test case, the following 55 arrays satisfy the conditions from the statement:
- [1,1,1,1][1,1,1,1];
- [2,1,1,1][2,1,1,1];
- [2,2,1,1][2,2,1,1];
- [2,2,2,1][2,2,2,1];
- [2,2,2,2][2,2,2,2].
Code
#include<iostream>
#include<cstring>
#include<algorithm>
#include<functional>
using namespace std;
using LL = long long;
const int maxn = 2e5 + 5;
template<const int T>
struct ModInt {
const static int mod = T;
int x;
ModInt(int x = 0) : x(x % mod) {}
ModInt(long long x) : x(int(x % mod)) {}
int val() { return x; }
ModInt operator + (const ModInt &a) const { int x0 = x + a.x; return ModInt(x0 < mod ? x0 : x0 - mod); }
ModInt operator - (const ModInt &a) const { int x0 = x - a.x; return ModInt(x0 < 0 ? x0 + mod : x0); }
ModInt operator * (const ModInt &a) const { return ModInt(1LL * x * a.x % mod); }
ModInt operator / (const ModInt &a) const { return *this * a.inv(); }
void operator += (const ModInt &a) { x += a.x; if (x >= mod) x -= mod; }
void operator -= (const ModInt &a) { x -= a.x; if (x < 0) x += mod; }
void operator *= (const ModInt &a) { x = 1LL * x * a.x % mod; }
void operator /= (const ModInt &a) { *this = *this / a; }
friend ostream &operator<<(ostream &os, const ModInt &a) { return os << a.x;}
ModInt pow(int64_t n) const {
ModInt res(1), mul(x);
while(n){
if (n & 1) res *= mul;
mul *= mul;
n >>= 1;
}
return res;
}
ModInt inv() const {
int a = x, b = mod, u = 1, v = 0;
while (b) {
int t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
if (u < 0) u += mod;
return u;
}
};
typedef ModInt<1000000007> mint;
int stk[maxn], a[maxn], ls[maxn], rs[maxn];
int build(int n){
int top = 0;
for(int i = 1; i <= n; i++){
int pos = top;
while(pos && a[stk[pos]] < a[i]) pos--;
if (pos) rs[stk[pos]] = i;
if (pos < top) ls[i] = stk[pos + 1];
stk[top = ++pos] = i;
}
return stk[1];
}
int main(){
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int T;
cin >> T;
while(T--){
int n, m;
cin >> n >> m;
for(int i = 0; i <= n + 1; i++) ls[i] = rs[i] = 0;
for(int i = 1; i <= n; i++) cin >> a[i];
vector<vector<mint> > dp(n + 1, vector<mint>(m + 1, 0));
for(int i = 0; i <= m; i++) dp[0][i] = 1;
function<void(int)> dfs = [&](int u){
if (ls[u]) dfs(ls[u]);
if (rs[u]) dfs(rs[u]);
for(int i = 1; i <= m; i++){
dp[u][i] = dp[ls[u]][i - 1] * dp[rs[u]][i];
dp[u][i] += dp[u][i - 1];
}
};
int root = build(n);
dfs(root);
cout << dp[root][m] << '\n';;
}
}