Codeforces Round #697 (Div. 3) A~E题解
写在前边
状态及其不佳,很累很困,还好了
A. Odd Divisor
链接:A题链接
题目大意:
判断一个数是否有奇数因子。
思路:
一开始挺懵的,然后自己推了一下,发现只有的幂才不会有奇数因子,因此本题只需要判断是否是二的幂即可,利用位运算,。
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
#define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define PLL pair<long, long>
typedef long long ll;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI;
void solve() {
ll n;
cin >> n;
if (n & (n - 1)) {
puts("YES");
} else {
puts("NO");
}
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
B. New Year's Number
链接:B题链接
题目大意:
判断一个数是否可以由个和个组成。
思路:
既然由与组成,那么只需要判断对取模后得到的数是否可以由个组成即可,即。
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
#define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define PLL pair<long, long>
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI;
void solve() {
int n;
cin >> n;
if ((n % 2020) <= (n / 2020)) {
puts("YES");
} else {
puts("NO");
}
}
int main()
{
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
C. Ball in Berland
链接:C题链接
题目大意:
有个男生,个女生,一个男生一个女生组合起来跳舞,并且要求选出两对,给出对男女组合,求有多少种组合方式。
思路:
可以两重直接循环枚举,枚举到的时候,二重循环中判断不包含顶点的都可以加到答案中,但是直接枚举肯定超时,因此优化一下我们就可以预处理出的出度,的出度,那么一重循环枚举到我们可以直接找到不符合条件的有几个,即,一共有对那么符合条件的就有,因此就从优化到了,最后再将答案除以即可,因为每个顶点都算了两遍。
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
#define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define PLL pair<long, long>
typedef long long ll;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI;
const int N = 2e5 + 10;
int edga[N], edgb[N];
ll out_degreeA[N], out_degreeB[N];
void solve() {
int a, b, k;
cin >> a >> b >> k;
for (int i = 1; i <= k; i++) {
cin >> edga[i];
out_degreeA[edga[i]]++;
}
for (int i = 1; i <= k; i++) {
cin >> edgb[i];
out_degreeB[edgb[i]]++;
}
ll res = 0;
for (int i = 1; i <= k; i++) {
res += k - (out_degreeA[edga[i]] + out_degreeB[edgb[i]] - 1);
}
cout << res / 2 << endl;
memset(out_degreeA, 0, sizeof out_degreeA);
memset(out_degreeB, 0, sizeof out_degreeB);
}
int main()
{
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
D. Cleaning the Phone
链接:D题链接
题目大意:
清理手机问题,每个占用内存,它相应的实用度有 ,那么总的实用度就是的和,现在要清理内存,要求清理出至少的内存,损失最少实用度,求出损失的最少实用度,若没有则输出。
思路:
很明显的贪心策略就是优先删除实用度为并且占内存比较大的,再者就是删除实用度为占内存比较大的,而但是毕竟选择的不是一两个,因此为了解决这个问题可以将实用度为1或者2的分成两个数组,然后求其前缀和,最好的方案就是再能达到需求的情况下尽可能少的卸载实用度为的,因此从大到小枚举实用度为的,然后剩下就是,那么另一边就用二分来选择,维护一个始终为最小方案。
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <climits>
using namespace std;
#define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define PLL pair<long, long>
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI;
void solve() {
int n, m;
cin >> n >> m;
vector<LL> v(n + 1), preSum1, preSum2;
preSum1.push_back(0), preSum2.push_back(0);
for (int i = 1; i <= n; i++) {
cin >> v[i];
}
for (int i = 1; i <= n; i++) {
int c;
cin >> c;
if (c == 1) {
preSum1.push_back(v[i]);
}
else if (c == 2) {
preSum2.push_back(v[i]);
}
}
sort(preSum2.rbegin(), preSum2.rend() - 1), sort(preSum1.rbegin(), preSum1.rend() - 1);
for (int i = 1; i < preSum1.size(); i++) {
preSum1[i] += preSum1[i - 1];
}
for (int i = 1; i < preSum2.size(); i++) {
preSum2[i] += preSum2[i - 1];
}
LL res = INT_MAX;
for (int i = preSum2.size() - 1; i >= 0; i--) { //从大到小枚举
LL aim = m - preSum2[i];
LL l = 0, r = preSum1.size();
while (l < r) {
LL mid = l + r >> 1;
if (preSum1[mid] >= aim) {
res = min(res, i * 2 + mid);
r = mid;
}
else {
l = mid + 1;
}
}
}
cout << (res == INT_MAX ? -1 : res) << endl;
}
int main()
{
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
E. Advertising Agency
链接:E题链接
题目大意:
一个人从个博主里选个来带货,那么她当然是优先选择粉丝多的博主了,问有多少种选择方案。
思路:
设为拥有个粉丝的博主,那么我们肯定优先从粉丝多的博主选了,例如要求选个,已经选了个,还剩下个需要选,那么现在,因此只需要从cnt[x]里选k-m个即可,即求组合数,因为数据范围很小,可以直接用递推式即可:
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
#define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define PLL pair<long, long>
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI;
const int mod = 1e9 + 7;
const int N = 1010;
int c[N][N];
void init() {
for (int i = 0; i < N; i++) {
for (int j = 0; j <= i; j++) {
if (!j) {
c[i][j] = 1;
} else {
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
}
}
}
}
void solve() {
int k, n;
cin >> n >> k;
vector<int> cnt(n + 1);
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
cnt[x]++;
}
init();
for (int i = n; i >= 1; i--) {
if (cnt[i] >= k) {
cout << c[cnt[i]][k] << endl;
return;
} else {
k -= cnt[i];
}
}
}
int main()
{
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端