-
Notifications
You must be signed in to change notification settings - Fork 59
/
autobuze.cpp
69 lines (69 loc) · 1.33 KB
/
autobuze.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
# include <fstream>
# include <algorithm>
# include <map>
# include <vector>
using namespace std;
ifstream f("autobuze.in");
ofstream g("autobuze.out");
vector <int> v[50005];
int x,n,sol,maxx,nr,VV,var;
int a[50005];
bool ap[50005];
void DFS (int k)
{
ap[k]=1;
for (int i=0; i<v[k].size(); ++i)
{
if (! ap[v[k][i]]) DFS (v[k][i]);
}
}
int main ()
{
map <int,int> m;
int i,j;
f>>n;
for (i=1; i<=n; ++i)
{
f>>a[i];
m.insert(std::pair<int,int>(a[i],i));
maxx=max(maxx, a[i]);
}
sol=n;
for (i=1; i<=n; ++i)
{
if (maxx/a[i]>=n)
{
for (j=1; j<=n; ++j)
{
if (a[i]%a[j]==0 || a[j]%a[i]==0)
{
v[i].push_back(j);
v[j].push_back(i);
}
}
}
else
{
nr=a[i];
for (j=2; j<=n && nr<=maxx; ++j)
{
nr+=a[i];
if (m.find(nr)!=m.end())
{
v[i].push_back(m.find(nr)->second);
v[m.find(nr)->second].push_back(i);
}
}
}
}
for (i=1; i<=n; ++i)
{
if (! ap[i])
{
++VV;
DFS (i);
}
}
g<<VV<<"\n";
return 0;
}