首页 > 其他分享 >[ARC145] Non Arithmetic Progression Set

[ARC145] Non Arithmetic Progression Set

时间:2022-12-17 08:33:35浏览次数:38  
标签:10 Non Set 15 进制 Progression int sum 集合

Problem Statement

Construct a set $S$ of integers satisfying all of the conditions below. It can be proved that at least one such set $S$ exists under the Constraints of this problem.

  • $S$ has exactly $N$ elements.
  • The element of $S$ are distinct integers between $-10^7$ and $10^7$ (inclusive).
  • $ \displaystyle \sum _{s \in S} s = M$.
  • $ y-x\neq z-y$ for every triple $ x,y,z$ $(x < y < z)$ of distinct elements in $ S$.

Constraints

  • $1 \leq N \leq 10^4$
  • $|M| \leq N\times 10^6$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$ $M$

Output

Let $s_1,s_2,\ldots,s_N$ be the elements of $S$. Print a set $S$ that satisfies the conditions in the following format:

$s_1$ $s_2$ $\ldots$ $s_N$

If multiple solutions exist, any of them will be accepted.


Sample Input 1

3 9

Sample Output 1

1 2 6

We have $2-1 \neq 6-2$ and $1+2+6=9$, so this output satisfies the conditions. Many other solutions exist.


Sample Input 2

5 -15

Sample Output 2

-15 -5 0 2 3

$M$ may be negative.

存在等差数列,当且仅当存在三个数$x,y,z$,使得 $y-x=z-y$

移一下项,得到 \(2y=z+x\)

考虑一个数的三进制。如果集合中所有数的三进制表示下只有 \(0\) 和\(1\),那么 \(2y\) 的三进制表示下只由 \(0\) 和 \(2\) 组成。若要选出 \(x+z=2y\) ,当且仅当 \(x=z=y\),而集合有不可重性。所以如果这样构造,可以得出答案。易得构造出的数在 \([-10^7,10^7]\) 内(见附注)。

但是我们要保证所有数和为m。容易发现,如果一个集合 \(s\) 是合法的,那么给 \(s\) 中每一个数同时加上一个数,集合仍然没有等差数列。所以如果构造出序列后,我们先想办法让他们的和与 \(m\) 模 \(n\) 同余,然后再给每个数加上 \(\frac{(m-sum(s))}{n}\)即可。如何微调集合使得他们的和模 \(n\) 同余呢?在枚举三进制时,我们可以空出最后一位,然后微调。

上面就是大概思路,我们用样例详解

\(n=5,m=-15\)

首先构造有5个数的合法集合

\((0010)_3=3\)
\((0100)_3=9\)
\((0110)_3=12\)
\((1000)_3=27\)
\((1010)_3=30\)

和为 \(3+9+12+27+30=81\),模 \(5\) 余 \(1\)。\(m\) 模 \(5\) 余 \(0\)。

所以我们要选择 \(4\) 个数加 \(1\)。集合变成了:

\((0011)_3=4\)
\((0101)_3=10\)
\((0111)_3=13\)
\((1001)_3=28\)
\((1010)_3=30\)

那么我们再给每个数减去 \((85-(-15))/5=20\)。集合就是
$ {-16,-10,-7,8,10}$

代码就很好写了。

建议枚举初始合法三进制时用二进制枚举。

#include<bits/stdc++.h>
const int N=10005;
int n,s[N],ret;
long long m,x,sum,d;
int main()
{
	scanf("%d%lld",&n,&m);
	for(int i=2;i<=2*n;i+=2)
	{
		ret=3;
		for(int j=1;j<15;j++)
		{
			if(i>>j&1)
				s[i>>1]+=ret;
			ret*=3;
		}
		sum+=s[i>>1];
	}
	x=((m-sum)%n+n)%n;
	d=floor((1.00*m-sum)/n);
	for(int i=1;i<=x;i++)
		s[i]++;
	for(int i=1;i<=n;i++)
		printf("%lld ",s[i]+d);
}

标签:10,Non,Set,15,进制,Progression,int,sum,集合
From: https://www.cnblogs.com/mekoszc/p/16988594.html

相关文章

  • setnx命令学习
    转自:https://segmentfault.com/a/1190000039670844,https://bbs.csdn.net/topics/392160228,https://cloud.tencent.com/developer/article/17757151.介绍SETNXkeyvalue......
  • Pset_ChimneyCommon
    Pset_ChimneyCommon烟囱所有引用对象和类型对象定义的公共特性 NameTypeDescriptionReferenceP_SINGLEVALUE / IfcIdentifierBauteiltypBezeichnungzur......
  • magento Attribute Set Details
    IntroductionAnattributesetisacollectionofattributeswhichcanbecreated/editedfromCatalog->ManageAttributeSetsinbackend.Someusefulsnippetsre......
  • hasattr()、getattr()、setattr()函数简介
    hasattr(object,name)判断object对象中是否存在name属性,当然对于python的对象而言,属性包含变量和方法;有则返回True,没有则返回False;需要注意的是name参数是string类型,所以......
  • torch.version.cuda返回None,改为使用手动下载Pytorch GPU版本whl文件并安装
    用pip安装时网速实在太慢,换源也不太行,1.2G的文件,一个网络波动就开始疯狂红字。因此使用whl文件进行安装!PytorchGPU版本whl文件安装_龙倚亭的博客-CSDN博客_pytorchwhl......
  • 应用设置Setting的实现
    有很多应用都在iOS设置中有相关的设置,如下图:   通过这个设置可以方便的对应用的一些基本的设置进行更改。要完整的实现这个设置功能,有以下几方面问题需要解决:1)设置的编......
  • 【快应用】初始化页面时,调用configuration.setLocale()不生效
    ​现象描述快应用app.ux中定义了全局方法changeLocaleConfiguration,用于设置应用显示语言,在首页生命周期onInit中调用changeLocaleConfiguration(),实际已经触发了该方法,但......
  • reset.css 2022
    /***ThenewCSSreset-version1.7.3(lastupdated7.8.2022)GitHubpage:https://github.com/elad2412/the-new-css-reset***//*Removeallthe......
  • Chapter 10.利用Redis Zset实现双维度排行榜
    欢迎来到「我是真的狗杂谈世界」,关注不迷路背景最近需要将遇到的几个排行需求点抽出来做一个独立的通用排行组件,整理记录一下。核心需求能获得连续的部分的榜单:比如......
  • VUE使用axios数据请求时报错 TypeError Cannot set property 'xxxx' of undefined 的
    正常情况下在data里面都有做了定义data(){list:"haha"}在函数里面进行赋值this.list=response.data.result这时候你运行时会发现,数据可以请求到,但是会报错TypeErr......