首页 > 其他分享 >[ABC282E] Choose Two and Eat One

[ABC282E] Choose Two and Eat One

时间:2023-01-03 16:45:01浏览次数:44  
标签:box balls return int ABC282E leq Choose fourth Eat

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$.

  1. 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.
  2. 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.
  3. 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);
}

标签:box,balls,return,int,ABC282E,leq,Choose,fourth,Eat
From: https://www.cnblogs.com/mekoszc/p/17022683.html

相关文章

  • 提示:clnt_create: RPC: Program not registered
    提示:clnt_create:RPC:Programnotregistered在NFS服务器端(RHEL6.5)使用命令showmount-e127.0.0.1报错,提示:clnt_create:RPC:Programnotregistered解决办法:......
  • cocos creator教程:框架 - 音频
    【muzzik教程】:框架-音频此处仅谈思想,代码后续会发布在很多代码框架中、其实audio这一块的逻辑一直都没有更新、永远都只有管理音乐、音效的状态控制在我看来这......
  • cocos creator教程:框架 - 多语言
    【muzzik教程】:框架-多语言此处仅谈思想,代码后续会发布多语言需要解决的问题文本、图片不同语言之间的动态切换多状态节点(不同语言间节点属性可能不同、如位......
  • torch.repeat()
    torch.repeat函数函数功能torch.tensor.repeat()函数可以对张量进行重复扩充1)当参数只有两个时:(行的重复倍数,列的重复倍数),1表示不重复。2)当参数有三个时:(通道数的重复......
  • seata 分支事务注册及分支事务回滚时序图梳理
    1.业务时序图集成的seata版本为1.5.2。如图所示,创建三个微服务注册分支事务,整个流程中有四次DB操作,在最后执行异常抛出,用于演示分支事务注册及回滚时序。  2.各服务......
  • Linux搭建ELK+Filebeat+Redis分布式日志管理平台
    ELK介绍需求背景业务发展越来越庞大,服务器越来越多各种访问日志、应用日志、错误日志量越来越多,导致运维人员无法很好的去管理日志开发人员排查问题,需要到服务器上查日志,不......
  • Linux一键部署ELK+Filebeat+Nginx+Redis日志平台自动化脚本
    此脚本是Linux一键部署ELK+Filebeat+Nginx+Redis日志平台自动化脚本,有需要朋友可以参考,脚本内容如下:环境准备操作系统:CentOSLinuxrelease7.8.2003软件版本Elasticsearch:e......
  • ELK+Filebeat+Kafka分布式日志管理平台搭建
    ELK介绍需求背景业务发展越来越庞大,服务器越来越多各种访问日志、应用日志、错误日志量越来越多,导致运维人员无法很好的去管理日志开发人员排查问题,需要到服务器上查日志,不......
  • Linux搭建ELK+Filebeat+Nginx+Redis分布式日志管理平台
    ELK介绍需求背景业务发展越来越庞大,服务器越来越多各种访问日志、应用日志、错误日志量越来越多,导致运维人员无法很好的去管理日志开发人员排查问题,需要到服务器上查日志,不......
  • Adobe Creative Suite 5.5 Master …
    AdobeCS5.5目前在官方服务器上可以下载CS5.5的试用版本。AdobeCS5.5包含各种平面设计,视频编辑,多媒体制作和网站开发应用程序。本文提供AdobeCS5.5系列下载汇总。注:如以......