Codeforces Global Round 17
A. Anti Light's Cell Guessing
analysis:
Just consider the case of 0.
code:
#include <bits/stdc++.h> using namespace std; //#pragma GCC optimize(2) #define close(); ios::sync_with_stdio(false); #define endl '\n' #define rep(i, l, r) for(int i = l; i <= r; i++) #define dwn(i, r, l) for(int i = r; i >= l; i--) typedef long long LL; const int N = 3e5+100; void solve() { int a, b; cin >> a >> b; if(a > b) swap(a, b); if(a == 1 && b == 1) cout << 0 << endl; else if(a == 1) cout << 1 << endl; else cout << 2 << endl; } int main() { int T; cin >> T; while(T--) solve(); // system("pause"); }
B. Kalindrome Array
analysis:
Remember that the string is s and the length is m
The discussion is divided into the following situations:
- It is a palindrome string and outputs yes
- If it is not a palindrome string, there must be a pair of \ (s_i!=s_j, i = m+1-j \)
- Consider deleting all the characters of the \ (s_i \) class to see if it is a palindrome string again
- Consider deleting all characters of the \ (s_j \) class to see if it is a palindrome string again
Why delete all its class characters? It seems a little different from the title stem. In fact, even if it does not delete its class characters, it is the matching itself. It is equivalent to judging whether it is a palindrome string in the case of complete deletion
code:
#include <bits/stdc++.h> using namespace std; //#pragma GCC optimize(2) #define close(); ios::sync_with_stdio(false); #define endl '\n' #define rep(i, l, r) for(int i = l; i <= r; i++) #define dwn(i, r, l) for(int i = r; i >= l; i--) typedef long long LL; const int N = 3e5+100; int a[N]; bool check(int n, int x) { int l = 1, r = n; while(l < r) { while(l < r && a[l] == a[x]) l++; if(l >= r) break; while(l < r && a[r] == a[x]) r--; if(l >= r) break; if(a[l] == a[r]) l++, r--; else if(a[l] != a[r]) return 0; } return 1; } void solve() { int n; cin >> n; rep(i, 1, n) cin >> a[i]; int l = 1, r = n; int u, v; u = v = 0; while(l < r) { if(a[l] == a[r]) l++, r--; else { u = l; v = r; break; } } bool f = 0; if(u == v) f = 1; else { f |= check(n, u); f |= check(n, v); } if(f) cout << "YES\n"; else cout << "NO\n"; } int main() { close(); int T; cin >> T; while(T--) solve(); // system("pause"); }
C. Keshi Is Throwing a Party
analysis:
At first, I thought about it for a while. I regretted my greed and found that I couldn't do it
Then I found that it was a useful algorithm
When we determine the number of people we want to check, we directly look for it greedily. If we don't understand, read the code
Time complexity \ (O(n\log n) \)
code:
#include <bits/stdc++.h> using namespace std; //#pragma GCC optimize(2) #define close(); ios::sync_with_stdio(false); #define endl '\n' #define rep(i, l, r) for(int i = l; i <= r; i++) #define dwn(i, r, l) for(int i = r; i >= l; i--) typedef long long LL; const int N = 3e5+100; int a[N], b[N]; int n; bool check(int x) { int cnt = 0; rep(i, 1, n) { if(a[i] >= x-cnt-1 && b[i] >= cnt) { cnt++; } if(cnt == x) return 1; } return 0; } void solve() { cin >> n; rep(i, 1, n) cin >> a[i] >> b[i]; int l = 1, r = n; while(l < r) { if(l == r-1) { if(check(r)) l = r; else r = l; break; } int mid = l+r>>1; if(check(mid)) l = mid; else r = mid-1; } cout << l << endl; } int main() { close(); int T; cin >> T; while(T--) solve(); // system("pause"); }
D. Not Quite Lee
analysis:
Consider an array \ (c \) with \ (k \) elements, and how to judge whether it is good.
We find that for each \ (c_i\in c \), the sum of consecutive sequences of integers is \ (\ frac{c_i(c_i-1)}{2}+x_ic_i \), where \ (x_i \) is any integer.
Therefore, we can convert whether an array c is good or not into this equation:
By observing this formula, we find that we are very familiar with it. We might as well define \ (t \) as the following formula:
According to peishu theorem, \ (GCD (c_1, c_2,..., c_k) | t \)
Record \ (s = \ sum_{I = 1} ^ k \ frac {c_i (c_i-1)} {2}, g = GCD (c_1, c_2,..., c_k) \)
So the problem of determining whether an array c is good is transformed into: whether g can divide s
Consider when there are odd numbers for array c:
- If there are odd and even numbers in array c, then \ (g=1 \) has a solution
- If there are only odd numbers in array c, let the midpoint of all continuous sequences be placed at zero, s = 0, there is a solution
So next, we only need to consider the case that there are only even numbers in array c
It is observed that if the odd number y is divided by the even number x, then \ (y|\frac{x}{2} \); If even y is divided by even x, then \ (\ frac{y}{2}|\frac{x}{2} \)
Considering that enumeration g is time infeasible, we try to establish a 1-1 mapping to reduce the divisor we need to enumerate
For \ (g|s \), obviously s is an even number and G is also an even number (the odd number has been calculated), then \ (\ frac {g} {2^l} \ frac {s} {2^l} \). Where $2^l | g \space \ $and \ (\ space2^{l+1} \not | g \), that is to say \ (2^l \) is the largest integral power factor of 2 of G
Note: odd numbers are handled in advance, so \ (l\le 1 \)
So we might as well map g to \ (2^l \), which is obviously a 1-1 mapping. The specific operations are as follows:
For all elements in array c that can be divided by \ (2^l \) but cannot be divided by $2^{l+1} $, put them into set A
For all elements in array c that can be divided by \ (2^{l+1} \), put them into set B
Obviously, for any combination containing an even number of elements in set A, the mapping of g is \ (2^l \)
Why not include an odd number of elements in set A? If you look at A single element, you might as well write it as \ (e = k2^l \), where k is an odd number. Then \ (\ frac{e(e-1)}{2} = k2^{l-1}(k2^l-1) \), this is A number with the largest integral power factor of 2 as \ (2^{l-1} \). Obviously, the integral power factor of the largest 2 of s is also \ (2^{l-1} \)
But if it is an even number of set A elements, this problem can be solved
Let the number of elements in array c that can be divided by \ (2^l \) but cannot be divided by $2^{l+1} $be \ (a \); The number of all elements in array c that can be divided by \ (2^{l+1} \) is \ (b \)
Then the answer to \ (2^l \) is \ (2^{a-1}\cdot 2^b \)
Just enumerate \ (l \) statistical answers
The total time complexity is \ (O(n\log(1e9)) \)
code:
#include <bits/stdc++.h> using namespace std; //#pragma GCC optimize(2) #define close(); ios::sync_with_stdio(false); #define endl '\n' #define rep(i, l, r) for(int i = l; i <= r; i++) #define dwn(i, r, l) for(int i = r; i >= l; i--) typedef long long LL; const int N = 3e5+100; const LL p = 1e9+7; LL qpow(LL a, LL b) { LL rev = 1; while(b){ if(b&1) rev = rev * a % p; a = a * a % p; b>>=1; } return rev; } LL a[N]; int main() { // close(); int n; cin >> n; rep(i, 1, n) cin >> a[i]; LL ans = 0; LL even = 0; rep(i, 1, n) { if(a[i] & 1) ; else even++; } ans = (qpow(2, n) - qpow(2,even)) % p; rep(i, 1, 31) { LL x = 1ll<<i; LL o, e; o = e = 0; rep(j, 1, n) { if(a[j] % x == 0 && a[j] % (x<<1) != 0) o++; else if(a[j] % (x<<1) == 0) e++; } if(o > 0) (ans += ((qpow(2, o-1)-1+p)%p) * ((qpow(2, e))%p) ) %= p; } cout << ans << endl; // system("pause"); }
E. AmShZ and G.O.A.T.
Catch up with your homework and make it up when you are free