【Atcoder】 [ARC159C] Permutation Addition

这篇具有很好参考价值的文章主要介绍了【Atcoder】 [ARC159C] Permutation Addition。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

题目链接

Atcoder方向
Luogu方向

题目解法

首先考虑是否有可行的方案
从数列总和方向考虑
最终每个数相同,那么 s u m sum sum 一定是 n n n 的倍数
同时每次数列会加上 n ( n + 1 ) 2 \frac{n(n+1)}{2} 2n(n+1)
考虑对 n n n 分奇偶考虑

  1. n为奇数,那么 n ( n + 1 ) 2 \frac{n(n+1)}{2} 2n(n+1)一定是 n n n 的倍数,所以一开始数列的和 s s s 一定要为 n n n 的倍数
  2. n为偶数, n ( n + 1 ) 2   m o d   n = n 2 \frac{n(n+1)}{2} \bmod n=\frac{n}{2} 2n(n+1)modn=2n,所以 s s s 一定模 n n n 0 0 0 n 2 \frac{n}{2} 2n
    同时当 s s s n 2 \frac{n}{2} 2n 的倍数时,可以随机操作一次使 s s s n n n 的倍数

现在考虑 s s s n n n 的倍数时如何构造
考虑是否可以将 2 2 2 个数相对整个数列分别 + 1 +1 +1 − 1 -1 1
构造方案:
2 ,          1 ,              3 , . . . ,        n 2,\;\;\;\;1,\;\;\;\;\;\;3,...,\;\;\;n 2,1,3,...,n
n , n − 1 , n − 2 , . . . , 1 n,n-1,n-2,...,1 n,n1,n2,...,1
第一个数加了 n + 2 n+2 n+2,第二个数加了 n n n,其他数加了 n + 1 n+1 n+1
并且进行了 2 2 2 次操作之后 s s s 仍为 n n n 的倍数
所以若干次之后一定可以将每个数都变为相同

操作次数是 2 n a 2na 2na 级别的文章来源地址https://www.toymoban.com/news/detail-559544.html

#include <bits/stdc++.h>
using namespace std;
const int N(60),M(10100);
int n,ans,way[M][N];
pair<int,int> a[N];
inline int read(){
	int FF=0,RR=1;
	char ch=getchar();
	for(;!isdigit(ch);ch=getchar()) if(ch=='-') RR=-1;
	for(;isdigit(ch);ch=getchar()) FF=(FF<<1)+(FF<<3)+ch-48;
	return FF*RR;
}
int main(){
	n=read();
	int s=0;
	for(int i=1;i<=n;i++) a[i]=make_pair(read(),i),s+=a[i].first;
	if((n&1)&&(s%n)){ puts("No");exit(0);}//n为奇数,必须满足s是n的倍数 
	if((~n&1)&&(s%n!=0)&&(s%n!=n/2)){ puts("No");exit(0);}//n为偶数,必须满足s为0或n/2的倍数
	if(s%n==n/2){
		sort(a+1,a+n+1);ans++;
		for(int i=1;i<=n;i++) way[ans][a[i].second]=n-i+1,a[i].first+=n-i+1;
	} 
	while(true){
		bool flg=1;
		for(int i=2;i<=n;i++) if(a[i].first!=a[i-1].first){ flg=0;break;}
		if(flg) break;
		sort(a+1,a+n+1);ans++;
		way[ans][a[1].second]=2,a[1].first+=2,way[ans][a[n].second]=1,a[n].first++;
		for(int i=2;i<n;i++) way[ans][a[i].second]=i+1,a[i].first+=i+1;
		ans++;
		way[ans][a[1].second]=n,a[1].first+=n,way[ans][a[n].second]=n-1,a[n].first+=n-1;
		for(int i=2;i<n;i++) way[ans][a[i].second]=n-i,a[i].first+=n-i; 
	}
	puts("Yes");printf("%d\n",ans);
	for(int i=1;i<=ans;i++){
		for(int j=1;j<=n;j++) printf("%d ",way[i][j]);
		puts("");
	}
	return 0;
}

