[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 ...
随机推荐
- Codeforces 1462F The Treasure of The Segments
题意 给\(n(1\leq n\leq 2*10^5)\)个线段$[l_i,r_i] (1≤l_i≤r_i≤10^9) $,问最少删除几个线段,使得剩下线段中,有至少一个线段与所有线段相交. 分析 对 ...
- 小白弄明白了 unix 时间戳的转换问题
小白对于将 unix 时间戳转换为日期时间和使用日期时间转换为 unix 时间戳,在项目中见到过很多,每次使用时不是用现有的方法转换就是网上搜索方法. 小白见过各种转换方式觉得moment库很是方便, ...
- 《Python魔法大冒险》008 石像怪的挑战:运算符之旅
小鱼和魔法师继续深入魔法森林.不久,他们来到了一个巨大的魔法石圈旁边.石圈中心有一个闪闪发光的魔法水晶,周围则是一些神秘的符号.但令人意外的是,水晶的旁边还有一个巨大的石像怪,它的眼睛散发着红色的光芒 ...
- Solution Set -「ARC 116」(in progress)
「ARC 116A」Odd vs Even Link. 看 \(n\) 有多少 \(2\) 因子. // Problem: A - Odd vs Even // Contest: AtCoder - ...
- 西门子300PLC转以太网无需编程实现与1200PLC转以太网数据交换
西门子300PLC转以太网无需编程实现与1200PLC转以太网数据通信 本文介绍利用兴达易控生产的PLC转以太网模块(MPI-ETH-XD1.0Plus)实现1200/1500PLC与300(CPU3 ...
- 历时一个月,《穿透Laravel》全书完成!
近几年来Laravel在PHP领域大放异彩,逐渐成为PHP开发框架中的中流砥柱. 这个系列的文章, 会带你一起探知Laravel框架底层的实现细节.与其他框架相比,Laravel的设计理念确实更为先进 ...
- Django框架——模板层
文章目录 1 模板层 一 模版简介 二 模版语法之变量 views.py html文件 三 模版之过滤器 语法: default length filesizeformat date slice tr ...
- Python面向对象——封装
文章目录 内容回顾 封装 为何要隐藏? 作业 内容回顾 上节课复习: 1.编程范式/思想 面向过程 介绍: 核心是"过程"二字 过程就是"流水线" 过程终极奥义 ...
- 轻松掌握组件启动之Redis单机、主从、哨兵、集群配置
单机配置启动 Redis安装 下载地址:http://redis.io/download 安装步骤: 1: 安装gcc编译器:yum install gcc 2: 将下载好的redis‐5.0.3.t ...
- Go开发IDE全览:GoLand vs VSCode全面解析
关注[TechLeadCloud],分享互联网架构.云服务技术的全维度知识.作者拥有10+年互联网服务架构.AI产品研发经验.团队管理经验,同济本复旦硕,复旦机器人智能实验室成员,阿里云认证的资深架构 ...