# B. Big Vova SOLUTIONS Codeforces Round #669 (Div. 2)

## Big Vova SOLUTION

Alexander is a notable software engineer. Today he chose to at last go out and play football, however with the principal hit he left a mark on the new Rolls-Royce of the affluent finance manager Big Vova. Vladimir has as of late opened a store on the mainstream online commercial center “Zmey-Gorynych”, and extends Alex an employment opportunity: on the off chance that he shows his programming abilities by illuminating an assignment, he’ll fill in as a cybersecurity master. Else, he’ll be conveying some far fetched items for the following two years.
You’re given n positive numbers a1,a2,… ,an. Utilizing every one of them precisely without a moment’s delay, you’re to make such succession b1,b2,… ,bn that arrangement c1,c2,… ,cn is lexicographically maximal, where ci=GCD(b1,… ,bi) – the best basic divisor of the main I components of b.
Alexander is truly terrified of the states of this straightforward undertaking, so he requests that you illuminate it.
A grouping an is lexicographically littler than a succession b if and just in the event that one of the accompanying holds:
a will be a prefix of b, however a≠b;
in the main position where an and b vary, the succession a has a littler component than the relating component in b.
Information
Each test contains various experiments. The principal line contains the quantity of experiments t (1≤t≤103). Portrayal of the experiments follows.
The principal line of each experiment contains a solitary whole number n (1≤n≤103) — the length of the succession a.
The second line of each experiment contains n whole numbers a1,… ,a (1≤ai≤103) — the succession a.
It is ensured that the whole of n over all experiments doesn’t surpass 103.
Yield
For each experiment yield the appropriate response in a solitary line — the ideal grouping b. On the off chance that there are various answers, print any.
Model
inputCopy
7
2
2 5
4
1 8 2 3
3
3 8 9
5
64 25 75 100 50
1
42
6
96 128 88 80 52 7
5
2 4 8 16 17
outputCopy
5 2
8 2 1 3
9 3 8
100 50 25 75 64
42
128 96 80 88 52 7
17 2 4 8 16
Note
In the main experiment of the model, there are just two potential changes b — [2,5] and [5,2]: for the first c=[2,1], for the second one c=[5,1].
In the third experiment of the model, number 9 ought to be the first in b, and GCD(9,3)=3, GCD(9,8)=1, so the second number of b ought to be 3.
In the seventh experiment of the model, initial four numbers pairwise have a typical divisor (an intensity of two), yet none of them can be the first in the ideal stage b.

### 1 thought on “B. Big Vova SOLUTIONS Codeforces Round #669 (Div. 2)”

1. #include
using namespace std;
typedef long long int ll;
ll power(ll x,ll y,ll m){if(y==0)return 1;ll p=power(x,y/2,m)%m;p=(p*p)%m;return (y%2==0)?p:(x*p)%m;}
ll nCr(ll n,ll r,ll m){if(r>n)return 0;ll a=1,b=1,i;for(i=0;i>t;while(t–)
#define endl 'n'
vector> tree;
vector sw,par;
ll dfs(int node,int p)
{
par[node]=p;
for(int u:tree[node])
{
if(u!=p)
dfs(u,node);
}
}
void solve()
{
int n;
cin >> n;
tree = vector>(n);
sw = par = vector(n);
for(int i=0;i> sw[i];
for(int i=0;i> x >> y;
–x;
–y;
tree[x].push_back(y);
tree[y].push_back(x);
}
par[0]=-1;
dfs(0,-1);
int q;
cin >> q;
q=2*q-1;
while(q–>0)
{
int t;
cin >> t;
if(t==1)
{
int node;
cin >> node;
–node;
ll ans=0;
while(node!=-1)
{
ans+=sw[node];
node=par[node];
}
cout << ans << endl;
}
else
{
ll l,r;
cin >> l >> r;
sw[l-1]=r;
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//Test
solve();
return 0;
}