Codeforces Round #720 (Div. 2), B. Nastia and a Good Array

Posted 小哈里

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #720 (Div. 2), B. Nastia and a Good Array相关的知识,希望对你有一定的参考价值。

problem

B. Nastia and a Good Array
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Nastia has received an array of n positive integers as a gift.

She calls such an array a good that for all i (2≤i≤n) takes place gcd(ai−1,ai)=1, where gcd(u,v) denotes the greatest common divisor (GCD) of integers u and v.

You can perform the operation: select two different indices i,j (1≤i,j≤n, i≠j) and two integers x,y (1≤x,y≤2⋅109) so that min(ai,aj)=min(x,y). Then change ai to x and aj to y.

The girl asks you to make the array good using at most n operations.

It can be proven that this is always possible.

Input
The first line contains a single integer t (1≤t≤10000) — the number of test cases.

The first line of each test case contains a single integer n (1≤n≤105) — the length of the array.

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — the array which Nastia has received as a gift.

It’s guaranteed that the sum of n in one test doesn’t exceed 2⋅105.

Output
For each of t test cases print a single integer k (0≤k≤n) — the number of operations. You don’t need to minimize this number.

In each of the next k lines print 4 integers i, j, x, y (1≤i≠j≤n, 1≤x,y≤2⋅109) so that min(ai,aj)=min(x,y) — in this manner you replace ai with x and aj with y.

If there are multiple answers, print any.

Example
inputCopy
2
5
9 6 3 11 15
3
7 5 13
outputCopy
2
1 5 11 9
2 5 7 6
0
Note
Consider the first test case.

Initially a=[9,6,3,11,15].

In the first operation replace a1 with 11 and a5 with 9. It’s valid, because min(a1,a5)=min(11,9)=9.

After this a=[11,6,3,11,9].

In the second operation replace a2 with 7 and a5 with 6. It’s valid, because min(a2,a5)=min(7,6)=6.

After this a=[11,7,3,11,6] — a good array.

In the second test case, the initial array is already good.

solution

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5+10;
LL gcd(LL a, LL b){ return b==0 ? a : gcd(b,a%b); }
int a[maxn];
int main(){
	ios::sync_with_stdio(false);
	int T;  cin>>T;
	while(T--){
		int n;  cin>>n;
		int mi = 1, ok = 1;
		cin>>a[1];
		for(int i = 2; i <= n; i++){
			cin>>a[i];
			if(a[i]<a[mi])mi = i;
			if(gcd(a[i],a[i-1])!=1)ok = 0;
		}
		if(ok==1){cout<<"0\\n"; continue;}
		
		cout<<n-1<<"\\n";
		int cc = 1;
		for(int i = mi+1; i<= n; i++){
			cout<<mi<<" "<<i<<" "<<a[mi]<<" "<<a[mi]+cc<<"\\n";
			cc++;
		}
		cc = 1;
		for(int i = mi-1; i >= 1;i--){
			cout<<mi<<" "<<i<<" "<<a[mi]<<" "<<a[mi]+cc<<"\\n";
			cc++;
		}
	}
	
	return 0;
}


以上是关于Codeforces Round #720 (Div. 2), B. Nastia and a Good Array的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #720 (Div. 2)Codeforces-1521ABC

Codeforces Round #720 (Div. 2) ABCDE题解

Codeforces Round #720 (div2) AB

Codeforces Round #720 (Div. 2), B. Nastia and a Good Array

Codeforces Round #720 (Div. 2) A. Nastia and Nearly Good Num

Codeforces Round #720 (Div. 2) A题解