[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 ...
随机推荐
- java多线程使用详解与案例,超详细
一.创建线程的方式 1.继承Thread类 让子类继承Thread线程类 子类必须重写Thread类的run方法 创建一个自己定义的线程对象 调用start()方法启动线程 //测试类 /** * 1 ...
- 如何调用API接口获取淘宝商品数据
淘宝商品数据的获取是一项非常重要的技术,它可以为淘宝卖家和买家提供有利的数据分析和扩展市场的机会.调用API接口是一种快速.方便.高效的方式获取淘宝商品数据. 以下是一些步骤来调用API接口来获取淘宝 ...
- Mysql优化篇-索引优化与查询优化
1.索引失败案列 如果查询时没有使用索引,查询语句就会扫描表中所有记录,在数据量大的情况下,查询会很慢. (1)全值匹配 (2)最佳左前缀法则 mysql可以为多个字段创建索引,一个索引可以包括16个 ...
- 《最新出炉》系列初窥篇-Python+Playwright自动化测试-16-处理模态对话框弹窗
1.简介 我们在日常工作中,会经常遇到弹出警告框的问题,弹框无法绕过,必须处理才可以执行后续的测试,所以弹框处理也是我们必须掌握的一个知识.宏哥在java+selenium系列文章中介绍过这部分内容. ...
- redis基本数据类型 set类型
127.0.0.1:6379> SADD s1 a b c (integer) 3 127.0.0.1:6379> SMEMBERS s1 1) "b" 2) &quo ...
- 如何选择适合你的HTAP数据库?
最近,在数据库行业对HTAP(混合事务/分析处理,Hybrid Transactional/Analytical Processing)这个概念宣传的非常火爆,也衍生出 Real-Time HTAP的 ...
- u-boot启动流程
U-Boot(Universal Bootloader)是一个通用的开源引导加载程序,通常用于嵌入式系统中,负责引导操作系统或加载 Linux 内核等任务.U-Boot的启动流程可以概括为以下几个关键 ...
- Python面试题——面向对象题
1.简述面向对象的三大特性. 封装: 封装指的是把一堆数据属性与方法数据放在一个容器中,这个容器就是对象.让对象可以通过 "." 来调用对象中的数据属性与方法属性. 继承: 继承指 ...
- Python基础——CPU详解
一 五大组成单元=>三大核心组件 组成计算机五大单元可以合并成三大核心组件:CPU.IO设备.主存储器 1.控制单元+算数逻辑单元=>CPU 2.主存储器,即主記憶體 3.输入单元Inpu ...
- QQ机器人整理合集
QQ机器人有什么用呢? QQ机器人可以实现包括自动回复.定时推送.发送图片QQ机器人,营销圈用的比较多,可以开发各种自动功能等等.用其制作的QQ机器人程序 机器人框架+插件 小栗子机器人 官网:htt ...