Codeforces 912A/B
2024-09-07 09:55:55
A. Tricky Alchemy
传送门:http://codeforces.com/contest/912/problem/A
参考程序如下:
#include <stdio.h>
#include <stdint.h> int main(void)
{
int64_t a, b, x, y, z;
scanf("%I64d%I64d%I64d%I64d%I64d", &a, &b, &x, &y, &z);
int64_t addA = 2LL * x + y - a > ? 2LL * x + y - a: ;
int64_t addB = 3LL * z + y - b > ? 3LL * z + y - b: ;
printf("%I64d\n", addA + addB);
return ;
}
B. New Year's Eve
给定两个整数n、k:在1~n中选择至多k个整数,使得其异或和最大。求解这个最大值。
显然,当k=1时,只选择一个整数n,于是答案为n;
当k>1时,可以考虑异或和可能达到的最大值:将n的所有可改变的二进制位(即最高的‘1’位以下的二进制位)均变为‘1’,这个值就是异或和可能达到的最大值。
设S=2b-1,则对于任意自然数a,若a<S,则一定有aÅ(S-a)=S。由于S的二进制位,0~b-1位均为‘1’,于是根据位运算的基本规则,等式显然成立。
于是,当k>1时,设n的最高‘1’位为b-1位,S=2b-1,则可选择两个整数:n和S-n(注意,这里n≥2b-1,于是S-n<2b-1≤n);特别地,当n=S时,只选择一个整数n。于是答案为S。
参考程序如下:
#include <stdio.h>
#include <stdint.h> int main(void)
{
int64_t n, k;
scanf("%I64d%I64d", &n, &k);
if (k == ) {
printf("%I64d\n", n);
return ;
}
int bit = ;
for (; n >> bit; bit++);
printf("%I64d\n", (1LL << bit) - );
return ;
}
最新文章
- SpringSecurity操作指南-基于Spring、SpringMVC和MyBatis自定义SpringSecurity权限认证规则
- JS中this的指向问题
- Asp.net GridView控件使用纪要
- windows mysql 自动备份的几种方法
- HDU5052 Yaoge’s maximum profit(LCT)
- 机器人学 —— 机器人感知(Gaussian Model)
- NDK(21)JNI的5大正确性缺陷及优化技巧(注意是正确性缺陷)
- PHP.3-DIV+CSS标准网页布局准备工作(上)
- php前端控制器2
- docker 基础命令
- Codeforces 895C - Square Subsets 状压DP
- 深入剖析Tomcat会话机制
- 使用mybatis操作AS400数据库
- SQL Server通过BCP进行大批量数据导入导出
- np.tile语法
- ArgumentException: 已添加了具有相同键的项。
- 20165318 预备作业3 Linux安装及学习
- Android 的事件传递机制,详解
- php PDO操作类
- C语言 结构体作为参数和返回值使用
热门文章
- Android studio第一次使用配置(三)gradle项目构建
- Ext.tree.Panel实现单选,多选
- oc35--自定义构造方法
- C# SuperWebSocket服务端、客户端学习(三)
- hdoj--1518--Square(dfs)
- C++ 中的友元(friend)
- Error-Java-IJ:Imported project refers to unknown jdks JavaSE-1.7
- 操作系统-虚拟机-百科:VM
- im4java+GraphicsMagick
- BZOJ 1037 生日聚会 DP