文章目录
CodeForces - 483A Counterexample 【暴力】
传送门:CodeForces483A Counterexample
题目大意
给定区间 [l,r],问是否能在区间中找到3个不同的数a,b,c满足a与b互质,b与c互质,且a与c不互质。找到则输出3个数,否则输出-1。
解题思路
根据题意 r - l ≤ 50,暴力枚举即可。
AC代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
int main()
{
ll l,r;
scanf("%lld%lld",&l,&r);
for(ll i = l;i < r; i++)
{
for(ll j = i + 1; j < r; j++)
{
for(ll k = j + 1; k <= r; k++)
{
if(__gcd(i,j) == 1 && __gcd(j,k) == 1 && __gcd(i,k) != 1)
{
printf("%lld %lld %lld\n",i,j,k);
return 0;
}
}
}
}
printf("-1\n");
return 0;
}
CodeForces - 485B Valuable Resources 【贪心】
传送门:CodeForces485B Valuable Resources
题目大意
给定n个坐标,问:能将n个坐标包含在内的最小的正方形面积(坐标可以在正方形边上)。
解题思路
确定最长的一条边即可。
AC代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
ll x,y;
ll minx = INF,miny = INF,maxx = -INF,maxy = -INF;
int main()
{
int n;
scanf("%d",&n);
while (n--)
{
scanf("%lld%lld",&x,&y);
minx = min(x,minx);
miny = min(y,miny);
maxx = max(x,maxx);
maxy = max(y,maxy);
}
ll ans = max(maxx-minx,maxy-miny);
printf("%lld\n",ans * ans);
return 0;
}
CodeForces - 483B Friends and Presents 【二分 + 容斥】
传送门:CodeForces - 483B Friends and Presents
题目大意
你有两个朋友,现在要向每个朋友分配一些正整数。你希望将cnt1个数字分给第一个朋友,将cnt2个数字分给第二个朋友。所有分配的数字都是不同的,这也意味着不应该同时向两个朋友分配同一个数字。另外,第一个朋友不喜欢能被质数x除尽的数,第二个朋友不喜欢能被质数y除尽的数,当然,你不会给你朋友他们不喜欢的数。
找到最小的数字v,以便可以使用集合1,2,…,v中的数字来分配。
解题思路
AC代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 1e5;
const int INF = 0x3f3f3f3f;
LL cnt1, cnt2, x, y;
bool check(LL v)
{
if (v - v / x < cnt1 || v - v / y < cnt2)
{
return false;
}
LL lcm = x / __gcd(x, y) * y;
// if (v - v / x / y < cnt1 + cnt2)
if (v - v / lcm < cnt1 + cnt2)
{
return false;
}
return true;
}
int main()
{
scanf("%lld%lld%lld%lld",&cnt1, &cnt2, &x, &y);
LL l = 0, r = 1e18, mid;
// 二分枚举 v
while (l <= r)
{
mid = (l + r) >> 1;
if (check(mid))
{
r = mid - 1;
}
else
{
l = mid + 1;
}
}
printf("%lld\n",r + 1);
return 0;
}
CodeForces - 485C Bits 【位或】
题目大意
n个询问,对于每个询问的[l,r]区间,找出区间内的一个x,要求x的二进制位上的1要尽量多,当有多个x二进制位上1的个数相同时,要最小的x。
解题思路
在 l <= r 的前提下将 l 从右到左的每一位尽量变为1。因为是从右到左变的,所以最终找到的数必定是最小的。
AC代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 1e5;
const int INF = 0x3f3f3f3f;
int main()
{
int n;
scanf("%d", &n);
while (n--)
{
LL l, r, t = 1;
scanf("%lld%lld", &l, &r);
while ((l | t) <= r)
{
l = l | t;
t <<= 1;
}
printf("%lld\n",l);
}
return 0;
}
CodeForces485D Maximum Value【思维 + 二分 / 预处理 】
传送门:CodeForces485D Maximum Value
题目大意
给定一个n个数的整数序列,问: 在1 ≤ i, j ≤ n 且 ai ≥ aj 的情况下,ai % aj 的最大值为多少。
解题思路
对于一个数x,它的最大余数是x - 1。
对于ai ≥ aj,找出小于 k * aj 的最大的 ai(k >= 2),此时ai % aj 为一个可能的答案,对所有答案取最大即可。
AC代码1【二分】
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;
const int INF = 0x3f3f3f3f;
int a[MAXN];
int main()
{
int n, num;
scanf("%d",&n);
for (int i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
sort(a, a + n);
// 去重
n = unique(a, a + n) - a;
int ans = 0;
for (int i = 0; i < n; i++)
{
int k = a[n - 1] / a[i] +1;
for (int j = 2; j <= k; j++)
{
int pos = lower_bound(a, a + n, a[i] * j) - a - 1;
ans = max(ans, a[pos] % a[i]);
// 优化,当 ans大于取余 a[i]的最大余数,就没必要对 a[i]处理了
if (ans >= a[i] - 1)
{
break;
}
}
}
printf("%d\n",ans);
return 0;
}
AC代码2【预处理】
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e6 + 5;
const int INF = 0x3f3f3f3f;
// 数组要开 2e6,因为下标对应数据,后续要处理 a[i] * k(k >= 2)
int a[MAXN];
int main()
{
int n, num;
scanf("%d",&n);
for (int i = 0; i < n; i++)
{
scanf("%d",&num);
a[num] = num;
}
// 预处理
for (int i = 0; i < MAXN; i++)
{
if (a[i] != i)
{
a[i] = a[i - 1];
}
}
int ans = 0;
for (int i = 2; i < MAXN; i++)
{
if (a[i] == i)
{
for (int j = 2; i * j < MAXN; j++)
{
// a[i * j - 1] 是比 i 的 j 倍小的最大的数
ans = max(ans,a[i * j - 1] % i);
// 优化,当 ans大于取余 i 的最大余数,就没必要对 i 处理了
if (ans >= i - 1)
{
break;
}
}
}
}
printf("%d\n",ans);
return 0;
}