到了这里,关于【Atcoder】 [ARC159C] Permutation Addition的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • STL—next_permutation函数

    目录 1.next_permutation函数的定义 2.简单使用 2.1普通数组全排列  2.2结构体全排列 2.3string 3.补充 next_permutation函数 会按照字母表顺序生成给定序列的 下一个较大的排列 ,直到整个序列为 降序 为止。与其相对的还有一个函数—— prev_permutation函数。 next_permutaion(起始地址,末尾

    2024年02月04日
    浏览(80)
  • 线性代数 --- 置换矩阵 (Permutation matrix)

            对一个矩阵进行行交换,需要通过置换矩阵(permutation matrix)来完成。         在对一个Ax=b的方程组进行高斯消元的过程中,我们常常会遇到一种情况,也就是消元消不下去的情况。下面,我列出了两个不同的3x3矩阵的消元过程:         上图中的第一行,是一

    2024年02月06日
    浏览(31)
  • STM32 基础知识(探索者开发板)--159讲 CAN总线

    CAN基础知识:ISO国际标准化的串行通信协议,为了减少线束的数量 a.多主控制  每个设备都可以主动发送数据 b.通信速度较快,通信距离远。最高1Mbps(距离小于40M),最远可达10KM(速率低于5Kbps) c.具有错误检测、错误通知和错误恢复功能 d.故障封闭功能  能发现故障,且可以把故

    2024年01月17日
    浏览(36)
  • Machine Learning in Action: User Addition Prediction Challenge

    Contest type: Data mining, two classification. User addition prediction is a key step in analyzing user usage scenarios and predicting user growth, which is helpful for subsequent product and application iterative upgrades. The data set consists of about 620,000 training sets and 200,000 test sets, including 13 fields. In the preceding command, uuid is the u

    2024年02月13日
    浏览(30)
  • Application of Permutation and Combination

    目录 Summary Reference Online Tool Cracking the Safe! 计算比赛前三名有多少种排列方式? Can you win the lottery? How make a pill? Think 如果你遇到的问题,自己不确定是排列还是组合,但确定想求出摆放的全部方式,那么可以用逐步分析法。 0.首先确定这个问题的次序重要不? 1.重要 重要就用

    2024年02月08日
    浏览(23)
  • (列置换密码)(Column Permutation Cipher)(含代码)

    密码学(在西欧语文中,源于希腊语kryptós“隐藏的”,和gráphein“书写”)是研究如何隐密地传递信息的学科。在现代特别指对信息以及其传输的数学性研究,常被认为是数学和计算机科学的分支,和信息论也密切相关。著名的密码学者Ron Rivest解释道:“密码学是关于如何

    2023年04月17日
    浏览(49)
  • LeetCode每日一题(2457. Minimum Addition to Make Integer Beautiful)

    You are given two positive integers n and target. An integer is considered beautiful if the sum of its digits is less than or equal to target. Return the minimum non-negative integer x such that n + x is beautiful. The input will be generated such that it is always possible to make n beautiful. Example 1: Input: n = 16, target = 6 Output: 4 Explanation: Init

    2023年04月16日
    浏览(84)
  • c++入门必学库函数 next_permutation

    next_permutation的意思是下一个排列,与其相对的是prev_permutation,即上一个排列。我们需要使用全排列的时候就可以直接使用这两个函数,方便又快捷 由于prev_permutation和next_permutation的用法是一样的,下面就值讲解next_permutation的基本用法 next_permutation只能获得上一个排列,如果要

    2024年02月02日
    浏览(25)
  • 【iOS】ARC学习

    在学习ARC之前,先来复习一下内存管理以及autorelease的实现 先来看一下GNUstep源代码: autorelease 其本质就是调用 NSAutoreleasePool 对象的 addObject 类方法,就是 将对象加到自动释放池中 接下来再看一下废弃自动释放池的一些功能函数 可使用 showPools 输出现在的NSAutoreleasePool的状况

    2024年03月17日
    浏览(27)
  • 【iOS】ARC内存管理

    怎么说呢。经典再放送咯。 对象操作 对应的Objective-C方法 生成并持有对象 alloc/new/copy/mutableCopy等方法 持有对象 retain方法 释放对象 release方法 废弃对象 dealloc方法 iOS内存管理方案有三种,我们详细看下每种方案的实现及存在的意义。 标签指针 没有这种管理机制会引起内存浪

    2024年02月16日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包