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

举报
小哈里 发表于 2022/05/10 23:52:04 2022/05/10
【摘要】 problem B. Nastia and a Good Array time limit per test2 seconds memory limit per test256 megabytes in...

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

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。