CF1918G Permutation of Given

Luogu 链接
CodeForces 链接
Virtual Judge 链接

题意

对于一个长度为 \(n\) 的序列,定义长度同样为 \(n\) 的序列 \(B\)\(\forall2\le i\le n-1,B_i=A_{i-1}+A_{i+1}\)\(B_1=A_2,B_n=A_{n-1}\)

给定一正整数 \(n\),要求构造一个长度为 \(n\) 的序列 \(A\),使得 \(A\) 中元素均不为 \(0\),并且重排后可以与相对应的序列 \(B\) 相同。

思路

递推

我们先假设对于 \(n\),我们构造出了 \(1\) 个满足条件的序列 \(A\),现在证明可以通过序列 \(A\),构造 \(1\) 个满足条件的长度为 \(n+2\) 序列 \(A'\)

对于每个 \(A_i\),显然都有 \(1\)\(B_j\) 与之一一对应。而在添加 \(2\) 个数后,\(A_{n-1}'\) 就不能与 \(B_n'\) 对应了,而变成 \(A_{n+1}'\)\(B_{n+2}'\) 对应。

现在 \(A_{n-1}'\)\(A_{n+2}'\) 还没有与 \(B'\) 对应,\(B_n'=A_{n-1}'+A_{n+1}'\)\(B_{n+1}'=A_n'+A_{n+2}'\) 还没有与 \(A'\) 对应。

因此,可以令 \(\begin{cases}A_{n-1}'+A_{n+1}'=A_{n+2}'&\\A_n'+A_{n+2}'=A_{n-1}'&\end{cases}\),解得 \(\begin{cases}A_{n+1}'=-A_n'&\\A_{n+2}'=A_{n-1}'-A_n'&\end{cases}\)

因此,只需令 \(\forall1\le i\le n,A_i'=A_i\)\(A_{n+1}'=-A_n,A_{n+2}'=A_{n-1}'-A_n'\) 即可。

所以,我们的任务转化为对长度为奇数和偶数求满足条件的基序列。

构造基序列

偶数

显然,\(1,2\) 就是 \(1\) 个符合条件的序列。


奇数

首先先证明没有长度为 \(3\) 的符合条件的序列(长度为
\(5\) 的序列样例就有,不予证明)。

首先,显然有 \(B_1=A_2=B_3=a\),由于序列 \(B\) 已经有 \(2\)\(a\),所以序列 \(A\) 中也至少有 \(2\)\(a\)

不妨设 \(A_1=a,A_3=b\),则有 \(b=A_3=B_2=A_1+A_3=a+b\),即 \(a=0\),矛盾。

所以没有长度为 \(3\) 的符合条件的序列。


现在要构造一个长度为 \(7\) 的符合条件的序列。

我们不妨令

\[\begin{cases}A_1+A_3=A_5&\\A_2+A_4=A_7&\\A_3+A_5=A_4&\\A_4+A_6=A_1&\\A_5+A_7=A_3\end{cases} \]

\(\begin{cases}A_1=a&\\A_2=b\end{cases}\),则有

\[\begin{cases}A_1=a&\\A_2=b&\\\displaystyle A_3=a-\frac{b}{2}&\\A_4=-a-b&\\\displaystyle A_5=-\frac{b}{2}&\\A_6=2a+b&\\A_7=-a\end{cases} \]

\(a=1,b=2\) 时,便可构造满足条件的 \(1\) 个序列 \(1,2,-2,-3,-1,4,-1\)

程序

AC 记录

#include<cstdlib>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdio>
#include<iostream>
#include<vector>
#include<map>
#include<cmath>
#include<iomanip>
#include<string>
#include<stack>
#include<random>
#define re register
#define ll long long
#define ull unsigned long long
#define vl __int128
#define ld long double
#define LL 2e18
#define INT 1e9
#define INF 0x3f3f3f3f
#define lb(x) (x&(-x))
#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc _getchar_nolock
#define pc _putchar_nolock
#endif
int T=1;
using namespace std;
inline bool blank(const char x){return !(x^32)||!(x^10)||!(x^13)||!(x^9);}
template<typename Tp>inline void read(Tp &x){x=0;re bool z=true;re char a=gc();for(;!isdigit(a);a=gc())if(a=='-')z=false;for(;isdigit(a);a=gc())x=(x<<1)+(x<<3)+(a^48);x=(z?x:~x+1);}
inline void read(double &x){x=0.0;re bool z=true;re double y=0.1;re char a=gc();for(;!isdigit(a);a=gc())if(a=='-')z=false;for(;isdigit(a);a=gc())x=x*10+(a^48);if(a!='.')return x=z?x:-x,void();for(a=gc();isdigit(a);a=gc(),y/=10)x+=y*(a^48);x=(z?x:-x);}
inline void read(ld &x){x=0.0;re bool z=true;re ld y=0.1;re char a=gc();for(;!isdigit(a);a=gc())if(a=='-')z=false;for(;isdigit(a);a=gc())x=x*10+(a^48);if(a!='.')return x=z?x:-x,void();for(a=gc();isdigit(a);a=gc(),y/=10)x+=y*(a^48);x=(z?x:-x);}
inline void read(char &x){for(x=gc();blank(x)&&(x^-1);x=gc());}
inline void read(char *x){re char a=gc();for(;blank(a)&&(a^-1);a=gc());for(;!blank(a)&&(a^-1);a=gc())*x++=a;*x=0;}
inline void read(string &x){x="";re char a=gc();for(;blank(a)&&(a^-1);a=gc());for(;!blank(a)&&(a^-1);a=gc())x+=a;}
template<typename T,typename ...Tp>inline void read(T &x,Tp &...y){read(x),read(y...);}
template<typename T>inline void read(T *begin,T *end){re T *i;if(begin<end)for(i=begin;i<end;++i)read(*i);else for(i=begin-1;i>=end;--i)read(*i);}
template<typename Tp>inline void write(Tp x){if(!x)return pc(48),void();if(x<0)pc('-'),x=~x+1;re int len=0;re char tmp[64];for(;x;x/=10)tmp[++len]=x%10+48;while(len)pc(tmp[len--]);}
inline void write(const double x){re int a=6;re double b=x,c=b;if(b<0)pc('-'),b=-b,c=-c;re double y=5*powl(10,-a-1);b+=y,c+=y;re int len=0;re char tmp[64];if(b<1)pc(48);else for(;b>=1;b/=10)tmp[++len]=floor(b)-floor(b/10)*10+48;while(len)pc(tmp[len--]);pc('.');for(c*=10;a;a--,c*=10)pc(floor(c)-floor(c/10)*10+48);}
inline void write(const ld x){re int a=6;re ld b=x,c=b;if(b<0)pc('-'),b=-b,c=-c;re ld y=5*powl(10,-a-1);b+=y,c+=y;re int len=0;re char tmp[64];if(b<1)pc(48);else for(;b>=1;b/=10)tmp[++len]=floor(b)-floor(b/10)*10+48;while(len)pc(tmp[len--]);pc('.');for(c*=10;a;a--,c*=10)pc(floor(c)-floor(c/10)*10+48);}
inline void write(const pair<int,double>x){re int a=x.first;if(a<7){re double b=x.second,c=b;if(b<0)pc('-'),b=-b,c=-c;re double y=5*powl(10,-a-1);b+=y,c+=y;re int len=0;re char tmp[64];if(b<1)pc(48);else for(;b>=1;b/=10)tmp[++len]=floor(b)-floor(b/10)*10+48;while(len)pc(tmp[len--]);a&&(pc('.'));for(c*=10;a;a--,c*=10)pc(floor(c)-floor(c/10)*10+48);}else printf("%.*lf",a,x.second);}
inline void write(const pair<int,ld>x){re int a=x.first;if(a<7){re ld b=x.second,c=b;if(b<0)pc('-'),b=-b,c=-c;re ld y=5*powl(10,-a-1);b+=y,c+=y;re int len=0;re char tmp[64];if(b<1)pc(48);else for(;b>=1;b/=10)tmp[++len]=floor(b)-floor(b/10)*10+48;while(len)pc(tmp[len--]);a&&(pc('.'));for(c*=10;a;a--,c*=10)pc(floor(c)-floor(c/10)*10+48);}else printf("%.*Lf",a,x.second);}
inline void write(const char x){pc(x);}
inline void write(const bool x){pc(x?49:48);}
inline void write(char *x){fputs(x,stdout);}
inline void write(const char *x){fputs(x,stdout);}
inline void write(const string &x){fputs(x.c_str(),stdout);}
template<typename T,typename ...Tp> inline void write(T x,Tp ...y){write(x),write(y...);}
template<typename T>inline void write(T *begin,T *end,const char c=' '){re T *i;for(i=begin;i<end;++i)write(*i,c);}
template<typename T>inline void init(T *begin,T *end,const T& val=T()){re T* i;for(i=begin;i<end;++i)*i=val;}
template<typename T>inline T max(T *begin,T *end){re T *ans,*i;for(i=begin;i<end;++i)if(i==begin||*ans<*i)ans=i;return *ans;}
template<typename T>inline T min(T *begin,T *end){re T *ans,*i;for(i=begin;i<end;++i)if(i==begin||*i<*ans)ans=i;return *ans;}
template<typename T>inline T calc_sum(T *begin,T *end,const T& val=T()){re T ans=val,*i;for(i=begin;i<end;++i)ans+=*i;return ans;}
template<typename T>inline bool is_equal(T *begin,T *end,const T& val=T()){re T *i;for(i=begin;i<end;++i)if(*i!=val)return false;return true;}

ll mod=0;
const int MAXN=1e6;
const int N=MAXN+10;
//#define DEBUG
//#define more_text

int n,a[N];

void SOLVE(int _){
	read(n);
	if(n==3||n==5){
		write("NO");
		return;
	}
	if(n&1){
		a[1]=1,a[2]=2,a[3]=-2,a[4]=-3,a[5]=-1,a[6]=4,a[7]=-1;
		for(int i=8;i<=n;i+=2)a[i]=-a[i-1],a[i+1]=a[i-2]-a[i-1];
	}
	else{
		a[1]=1,a[2]=2;
		for(int i=3;i<=n;i+=2)a[i]=-a[i-1],a[i+1]=a[i-2]-a[i-1];
	}
	write("YES\n");
	write(a+1,a+n+1);
}
/*
Input:

Output:

Outline:

*/
int main(){
	#ifdef DEBUG
	freopen("test.in","r",stdin);
	freopen("test.out","w",stdout);
	#endif
	#ifdef more_text
	read(T);
	#endif 
	for(int i=1;i<=T;++i)SOLVE(i);
	#ifdef DEBUG
	fclose(stdin);fclose(stdout);
	#endif
	return 0;
}
posted @ 2025-03-09 10:15  LXcjh4998  阅读(12)  评论(0)    收藏  举报