其他分享
首页 > 其他分享> > Codeforces Round #760 (Div. 3)

Codeforces Round #760 (Div. 3)

作者:互联网

比赛链接

Codeforces Round #760 (Div. 3)

\(n\) towns are arranged in a circle sequentially. The towns are numbered from 1 to \(n\) in clockwise order. In the \(i\)-th town, there lives a singer with a repertoire of \(a_{i}\) minutes for each \(i \in[1, n]\).

Each singer visited all \(n\) towns in clockwise order, starting with the town he lives in, and gave exactly one concert in each town. In addition, in each town, the \(i\)-th singer got inspired and came up with a song that lasts \(a_{i}\) minutes. The song was added to his repertoire so that he could perform it in the rest of the cities.

Hence, for the \(i\)-th singer, the concert in the \(i\)-th town will last \(a_{i}\) minutes, in the \((i+1)\)-th town the concert will last \(2 \cdot a_{i}\) minutes, ..., in the \(((i+k) \bmod n+1)\)-th town the duration of the concert will be \((k+2) \cdot a_{i}, \ldots\), in the town \(((i+n-2) \bmod n+1)-n \cdot a_{i}\) minutes.

You are given an array of \(b\) integer numbers, where \(b_{i}\) is the total duration of concerts in the \(i\)-th town. Reconstruct any correct sequence of positive integers \(a\) or say that it is impossible.

Input

The first line contains one integer \(t\left(1 \leq t \leq 10^{3}\right)\) - the number of test cases. Then the test cases follow.
Each test case consists of two lines. The first line contains a single integer \(n\left(1 \leq n \leq 4 \cdot 10^{4}\right)-\) the number of cities. The second line contains \(n\) integers \(b_{1}, b_{2}, \ldots, b_{n}\left(1 \leq b_{i} \leq 10^{9}\right)\) - the total duration of concerts in \(i\)-th city.
The sum of \(n\) over all test cases does not exceed \(2 \cdot 10^{5}\).

Output

For each test case, print the answer as follows:
If there is no suitable sequence \(a\), print NO. Otherwise, on the first line print YES, on the next line print the sequence \(a_{1}, a_{2}, \ldots, a_{n}\) of \(n\) integers, where \(a_{i}\left(1 \leq a_{i} \leq 10^{9}\right)\) is the initial duration of repertoire of the \(i\)-th singer. If there are multiple answers, print any of them.

Example

input

4
3
12 16 14
1
1
3
1 2 3
6
81 75 75 93 93 87

output

YES
3 1 3 
YES
1 
NO
YES
5 5 4 1 4 5 

Note

Let's consider the 1-st test case of the example:

  1. the 1-st singer in the 1-st city will give a concert for 3 minutes, in the 2 -nd - for 6 minutes, in the 3 -rd - for 9 minutes;
  2. the 2 -nd singer in the 1 -st city will give a concert for 3 minutes, in the 2 -nd - for 1 minute, in the 3 -rd - for 2 minutes;
  3. the 3 -rd singer in the 1 -st city will give a concert for 6 minutes, in the 2 -nd - for 9 minutes, in the 3 -rd - for 3 minutes.

解题思路

构造,线性代数

即求解如下 \(n\) 个 \(n\) 元一次方程组:
\( \begin{cases} & a_1+ n\times a_2+(n-1)\times a_3+\dots +2\times a_n=b_1 \\ & 2\times a_1+a_2+n\times a_3+\dots +3\times a_n=b_2 \\ & \vdots \\ & n\times a_1 + (n-1)\times a_2 +(n-2)\times a_3+\dots +a_n=b_n \end{cases} \)

当高斯消元的复杂度为 \(n^3\),会超时,不妨先从小点的范围开始构造
构造答案前,将上面的等式加起来,有 \(\frac{(n+1)\times n}{2} \times \sum_{i=1}^n a_i=\sum_{i=1}^n b_i\),则必须有 \(\sum_{i=1}^n b_i \%\frac{(n+1)\times n}{2} =0\)
从 \(n=3\) 开始构造:
有如下等式:
\( \begin{cases} & a_1+2a_2+2a_3=b_1 \\ & 2a_1+a_2+3a_3=b_2 \\ \end{cases} \)
做差,得 \( -a_1+2a_2-a_3=b_1-b_2 \),即 \(a_2=\frac{b_1-b_2+a_1+a_2+a_3}{3}\),
得构造得到解:\(a[i\%n+1]=\frac{b[i]-b[i\%n+1]+\sum_{i=1}^na[i]}{n}\),注意 \(a[i]\) 为正整数,且范围为 \([1,10^9]\)

代码

// Problem: E. Singers' Tour
// Contest: Codeforces - Codeforces Round #760 (Div. 3)
// URL: https://codeforces.com/contest/1618/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=4e4+5;
int n,t;
LL a[N],b[N];
int main()
{
    for(scanf("%d",&t);t;t--)
    {
    	scanf("%d",&n);
    	LL sum=0;
    	for(int i=1;i<=n;i++)scanf("%lld",&b[i]),sum+=b[i];
    	
    	int all=1ll*n*(n+1)/2;
    	if(sum%all!=0)
    	{
    		puts("NO");
    		continue;
    	}
    	sum/=all;
    	bool f=true;
    	for(int i=1;i<=n;i++)
    	{
    		LL t=b[i]-b[i%n+1]+sum;
    		if(t%n!=0)
    		{
    			f=false;
    			break;
    		}
    		a[i%n+1]=t/n;
    		if(!(a[i%n+1]>=1&&a[i%n+1]<=1e9))
    		{
    			f=false;
    			break;
    		}
    	}
    	
    	if(!f)puts("NO");
    	else
    	{
    		puts("YES");
    		for(int i=1;i<=n;i++)printf("%lld ",a[i]);
    		puts("");
    	}
    }
    return 0;
}

标签:town,singer,760,Codeforces,times,leq,th,Div,minutes
来源: https://www.cnblogs.com/zyyun/p/16250728.html