-
Notifications
You must be signed in to change notification settings - Fork 1
/
arrgraph.cpp
92 lines (90 loc) · 1.27 KB
/
arrgraph.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<bits/stdc++.h>
#define mod 1000000007
#define ll long long
using namespace std;
bool prime(int a,int b)
{
int i=2;
for(i=2;i<=min(a,b);i++)
{
if((a%i==0)&&(b%i==0))
return false;
}
return true;
}
bool trial(int arr[],int n)
{
int i,j;
vector<int> v;
int p=0;
v.push_back(arr[0]);
int d[n]={0};
d[0]=1;
while(p<v.size())
{
for(j=0;j<n;j++)
{
if(d[j]==0)
{
if(prime(v[p],arr[j]))
{
d[j]=1;
v.push_back(arr[j]);
}
}
}
p++;
}
if(v.size()==n)
return true;
return false;
}
main()
{
int t,i,j,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
if(trial(arr,n))
{
printf("0\n");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
printf("\n");
}
else
{
int c=0;
for(i=0;i<n;i++)
if(arr[i]==47)
c++;
if(c>=1&&c<n)
{
printf("0\n");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
printf("\n");
}
else if(c==0)
{
printf("1\n");
arr[0]=47;
for(i=0;i<n;i++)
printf("%d ",arr[i]);
printf("\n");
}
else
{
printf("1\n");
arr[0]=2;
for(i=0;i<n;i++)
printf("%d ",arr[i]);
printf("\n");
}
}
}
}