Codeforces Round 901 (Div
C. Jellyfish and Green Apple
题解
显然\(n \% m =0\),答案一定为\(0\)
如果\(n > m\),我们显然可以将\(n / m\)的苹果分给每个人,然后再处理$n % m $
如果\(n < m\),我们一定会将所有苹果一直对半切直到\(n > m\),所以答案每次对半切一定会加\(n\)
直到\(n \%m = 0\)的时候结束
那么什么情况下无解呢?
我们考虑每次对半切会使得\(n :=n * 2\),也就是说如果一个质因子\(p,p\neq 2\)在\(m\)中存在,但是在\(n\)中不存在,那么\(n\)不管怎么翻倍,永远不会出现\(n \%m = 0\)
那么这个该如何判断呢?
我们可以通过\(\frac{m}{gcd(n,m)}=2^i\)来判断
int n, m;
int lowbit(int x) { return x & -x; }
void solve()
{
cin >> n >> m;
if (n % m == 0)
{
cout << 0 << endl;
return;
}
n = n % m;
int r = m / gcd(n, m);
if (r != lowbit(r))
{
cout << -1 << endl;
return;
}
int ans = 0;
while (true)
{
while (n * 2 < m)
{
ans += n;
n <<= 1;
}
ans += n;
n <<= 1;
if (n % m == 0)
break;
n = n % m;
}
cout << ans << endl;
}
D. Jellyfish and Mex
\(1 \leq n \leq 5000\)
题解:\(DP\)
我们考虑\(dp\)
显然对于\(a_i > mex\)部分我们可以不用管
如果当前数组\(a\)的\(mex\)为\(g\),我们想使得\(x,x < g\)成为\(mex\),那么代价为\(g \times (cnt[x] - 1) + x\)
我们定义状态为:\(dp[i]\):代表通过删除操作使得\(mex = i\)的最小代价
转移方程为:
\[dp[mex] = 0 \\
dp[i] = min \sum_{j = i + 1}^{mex}(dp[j] + j \times (cnt[i] - 1) + i)
\]倒序遍历\(i\)即可
- 显然答案为\(dp[0]\)
int n, a[N];
// dp[i] 代表通过删除操作使得 mex = i 的最小代价
void solve()
{
cin >> n;
map<int, int> mp;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
mp[a[i]]++;
}
int mex = -1;
for (int i = 0;; ++i)
if (!mp.count(i))
{
mex = i;
break;
}
vector<int> dp(mex + 10, INF);
dp[mex] = 0;
for (int i = mex - 1; i >= 0; --i)
for (int j = i + 1; j <= mex; ++j)
dp[i] = min(dp[i], dp[j] + j * (mp[i] - 1) + i);
cout << dp[0] << endl;
}
Codeforces Round 901 (Div的更多相关文章
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Codeforces Round #262 (Div. 2) 1004
Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
- Codeforces Round #268 (Div. 2) ABCD
CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
随机推荐
- [python][selenium] Web UI自动化8种页面元素定位方式
关联文章:Web UI自动化页面切换iframe框架 简单的加个前置知识: 第一:webdriver.Chrome()这句话,通过WebDriver的构造方法,拿到浏览器驱动的对象,然后通过这个对象, ...
- .NET 多版本兼容的精美 WinForm UI控件库
前言 有粉丝小伙伴在后台留言咨询有没有WinForm 控件库推荐,现在就给安排上. .NET 平台进行 Windows 应用程序开发的我们来说,找一个既美观又实用的 WinForm UI 控件库至关重 ...
- numpy argsort排序如何让其稳定排序
numpy.argsort(a, axis=-1, kind=None, order=None) Parameters: aarray_like Array to sort. axis int or ...
- tailwindcss 经验
树摇时扫描的文件范围 根据 tailwindcss.config.js 中 content 的配置,跟打包软件加载的模块无关.即未使用的模块中的 class 也会被包含进来.
- QT疑难杂症之QML程序中如何使用文件系统模型QFileSystemModel?
简介 本文介绍了 QML程序中如何使用树状控件TreeView展示QT文件系统模型QFileSystemModel中的数据,并给出了两种实现模式. 目录 QML程序中使用文件系统模型的代码 树状控件自 ...
- Serilog文档翻译系列(六) - 可用的接收器、增强器、格式化输出
01.提供的接收器 Serilog 使用接收器将日志事件以各种格式写入存储.许多接收器由更广泛的 Serilog 社区开发和支持:可以通过在 NuGet 上搜索 serilog 标签找到. 02.增强 ...
- Blazor与IdentityServer4的集成
本文合并整理自 CSDN博主「65号腕」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明. Blazor与IdentityServer4的集成(一) IdentityS ...
- 使用GRUB Multiboot2引导自制操作系统
使用GRUB Multiboot2引导自制操作系统 前言 之前花了一周时间,从头学习了传统 BIOS 的启动流程.惊讶于背后丰富的技术细节的同时,也感叹 x86 架构那厚重的历史包袱.毕竟,谁能想到, ...
- os.path.basename()和os.path.splitext()
1.os.path.splitext()是用来分离文件名与扩展名: 2.os.path.basename()他返回的是一个base name,我认为就是路径最后一个文件名. import os fna ...
- [Vue warn]: Error in v-on handler (Promise/async): "TypeError: Object(...) is not a function"
引用外部发文件时候,只是部分引用,所以原本是解构方式引用的,我忘记加{ }了

