A. Shovels and Swords
思路
题意非常简单,就是得到最多的物品嘛,我们假定 a , b a, b a,b中 a a a是最小的一个,分两种情况。
如果 2 ∗ a < = b 2 * a <= b 2∗a<=b,那么我们只需要购买花费是 1 , 2 1, 2 1,2的东西即可,也就是最后能购买得到 a a a件物品。
否则的话,我们一定是先让数量更多的去减 2 2 2,用数量更少的去减 1 1 1,直到两个物品的数量相等,再通过 1 , 2 1, 2 1,2, 2 , 1 2, 1 2,1的顺序去交换执行,总结一下最后的答案就是 ( n + m ) / 3 (n + m) / 3 (n+m)/3。
代码
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-7;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
inline ll read() {
ll f = 1, x = 0;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f * x;
}
const int N = 2e5 + 10;
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int t = read();
while(t--) {
ll a = read(), b = read();
if(a > b) swap(a, b);
if(a * 2 <= b) printf("%lld\n", a);
else printf("%lld\n", (a + b) / 3);
}
return 0;
}
B.Shuffle
思路
就是一个区间有重合判断并集的问题,如果我们给定的区间 [ l , r ] [l, r] [l,r]在原本的区间外也就是 r < L ∣ ∣ l > R r < L || l > R r<L∣∣l>R,否则的话我们就更新 L , R L, R L,R的最大最小值
代码
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-7;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
inline ll read() {
ll f = 1, x = 0;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f * x;
}
const int N = 2e5 + 10;
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int t = read();
while(t--) {
int n = read(), x = read(), m = read();
int l = x, r = x;
// cout << l << " " << r << endl;
for(int i = 1; i <= m; i++) {
int a = read(), b = read();
if((a <= r && a >= l) || (b >= l && b <= r) || (l >= a && b >= r)) {//比赛时判断条件写的比较繁琐。
l = min(l, a);
r = max(r, b);
}
// cout << l << " " << r << endl;
}
printf("%d\n", r - l + 1);
}
return 0;
}
C.Palindromic Paths
思路
开两个数组, n u m 1 [ i ] num1[i] num1[i]记录的是步数为 i i i的时候的位置上的 1 1 1的个数, n u m 0 [ i ] num0[i] num0[i]记录的是步数为 i i i的时候的位置上 0 0 0的个数,因为整体的步数就是在 [ 1 , n + m − 1 ] [1, n + m - 1] [1,n+m−1]之间,所以我们可以通过对每一步全变为 0 0 0或者全变为 1 1 1中挑选一个最小值,作为我们的花费,然后累加花费就是答案。
代码
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-7;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
inline ll read() {
ll f = 1, x = 0;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f * x;
}
const int N = 2e5 + 10;
int a[40][40];
int num0[100], num1[100];
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int t = read();
while(t--) {
memset(num1, 0, sizeof num1);
memset(num0, 0, sizeof num0);
int n = read(), m = read();
// cout << n << " " << m << endl;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) {
a[i][j] = read();
if(a[i][j] == 1) num1[i + j]++;
else num0[i + j]++;
}
if(n == 2 && m == 2) {
puts("0");
// puts("");
continue;
}
int ans = 0;
int l = 2, r = n + m;
while(l < r) {
//好像这个if语句并没有什么用,不知道比赛的时候怎么想的。
if((num1[l] && num0[l]) || (num1[r] && num0[r]) || (num1[l] && num0[r]) || (num0[l] && num1[r]))
ans += min(num0[l]+ num0[r], num1[l] + num1[r]);
// cout << ans << "\n";
l++, r--;
}
printf("%d\n", ans);
// puts("");
}
return 0;
}
D.Two Divisors
思路
先引入一个定理 g c d ( a , b ) = 1 = g c d ( a + b , a ∗ b ) gcd(a, b) = 1 = gcd(a + b, a * b) gcd(a,b)=1=gcd(a+b,a∗b),所以这道题就简简单单就可以水过了,但是我的赛况却不是如此,,,,,
那我们来证明一下这个定理的正确性吧:
假设有KaTeX parse error: Undefined control sequence: \and at position 15: gcd(x, y) = 1 \̲a̲n̲d̲ ̲gcd(x, z) = 1,所以 g c d ( x , y ∗ z ) = 1 gcd(x, y * z) = 1 gcd(x,y∗z)=1
g c d ( a , b ) = 1 − > g c d ( a + b , b ) = 1 = g c d ( a , a + b ) − > g c d ( a + b , a ∗ b ) = 1 gcd(a, b) = 1 -> gcd(a + b, b) = 1 = gcd(a, a + b) - > gcd(a + b, a * b) = 1 gcd(a,b)=1−>gcd(a+b,b)=1=gcd(a,a+b)−>gcd(a+b,a∗b)=1
代码
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-7;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
inline ll read() {
ll f = 1, x = 0;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f * x;
}
const int N1 = 1e7 + 10, N2 = 5e5 + 10;
int prime[N1], cnt;
int ans1[N2], ans2[N2], n;
bool st[N1];
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
for(int i = 2; i < N1; i++) {
if(!st[i]) prime[++cnt] = i;
for(int j = 1; j <= cnt && i * prime[j] < N1; j++) {
st[i * prime[j]] = true;
if(i % prime[j] == 0) break;
}
}
n = read();
for(int i = 1; i <= n; i++) {
int temp = read();
ans1[i] = ans2[i] = -1;
for(int j = 1; prime[j] * prime[j] <= temp; j++) {
int x = 1;
if(temp % prime[j] == 0) {
while(temp % prime[j] == 0) {
temp /= prime[j];
x *= prime[j];
}
if(temp == 1) break;
else {
ans1[i] = x;
ans2[i] = temp;
break;
}
}
}
}
for(int i = 1; i <= n; i++)
printf("%d%c", ans1[i], i == n ? '\n' : ' ');
for(int i = 1; i <= n; i++)
printf("%d%c", ans2[i], i == n ? '\n' : ' ');
return 0;
}