Skip to content

Commit f95b584

Browse files
Path Through Graph
1 parent 40c37ca commit f95b584

File tree

2 files changed

+264
-0
lines changed

2 files changed

+264
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
3+
Path Through Graph
4+
Problem Description
5+
You are given two natural numbers. Imagine these natural numbers as nodes on a graph. On this graph, a number is connected to its largest factor other than itself. You have to find the shortest path between them and print the number of edges on that path.
6+
7+
If the two numbers do not have any common factor, then construct a path through 1. For better understanding refer to the examples below:
8+
9+
Example 1: Input numbers: 2 4
10+
The numbers are directly connected as follows on the graph. 2 is the largest factor of 4, other than itself.
11+
We can also see that there is only on edge between them.
12+
4 <--> 2
13+
Hence the number of edges in shortest path is 1.
14+
Output: 1
15+
16+
Example 2: Input numbers: 18 19
17+
The graph for number 18 and 19 will look like this. Here we have 4 edges in the path.
18+
18 <--> 9 <--> 3 <--> 1 <--> 19
19+
Output: 4
20+
21+
Example 3: Input numbers: 9 9
22+
The number of edges in shortest path is zero since the numbers correspond to the same node.
23+
Output: 0
24+
25+
Constraints
26+
0 < M, N <= 10 ^ 9
27+
28+
Input
29+
Single line containing two space separated integers M, N
30+
31+
Output
32+
Number of edges in the shortest path.
33+
34+
Time Limit
35+
1
36+
37+
Examples
38+
Example 1
39+
40+
Input
41+
42+
15689 28
43+
44+
Output
45+
46+
5
47+
48+
Explanation :
49+
50+
The graph for number 15689 and 28 will look like this.
51+
52+
Since we know that largest factor of 15689 other than itself is 541.
53+
54+
Since 541 is a prime number, it’s largest factor other than itself is 1.
55+
56+
For number 28, it’s largest factor other than itself is 14.
57+
58+
Largest factor of 14, other than itself is 7.
59+
60+
Since 7 is a prime number, it’s largest factor other than itself is 1.
61+
62+
So, the graph will look like this:
63+
64+
15689 <--> 541 <--> 1 <--> 7 <--> 14 <--> 28
65+
66+
Since there are 5 edges in this graph, output will be 5.
67+
68+
Example 2
69+
70+
Input
71+
72+
16 4
73+
74+
Output
75+
76+
2
77+
78+
Explanation :
79+
80+
The graph for number 16 and 4 will look like this.
81+
82+
Since we know that largest factor of 16 other than itself is 8.
83+
84+
Largest factor of 8 other than itself is 4. That’s the other input number, so we will stop here.
85+
86+
So, the graph will look like this:
87+
88+
16<-->8<-->4
89+
90+
Since there are 2 edges in this graph, output will be 2.
91+
92+
93+
*/
94+
95+
96+
#include <bits/stdc++.h>
97+
using namespace std;
98+
99+
#define int long long
100+
#define endl '\n'
101+
102+
int get(int x)
103+
{
104+
for(int i=2;i*i<=x;i++)
105+
{
106+
if(x%i==0)return x/i;
107+
}
108+
return 1;
109+
}
110+
111+
void sol()
112+
{
113+
int x,y;
114+
cin>>x>>y;
115+
if(x<y)swap(x,y);
116+
if(x==y){cout<<0;return;}
117+
map<int,int> m;
118+
119+
int c=0;
120+
while(x!=1)
121+
{
122+
c++;
123+
x=get(x);
124+
m[x]=c;
125+
}
126+
c=0;
127+
while(!m.count(y))
128+
{
129+
c++;
130+
y=get(y);
131+
}
132+
cout<<c+m[y];
133+
}
134+
int32_t main()
135+
{
136+
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
137+
sol();
138+
return 0;
139+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
'''
2+
Path Through Graph
3+
Problem Description
4+
You are given two natural numbers. Imagine these natural numbers as nodes on a graph. On this graph, a number is connected to its largest factor other than itself. You have to find the shortest path between them and print the number of edges on that path.
5+
6+
If the two numbers do not have any common factor, then construct a path through 1. For better understanding refer to the examples below:
7+
8+
Example 1: Input numbers: 2 4
9+
The numbers are directly connected as follows on the graph. 2 is the largest factor of 4, other than itself.
10+
We can also see that there is only on edge between them.
11+
4 <--> 2
12+
Hence the number of edges in shortest path is 1.
13+
Output: 1
14+
15+
Example 2: Input numbers: 18 19
16+
The graph for number 18 and 19 will look like this. Here we have 4 edges in the path.
17+
18 <--> 9 <--> 3 <--> 1 <--> 19
18+
Output: 4
19+
20+
Example 3: Input numbers: 9 9
21+
The number of edges in shortest path is zero since the numbers correspond to the same node.
22+
Output: 0
23+
24+
Constraints
25+
0 < M, N <= 10 ^ 9
26+
27+
Input
28+
Single line containing two space separated integers M, N
29+
30+
Output
31+
Number of edges in the shortest path.
32+
33+
Time Limit
34+
1
35+
36+
Examples
37+
Example 1
38+
39+
Input
40+
41+
15689 28
42+
43+
Output
44+
45+
5
46+
47+
Explanation :
48+
49+
The graph for number 15689 and 28 will look like this.
50+
51+
Since we know that largest factor of 15689 other than itself is 541.
52+
53+
Since 541 is a prime number, it’s largest factor other than itself is 1.
54+
55+
For number 28, it’s largest factor other than itself is 14.
56+
57+
Largest factor of 14, other than itself is 7.
58+
59+
Since 7 is a prime number, it’s largest factor other than itself is 1.
60+
61+
So, the graph will look like this:
62+
63+
15689 <--> 541 <--> 1 <--> 7 <--> 14 <--> 28
64+
65+
Since there are 5 edges in this graph, output will be 5.
66+
67+
Example 2
68+
69+
Input
70+
71+
16 4
72+
73+
Output
74+
75+
2
76+
77+
Explanation :
78+
79+
The graph for number 16 and 4 will look like this.
80+
81+
Since we know that largest factor of 16 other than itself is 8.
82+
83+
Largest factor of 8 other than itself is 4. That’s the other input number, so we will stop here.
84+
85+
So, the graph will look like this:
86+
87+
16<-->8<-->4
88+
89+
Since there are 2 edges in this graph, output will be 2.
90+
91+
'''
92+
from collections import defaultdict
93+
def greatest_divisor_n(n):
94+
if n % 2 == 0:
95+
return n//2
96+
else:
97+
i = 3
98+
while i * i <= n:
99+
if n % i == 0:
100+
return n//i
101+
i = i + 2
102+
return 1
103+
def path_of_graph(var1,var2):
104+
map_d=defaultdict(int)
105+
if var1==var2:
106+
return 0
107+
count=0
108+
# swap
109+
if var1<var2:
110+
temp=var1
111+
var1=var2
112+
var2=temp
113+
count=0
114+
while var1!=1:
115+
var1=greatest_divisor_n(var1)
116+
count+=1
117+
map_d[var1]=count
118+
count=0
119+
while not map_d[var2]:
120+
var2=greatest_divisor_n(var2)
121+
count+=1
122+
return map_d[var2]+count
123+
124+
var1,var2=map(int,input().split())
125+
print(path_of_graph(var1,var2))

0 commit comments

Comments
 (0)