1618E - Singers' Tour - CodeForces Solution


constructive algorithms math *1700

Please click on ads to support us..

Python Code:

import sys
import itertools
import copy

input = sys.stdin.readline
for _ in range(int(input())):
  n = int(input())
  b = list(map(int, input().split()))
  s = sum(b)
  if s % (n * (n + 1) // 2):
    print("NO")
    continue
  s //= (n * (n + 1) // 2)
  b = itertools.starmap(int.__sub__, zip(b[-1:] + b[:-1], b))
  b = map(s.__add__, b)
  b = map(n.__rfloordiv__, b)
  if sum(copy.deepcopy(b)) != s or max(copy.deepcopy(b)) > 1e9 or min(copy.deepcopy(b)) < 1:
    print("NO")
    continue
  print("YES")
  print(*b)

C++ Code:

#include <bits/stdc++.h>
using namespace std;

//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
//typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set;
//find_by_order, order_of_key

#ifdef LOCAL
	#define db(x) cout << "Line " << __LINE__ << ": " << #x << " = " << to_string(x) << endl;
	#define endl endl
#else
	#define db(x) 42;
	#define endl '\n'
#endif


#define int 	long long
#define ll 	long long
#define all(x) 	x.begin(),x.end()
#define rev(x) 	x.rbegin(),x.rend()
#define nd 	<< " " << 
#define ln 	<< endl
#define lbr 	cout << endl;
#define fi 	first
#define se 	second
#define pb 	push_back
#define rep(i, s, e) 	for(int i = s; i < e; i++)
#define repr(i, s, e) 	for(int i = s; i >= e; i--)
#define reps(i,s,e,step) 	for(int i=s; i < e; i+=step)
#define repa(x, a) 	for(auto &x: a)
#define vc vector
#define vi 	vector<int>
#define pii 	pair<int, int>
#define endt 	return 0;
#define sz(a)  ((long long)a.size())
#define odd 	%2==1
#define even 	%2==0
#define llmx LONG_LONG_MAX
#define llmn LONG_LONG_MIN
#define yn(f)   cout << ((f)?"YES":"NO") << endl;
#define inrange(x,a,b) ((x>=min(a,b)) && (x<=max(a,b)))
//use (long long)round(pow(x, y))


signed solve(int& tc){
	//db(tc)
	int n;
	cin >> n ;
	
	vi b(n);
	rep(i, 0, n){
		cin >> b[i];
	}
	
	ll S=2*(accumulate(all(b), 0LL));
	ll x=S/(n*(n+1));
	if(x*(n*(n+1))!=S){
		yn(0)
		endt
	}
	
	vi a(n);
	rep(i,0,n){
		a[i]=(x+b[(i-1+n)%(n)]-b[i]);
		if(a[i]%n!=0 || inrange(a[i]/n, 1LL, (ll)(1e9))==0){
			yn(0)
			endt
		}
		a[i]/=n;
	}
	
	yn(1)
	rep(i,0,n) cout << a[i] << " ";
	
	cout << endl;
	return 0;
}


signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin.exceptions(ios::badbit | ios::failbit);
	
	#ifdef LOCAL
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif
	
	int tc=1;
	cin >> tc;
	for(int i=1; i<=tc; i++) solve(i);
	return 0;
}


Comments

Submit
0 Comments
More Questions

230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns