[ABC282E] Choose Two and Eat One
Problem Statement
A box contains $N$ balls, each with an integer between $1$ and $M-1$ written on it.
For $i = 1, 2, \ldots, N$, the integer written on the $i$-th ball is $A_i$.
While the box has two or more balls remaining, Takahashi will repeat the following.
- First, choose two balls arbitrarily.
- Then, get a score equal to the remainder when $x^y + y^x$ is divided by $M$, where $x$ and $y$ are the integers written on the two balls.
- Finally, choose one of the two balls arbitrarily, eat it, and return the other to the box.
Print the maximum possible total score Takahashi will get.
Constraints
- $2 \leq N \leq 500$
- $2 \leq M \leq 10^9$
- $1 \leq A_i \leq M-1$
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
$N$ $M$
$A_1$ $A_2$ $\ldots$ $A_N$
Output
Print the answer.
Sample Input 1
4 10
4 2 3 2
Sample Output 1
20
Consider the following scenario. Below, $X \bmod Y$ denotes the remainder when a non-negative integer $X$ is divided by a positive integer $Y$.
- Take the first and third balls from the box to score $(4^3 + 3^4) \bmod 10 = 5$ points. Then, eat the first ball and return the third to the box. Now, the box has the second, third, and fourth balls.
- Take the third and fourth balls from the box to score $(3^2 + 2^3) \bmod 10 = 7$ points. Then, eat the third ball and return the fourth to the box. Now, the box has the second and fourth balls.
- Take the second and fourth balls from the box to score $(2^2 + 2^2) \bmod 10 = 8$ points. Then, eat the second ball and return the fourth to the box. Now, the box has just the fourth ball.
Here, Takahashi scores a total of $5 + 7 + 8 = 20$ points, which is the maximum possible value.
Sample Input 2
20 100
29 31 68 20 83 66 23 84 69 96 41 61 83 37 52 71 18 55 40 8
Sample Output 2
1733
做的时候硬是没看出这题,写个题解纪念一下。
如果我们把同选两个数 \(x,y\) 看作连一条边,那么最后会连出一棵树。此时从叶子节点选起,按照拓扑的方式往上走,选完后就把叶子节点删去,这就是一种按顺序取完这棵树的一种构造。那么这棵树的代价就是他的边权和。
反观这道题,其实就是一个最大生成树。暴力建边,跑kruskal就行了。
#include<bits/stdc++.h>
using namespace std;
const int N=505;
int n,m,a[N],k,fa[N];
long long ans;
int find(int x)
{
if(fa[x]==x)
return x;
return fa[x]=find(fa[x]);
}
int pow(int x,int y)
{
if(!y)
return 1;
int k=pow(x,y>>1);
if(y&1)
return 1LL*k*k%m*x%m;
return 1LL*k*k%m;
}
struct edge{
int u,v,w;
bool operator<(const edge&e)const{
return w>e.w;
}
}e[N*N];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",a+i),fa[i]=i;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
e[++k]=(edge){i,j,(pow(a[i],a[j])+pow(a[j],a[i]))%m};
sort(e+1,e+k+1);
for(int i=1;i<=k;i++)
{
// printf("%d %d %d\n",e[i].u,e[i].v,e[i].w);
if(find(e[i].u)!=find(e[i].v))
fa[find(e[i].u)]=find(e[i].v),ans+=e[i].w;
}
printf("%lld",ans);
}
[ABC282E] Choose Two and Eat One的更多相关文章
- HHKB Programming Contest 2022 Winter(AtCoder Beginner Contest 282)
前言 好久没有打 AtCoder 了.有点手生.只拿到了 \(\operatorname{rk}1510\),应该上不了多少分. 只切了 \(\texttt{A,B,C,D}\) 四题. A - Ge ...
- Eat the Trees hdu 1693
Problem DescriptionMost of us know that in the game called DotA(Defense of the Ancient), Pudge is a ...
- How the Microsoft Bot Framework Changed Where My Friends and I Eat: Part 1
Bots are everywhere nowadays, and we interact with them all of the time. From interactions on our ph ...
- 【HDU】1693:Eat the Trees【插头DP】
Eat the Trees Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 1693 Eat the Trees(插头DP,入门题)
Problem Description Most of us know that in the game called DotA(Defense of the Ancient), Pudge is a ...
- HDU1693 Eat the Trees —— 插头DP
题目链接:https://vjudge.net/problem/HDU-1693 Eat the Trees Time Limit: 4000/2000 MS (Java/Others) Mem ...
- Mybatis的choose when otherwise
<select id="getCount" resultType="int"> select count(1) from <choose> ...
- mybatis:choose when otherwise标签
choose标签是按顺序判断其内部when标签中的test条件是否成立,如果有一个成立,则 choose 结束. 当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的 ...
- 理解 OpenStack + Ceph (9): Ceph 的size/min_size/choose/chooseleaf/scrubbing/repair 等概念
本系列文章会深入研究 Ceph 以及 Ceph 和 OpenStack 的集成: (1)安装和部署 (2)Ceph RBD 接口和工具 (3)Ceph 物理和逻辑结构 (4)Ceph 的基础数据结构 ...
- UVA - 10375 Choose and divide[唯一分解定理]
UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
随机推荐
- Linux下后台运行Java程序
1.背景描述 用Java编写了一个程序(可执行的jar),需要在Linux中启动并持续运行 1.1.直接执行程序 直接执行程序后,在程序执行期间,无法在当前会话中再执行其他操作 1.2.直接执行程序后 ...
- svg动画 - 波浪动画
波浪 <path d="M 96.1271 806.2501 C 96.1271 806.2501 241.441 755.7685 384.5859 755.7685 C 529.8 ...
- AI绘画StableDiffusion:云端在线版免费使用笔记分享-Kaggle版
玩AI绘画(SD),自己电脑配置不够?今天给大家介绍一下如何baipiao在线版AI绘画StableDiffusion. Kaggle 是世界上最大的数据科学社区,拥有强大的工具和资源,可帮助您实现数 ...
- Mybatis插件功能
1 插件的作用 在Mybatis执行SQL的生命周期中,会使用插件进行埋点,主要包括Executor.StatementHandler.ParameterHandler和ResultSetHandle ...
- 《SQLi-Labs》02. Less 6~10
@ 目录 索引 Less-6 题解 原理 Less-7 题解 Less-8 题解 Less-9 题解 原理 Less-10 题解 sqli.开启新坑. 索引 Less-6:布尔盲注,字符型[" ...
- xlwt写入excel时候的合并单元格
简单版 import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet('My sheet') # 合并第0行的第0列到第3 ...
- DevSecOps之应用安全测试工具及选型
上篇文章,有同学私信想了解有哪些DevSecOps工具,这里整理出来,供大家参考(PS: 非专业安全人士,仅从DevOps建设角度,给出自己见解) 软件中的漏洞和弱点很常见:84%的软件漏洞都是利用应 ...
- Confluence的Excel插件Elements Spreadsheet安装
背景 Confluence是现在广泛使用的团队协作文档系统.虽然自身带了一些表格编辑功能,但表格的整体功能较弱,比如不能通过Excel文件进行导入导出,表格在复制到Excel时格式会比较奇怪等等.对于 ...
- CVE-2018-8120 漏洞复现
CVE-2018-8120 漏洞复现 漏洞描述 win32k.sys中函数 SetImeInfoEx未对指针进行合法性检查,从而导致一个任意地址写. 漏洞分析 漏洞成因 int __stdcall S ...
- 一文弄懂TypeScript中的混合
1.前言 由于TypeScrip中的类不支持多继承,所以引入了混合(Mixin)的特性,可以间接实现继承的效果. 2.正文 // 声明一个汽车类Vehicle,它有drive方法 class Vehi ...