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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。
原文链接:gwj1314.blog.csdn.net/article/details/117401306
- 点赞
- 收藏
- 关注作者
评论(0)