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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/*
===========================================================
* @Name: 1043F Make It One
* @Author: Shawn
* @create Time: 2020/3/8 11:49:59
* @url: https://codeforces.com/contest/1043/problem/F
* @Description: 容斥原理,组合数比较大,Pascal公式超内存,需要逆元计算组合数
===========================================================
*/
#ifdef ONLINE_JUDGE
#define NDEBUG //ban assert when submit to online judge
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxA = 3e5 + 1;
const ll MOD = 1e9 + 7;
//计算组合数:从a个数中取b个处理有多少种方案(Pascal公式,帕斯卡恒等式)
class CombinationPascal
{
private:
vector<vector<ll>> c;
/**
* 从M个数中取N个处理有多少种方案,动态规划(Pascal公式,帕斯卡恒等式)
* https://zhuanlan.zhihu.com/p/74787475
*/
void InitData(ll M)
{
c[0][0] = 1;
for (ll i = 0; i <= M; ++i)
{
for (ll j = 0; j <= i; ++j)
{
if (j == 0)
c[i][j] = 1;
else if (i == 1)
c[i][j] = 1;
else if (i == j)
c[i][j] = 1;
else
c[i][j] = ((c[i - 1][j - 1] % MOD) + (c[i - 1][j] % MOD) % MOD) % MOD;
}
}
}
public:
CombinationPascal(int m)
{
c.assign(m + 1, vector<ll>(m + 1, 0));
InitData(m);
}
/**
* 从a个数中取b个处理有多少种方案(Pascal公式,帕斯卡恒等式)
*/
ll C(ll a, ll b)
{
if (b > a)
return 0;
else if (b == 0)
return 1;
else if (a == 1)
return 1;
else if (a == b)
return 1;
else
return c[a][b];
}
};
//计算组合数:从M个数中取N个处理有多少种方案(逆元,Modular Multiplicative Inverse)
//https://www.zybuluo.com/ArrowLLL/note/713749
class CombinationMMI
{
private:
vector<ll> factorial;
void calFactorial() //求maxA以内的数的阶乘
{
factorial[0] = 1;
factorial[1] = 1;
for(ll i = 2; i < maxA+1; ++i)
factorial[i] = factorial[i - 1] * i % MOD;
}
//费马小定理求逆元
ll pow(ll a, ll n, ll p) //快速幂 a^n % p
{
ll ans = 1;
while(n)
{
if(n & 1) ans = ans * a % p;
a = a * a % p;
n >>= 1;
}
return ans;
}
ll mmi(ll a, ll b) //费马小定理求逆元
{
return pow(a, b - 2, b);
}
public:
CombinationMMI()
{
factorial.assign(maxA+1,0);
calFactorial();
}
ll C(ll a, ll b) //计算C(a, b)
{
if(b>a)
return 0;
return (factorial[a] * mmi(factorial[b], MOD) % MOD * mmi(factorial[a - b], MOD) % MOD) % MOD;
}
};
void solve()
{
int n;
cin >> n;
//assert(n > 0);
CombinationMMI CMMI;
//CombinationPascal CPAS(n);
vector<int> a(n, 0);
int gcd = 0;
int mx = 0;
for (int i = 0; i <= n - 1; ++i)
{
cin >> a[i];
//++m[a[i]];
//++f[1][a[i]];
gcd = __gcd(gcd, a[i]);
mx = max(mx, a[i]);
}
//vector<vector<int>> f(7 + 1, vector<int>(mx + 1, 0));
vector<int> f2(mx + 1, 0);
vector<int> m(mx, 0);
for (int i = 0; i <= n-1 ; ++i)
++m[a[i]];
for (int i = 1; i <= mx; ++i)
for (int j = i + i; j <= mx; j += i)
m[i] += m[j];
if (gcd > 1)
{
cout << -1 << '\n';
return;
}
for (int i = 1; i <= 7; ++i)
{
for (int j = mx; j >= 1; --j)
{
f2[j] = 1ll * CMMI.C(m[j],i) % MOD;
for (int k = j + j; k <= mx; k += j)
{
f2[j] -= f2[k];
if (f2[j] < 0)
f2[j] += MOD;
}
}
if (f2[1] > 0)
{
cout << i << '\n';
return;
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("CF_1043F_MakeItOne.in", "r", stdin);
//freopen("CF_1043F_MakeItOne.out", "w", stdout);
#endif
solve();
// Test
// int n=100;
// CombinationMMI CMMI;
// CombinationPascal CPAS(n);
// for (int i = 0; i <= n - 1; ++i)
// for (int j = 0; j <= i; ++j)
// cout << i << ":" <<setw(3)<< j << " " << CMMI.C(i,j) << " " <<setw(10)<< CPAS.C(i,j) << "\n";
cout.flush();
return 0;
}
|