别人聊着风花雪月,我想着恩格尔列,沙沙沙,沙净天下情侣狗
A - Divisible
Problem Statement
You are given positive integers N N N and K K K, and a sequence of length N N N, A = ( A 1 , A 2 , … , A N ) A=(A_1,A_2,\ldots,A_N) A=(A1,A2,…,AN).
Extract all elements of A A A that are multiples of K K K, divide them by K K K, and print the quotients.
思路:模拟即可
AC Code
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define int long long
#define pii pair<int, int>
#define pb push_back
#define gcd __gcd
#define lcm(a, b) (a * b) / gcd(a, b)
#define all(a) a.begin(), a.end()
#define mem(a, x) memset(a, x, sizeof(a))
#define f(i, s, e) for (int i = s; i <= e; i++)
#define ff(i, s, e) for (int i = s; i >= e; i--)
#define setbits(x) __builtin_popcount(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 100000007
#define maxn (ll)(2e5 + 5000)
#define INF LONG_LONG_MAX
using namespace std;
// set<int>::iterator it;
signed main(void)
{
ios::sync_with_stdio(false), cin.tie(nullptr);
int N, K;
cin >> N >> K;
vector<int> A(N);
for (int i = 0; i < N; i++)
cin >> A[i];
vector<int> result;
for (int i = 0; i < N; i++)
{
if (A[i] % K == 0)
result.push_back(A[i] / K);
}
for (int i = 0; i < result.size(); i++)
cout << result[i] << " ";
cout << endl;
return 0;
}
B - Substring
Problem Statement
You are given a string S S S consisting of lowercase English letters. How many different non-empty substrings does S S S have?
A substring is a contiguous subsequence. For example, xxx
is a substring of yxxxy
but not of xxyxx
.
最纯真的暴力,没有思路
AC Code
#include <bits/stdc++.h>
using namespace std;
int countDistinctSubstrings(string S) {
int n = S.size();
vector<string> suffixes(n);
for (int i = 0; i < n; i++) {
suffixes[i] = S.substr(i);
}
sort(suffixes.begin(), suffixes.end());
int count = suffixes[0].size();
for (int i = 1; i < n; i++) {
int lcp = 0;
while (lcp < suffixes[i].size() && lcp < suffixes[i - 1].size() && suffixes[i][lcp] == suffixes[i - 1][lcp]) {
lcp++;
}
count += suffixes[i].size() - lcp;
}
return count;
}
int main() {
string S;
cin >> S;
cout << countDistinctSubstrings(S) << endl;
return 0;
}
C - Ideal Holidays
Problem Statement
In the Kingdom of AtCoder, a week consists of A + B A+B A+B days, with the first through A A A-th days being holidays and the ( A + 1 ) (A+1) (A+1)-th through ( A + B ) (A+B) (A+B)-th being weekdays.
Takahashi has N N N plans, and the i i i-th plan is scheduled D i D_i Di days later.
He has forgotten what day of the week it is today. Determine if it is possible for all of his N N N plans to be scheduled on holidays.
思路:这道题思维较难,比D和E的思维难,题目说不知道现在是星期几,我们不妨设现在是第0天,然后通过取模,将他们压缩在一个周期即[0,A+B)中,此时去考察,此时发现,如果存在两个相邻点的距离大于b,或者最大间距小于a,就可保证都在节假日内(如果想不太明白,可以画个图看一下)
AC Code
#include <algorithm>
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define int long long
#define pii pair<int, int>
#define pb push_back
#define gcd __gcd
#define lcm(a, b) (a * b) / gcd(a, b)
#define all(a) a.begin(), a.end()
#define mem(a, x) memset(a, x, sizeof(a))
#define f(i, s, e) for (int i = s; i <= e; i++)
#define ff(i, s, e) for (int i = s; i >= e; i--)
#define setbits(x) __builtin_popcount(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 100000007
#define maxn (ll)(2e5 + 5000)
#define INF LONG_LONG_MAX
using namespace std;
// set<int>::iterator it;
int n, a, b;
int d[maxn];
signed main(void)
{
ios::sync_with_stdio(false), cin.tie(nullptr);
cin >> n >> a >> b;
f(i, 1, n)
{
cin >> d[i];
d[i] %= (a + b);
}
sort(d + 1, d + n + 1);
int newSize = (unique(d + 1, d + n + 1) - (d + 1));
f(i, 2, newSize)
{
if (d[i] - d[i - 1] > b)
{
cout << "Yes" << endl;
return 0;
}
}
if (d[newSize] - d[1] < a)
{
cout << "Yes" << endl;
return 0;
}
cout << "No" << endl;
return 0;
}
D - Popcount and XOR
Problem Statement
You are given non-negative integers a a a, b b b, and C C C. Determine if there is a pair of non-negative integers ( X , Y ) (X, Y) (X,Y) that satisfies all of the following five conditions. If such a pair exists, print one.
- KaTeX parse error: Expected 'EOF', got '&' at position 10: 0 \leq X &̲lt; 2^{60}
- KaTeX parse error: Expected 'EOF', got '&' at position 10: 0 \leq Y &̲lt; 2^{60}
- popcount ( X ) = a \operatorname{popcount}(X) = a popcount(X)=a
- popcount ( Y ) = b \operatorname{popcount}(Y) = b popcount(Y)=b
- X ⊕ Y = C X \oplus Y = C X⊕Y=C
Here, ⊕ \oplus ⊕ denotes the bitwise XOR.
If multiple pairs ( X , Y ) (X, Y) (X,Y) satisfy the conditions, you may print any of them.
What is popcount?
For a non-negative integer x x x, the popcount of x x x is the number of 1 1 1s in the binary representation of x x x. More precisely, for a non-negative integer x x x such that x = ∑ i = 0 ∞ b i 2 i ( b i ∈ { 0 , 1 } ) \displaystyle x=\sum _ {i=0} ^ \infty b _ i2 ^ i\ (b _ i\in\lbrace0,1\rbrace) x=i=0∑∞bi2i (bi∈{0,1}), we have popcount ( x ) = ∑ i = 0 ∞ b i \displaystyle\operatorname{popcount}(x)=\sum _ {i=0} ^ \infty b _ i popcount(x)=i=0∑∞bi.
For example,
13
13
13 in binary is 1101
, so
popcount
(
13
)
=
3
\operatorname{popcount}(13)=3
popcount(13)=3.What is bitwise XOR?
For non-negative integers x , y x, y x,y, the bitwise exclusive OR x ⊕ y x \oplus y x⊕y is defined as follows.
- The 2 k 2^k 2k’s place ( k ≥ 0 ) \ (k\geq0) (k≥0) in the binary representation of x ⊕ y x \oplus y x⊕y is 1 1 1 if exactly one of the 2 k 2^k 2k’s places ( k ≥ 0 ) \ (k\geq0) (k≥0) in the binary representations of x x x and y y y is 1 1 1, and 0 0 0 otherwise.
For example,
9
9
9 and
3
3
3 in binary are 1001
and 0011
, respectively, so
9
⊕
3
=
10
9 \oplus 3 = 10
9⊕3=10 (in binary, 1010
).
思路:题目挺吓人,实际非常简单,就是代码容易写错,需要调。按照题意,一步步来,首先先算出c中的1的个数,如果大于(a+b),直接结束。否则一步步构造即可(语言能力有限,just look look my code)
AC Code
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define int long long
#define pii pair<int, int>
#define pb push_back
#define gcd __gcd
#define lcm(a, b) (a * b) / gcd(a, b)
#define all(a) a.begin(), a.end()
#define mem(a, x) memset(a, x, sizeof(a))
#define f(i, s, e) for (int i = s; i <= e; i++)
#define ff(i, s, e) for (int i = s; i >= e; i--)
#define setbits(x) __builtin_popcount(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 100000007
#define maxn (ll)(2e5 + 5000)
#define INF LONG_LONG_MAX
using namespace std;
// set<int>::iterator it;
string s = "";
int cnt = 0;
void change(int x)
{
while (x)
{
if (x % 2 == 0)
{
s += '0';
x /= 2;
}
else
{
s += '1';
x /= 2;
cnt++;
}
}
int fuck = s.size();
f(i, fuck, 60)
{
s += '0';
}
}
int qpow(int a, int b)
{
int ans = 1;
while (b)
{
if (b & 1)
{
ans *= a;
}
a *= a;
b >>= 1;
}
return ans;
}
int check(string x)
{
int ans = 0;
f(i, 0, x.size() - 1)
{
if (x[i] == '1')
{
ans += qpow(2, i);
}
}
return ans;
}
signed main(void)
{
ios::sync_with_stdio(false), cin.tie(nullptr);
int a, b, c;
cin >> a >> b >> c;
change(c);
if (cnt > a + b)
{
cout << -1 << endl;
return 0;
}
string x, y;
int t = s.size();
f(i, 0, s.size() - 1)
{
if (s[i] == '1')
{
if (a <= b)
{
x += '0';
y += '1';
b--;
cnt--;
}
else
{
x += '1';
y += '0';
a--;
cnt--;
}
}
else if (s[i] == '0' && cnt == a + b)
{
x += '0';
y += '0';
}
else if (s[i] == '0' && cnt < (a + b) - 1)
{
x += '1';
y += '1';
a--;
b--;
}
if (a == 0 && b == 0)
{
break;
}
}
if (a != 0 || b != 0)
{
cout << -1 << endl;
return 0;
}
cout << check(x) << " " << check(y) << endl;
return 0;
}
E - Set Add Query
Problem Statement
There is an integer sequence A = ( A 1 , A 2 , … , A N ) A=(A_1,A_2,\ldots,A_N) A=(A1,A2,…,AN) of length N N N, where all elements are initially set to 0 0 0. Also, there is a set S S S, which is initially empty.
Perform the following Q Q Q queries in order. Find the value of each element in the sequence A A A after processing all Q Q Q queries. The i i i-th query is in the following format:
- An integer x i x_i xi is given. If the integer x i x_i xi is contained in S S S, remove x i x_i xi from S S S. Otherwise, insert x i x_i xi to S S S. Then, for each j = 1 , 2 , … , N j=1,2,\ldots,N j=1,2,…,N, add ∣ S ∣ |S| ∣S∣ to A j A_j Aj if j ∈ S j\in S j∈S.
Here, ∣ S ∣ |S| ∣S∣ denotes the number of elements in the set S S S. For example, if S = { 3 , 4 , 7 } S=\lbrace 3,4,7\rbrace S={3,4,7}, then ∣ S ∣ = 3 |S|=3 ∣S∣=3.
思路:如果直接模拟,显然会T;直接模拟,在每次需要更新时,都要遍历一次A,时间复杂度为O(QN),我们可以试着变成O(Q+N)(log是不可行的因为始终要遍历一次Q和N);顺着这个想,发现可以记录每次需要更新的值的前缀和,因为每个A的更新都是连续的一段|s|的和,大致是这么个想法,接下来看代码吧
AC Code
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define int long long
#define pii pair<int, int>
#define pb push_back
#define gcd __gcd
#define lcm(a, b) (a * b) / gcd(a, b)
#define all(a) a.begin(), a.end()
#define mem(a, x) memset(a, x, sizeof(a))
#define f(i, s, e) for (int i = s; i <= e; i++)
#define ff(i, s, e) for (int i = s; i >= e; i--)
#define setbits(x) __builtin_popcount(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 100000007
#define maxn (ll)(2e5 + 5000)
#define INF LONG_LONG_MAX
using namespace std;
// set<int>::iterator it;
int n, q;
map<int, int> mp;
map<int, vector<int>> mp1;
set<int> st;
int prefix[maxn] = {0};
int q1[maxn];
signed main(void)
{
ios::sync_with_stdio(false), cin.tie(nullptr);
cin >> n >> q;
f(i, 1, q)
{
cin >> q1[i];
mp[q1[i]]++;
}
f(i, 1, q)
{
if (mp[q1[i]] == 1)
{
mp1[q1[i]].pb(i);
mp1[q1[i]].pb(q);
}
else if (mp[q1[i]] > 1)
{
if (mp1[q1[i]].size() % 2 != 0 && mp1[q1[i]].size() != 0)
{
mp1[q1[i]].pb(i - 1); // 右端点减一
}
else
{
mp1[q1[i]].pb(i);
}
}
}
f(i, 1, q)
{
if (mp1[q1[i]].size() % 2 != 0)
{
mp1[q1[i]].pb(q);
}
}
f(i, 1, q)
{
if (st.find(q1[i]) != st.end())
{
st.erase(q1[i]);
}
else
{
st.insert(q1[i]);
}
prefix[i] = st.size() + prefix[i - 1];
}
f(i, 1, n)
{
int sum = 0;
for (int j = 0; j < mp1[i].size(); j += 2)
{
sum += prefix[mp1[i][j + 1]] - prefix[mp1[i][j] - 1];
}
cout << sum << " ";
}
return 0;
}