Codeforces Round #613 (Div. 2)
【A.Mezo Playing Zoma】
【题目大意】
给一个由L和R组成的字符串,L表示左移,R表示右移,最初在0位置,每一次你可以选择移动或者不移动,问你最后可能有有多少种不同的位置
【解题思路】
考虑极端情况所有的L都不执行以及所有的R都不执行,则会到达最右边和最左边,那么中间的所有位置就都有可能,所以答案是(numsof(L) + numsof(R) + 1) = strlen(s) + 1
【AC代码】
#include <bits/stdc++.h>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define Rep(i, n) for(register int i = 1; i <= (n); ++i)
using namespace std;
typedef long long ll;
inline int read()
{
register int X = 0, w = 0; register char ch = 0;
while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline void write(register ll x)
{
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
int main() {
int l;
l = read();
string s;
cin >> s;
write(l + 1);
return 0;
}
【B.Just Eat It!】
【题目大意】
给你n个数字,A会选择[L, R] ([L, R] != [1, n])区间内的数字,B会选择[1, n],问不论A如何选择你B的数字和是不是一定严格大于A的所选数字和
【解题思路】
对于B 显然sumB = a[1] + a[2] + … + a[n]
对于A 处理最大区间和
需要注意,A不能选择[1, n]
【AC代码】
#include <bits/stdc++.h>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define Rep(i, n) for(register int i = 1; i <= (n); ++i)
using namespace std;
typedef long long ll;
inline int read()
{
register int X = 0, w = 0; register char ch = 0;
while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline void write(register ll x)
{
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
int main() {
int t;
t = read();
while (t--) {
int n;
n = read();
ll sum = 0; //sumB
ll ans = 0; //sumA
ll temp = 0; //记录区间和
int l = 1;
int ansl = 1, ansr = 1; //记录A的选择区间
for (int i = 1; i <= n; ++i) {
int x;
x = read();
sum += x;
temp += x;
if (temp <= 0) { //一旦temp<=0,那么我们直接将区间左端设置为i+1
temp = 0;
l = i + 1;
continue;
}
if (ans < temp) { //更新
ans = temp;
ansl = l;
ansr = i;
}
}
if (sum > ans) puts("yes");
else if (sum < ans) puts("no");
else {
if (ansl == 1 && ansr == n) {
puts("yes");
}
else puts("no");
}
}
return 0;
}
【C.Fadi and LCM】
【题目大意】
给定一个x,找到a,b使得
1.max(a, b)最小
2.lcm(a, b) = x
【解题思路】
对于条件一,要使a,b最小,那么显然要使a,b尽可能接近,那么考虑对x取根号,从后往前遍历,找到一个数整除x
然后考虑条件二,lcm(a, b) = x,即a * b / gcd(a, b) = x,要使a, b尽量小,那么gcd(a, b)显然等于1,否则一定能够找到更小的a或者b满足条件
例如:24 = lcm(6, 8) = lcm(3, 8)
【AC代码】
#include <bits/stdc++.h>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define Rep(i, n) for(register int i = 1; i <= (n); ++i)
using namespace std;
typedef long long ll;
inline int read()
{
register int X = 0, w = 0; register char ch = 0;
while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline void write(register ll x)
{
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
inline ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
ll x;
scanf("%lld", &x);
if (x == 1) {
printf("1 1\n");
return 0;
}
ll k = sqrt(x);
if (k * k == x) --k;
for (ll i = k; i >= 1; --i) {
if (x % i == 0) {
if (gcd(x / i, i) == 1) {
printf("%lld %lld\n", i, x / i);
return 0;
}
}
}
return 0;
}
【D.Dr. Evil Underscores】
【题目大意】
给定n个数,找到最小的数x使其与n个数的异或值的最大值最小
【解题思路】
考虑答案在二进制下的posi,如果ai在posi只有1或只有0,那么显然ans-posi = 0或1
如果ai-posi既有1又有0,那么递归找到该位取1或0的最小值
本题参考:https://www.cnblogs.com/CADCADCAD/p/12179509.html
【AC代码】
#include <bits/stdc++.h>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define Rep(i, n) for(register int i = 1; i <= (n); ++i)
using namespace std;
typedef long long ll;
inline int read()
{
register int X = 0, w = 0; register char ch = 0;
while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline void write(register ll x)
{
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
vector<ll> a;
inline ll dfs(register int pos, register vector<ll>& v) {
if (pos < 0) return 0;
register vector<ll> v0, v1;
for (auto& it : v) {
if ((it >> pos) & 1) v1.push_back(it);
else v0.push_back(it);
}
if (v0.size() == 0) return dfs(pos - 1, v1); //只有1
if (v1.size() == 0) return dfs(pos - 1, v0); //只有0
ll a = dfs(pos - 1, v0);
ll b = dfs(pos - 1, v1);
return min(a, b) +(1 << pos); //否则找到取1或0的最小值
}
int main() {
register int n;
n = read();
Rep(i, n) {
register ll x;
scanf("%lld", &x);
a.push_back(x);
}
write(dfs(30, a));
return 0;
}