├── Code(Python).py ├── Problem Explanation.txt ├── Code(C++).cpp ├── Code(Java).java ├── Time complexity.txt └── Algorithm.txt /Code(Python).py: -------------------------------------------------------------------------------- 1 | def hel2(x,y): 2 | if x == y: 3 | return 0 4 | l = [] 5 | for i in range(1,int(x**0.5)+1): 6 | if x%i == 0: 7 | l.append(i) 8 | l.append(x//i) 9 | if i*i == x: 10 | l.pop() 11 | l.sort(reverse=True) 12 | #print(l) 13 | cnt = 0 14 | prev = x 15 | for i in range(1,len(l)): 16 | if prev%l[i] == 0 and l[i]%y == 0: 17 | cnt += prev//l[i] 18 | #print(l[i]) 19 | #print(cnt,prev) 20 | prev = l[i] 21 | if l[i] == y: 22 | break 23 | return cnt 24 | 25 | def gcd(x,y): 26 | if x%y == 0: 27 | return y 28 | return gcd(y,x%y) 29 | 30 | def fun(n,u,v): 31 | g = gcd(u,v) 32 | res1 = hel2(u,g) 33 | #print("res1 " + str(res1)) 34 | res2 = hel2(v,g) 35 | #print("res2 " + str(res2)) 36 | res = res1 + res2 37 | return res 38 | 39 | for _ in range(int(input())): 40 | n,q = map(int,input().split()) 41 | for _ in range(q): 42 | u,v = map(int,input().split()) 43 | print(fun(n,u,v)) 44 | -------------------------------------------------------------------------------- /Problem Explanation.txt: -------------------------------------------------------------------------------- 1 | The problem involves an undirected graph with weighted edges. The graph has N vertices numbered from 1 to N. For any pair of vertices u and v (where u ≠ v), there is an edge between u and v if and only if u divides v. The weight of this edge is denoted by u/v. 2 | 3 | The input consists of T test cases. Each test case begins with a line containing two integers N and Q, where N is the number of vertices in the graph and Q is the number of queries to be answered. Following that, there are Q lines, each containing two integers u and v representing a query. 4 | 5 | The goal is to find the minimum distance between the vertices u and v for each query. 6 | 7 | To solve this problem, you need to perform the following steps: 8 | 9 | 1. Read the number of test cases, T. 10 | 11 | 2. For each test case, do the following: 12 | a. Read the values of N and Q. 13 | b. Create the graph with N vertices and weighted edges according to the given conditions. 14 | c. Perform the following steps for each query: 15 | - Read the values of u and v. 16 | - Implement a shortest path algorithm (such as Dijkstra's algorithm or Breadth-First Search) to find the minimum distance between u and v in the graph. 17 | - Print the minimum distance. 18 | 19 | 4. Repeat steps 2b-2c for each test case. 20 | 21 | 5. End the program after processing all test cases. 22 | 23 | The output should be the minimum distances for each query in each test case. 24 | 25 | Note: You can optimize the creation of the graph by precomputing the weights for each pair of vertices before processing the queries. -------------------------------------------------------------------------------- /Code(C++).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | ll gcd(ll a, ll b) 5 | { 6 | if((a%b)==0){ 7 | return b; 8 | }else{ 9 | return gcd(b, a%b); 10 | } 11 | } 12 | ll count(ll a, ll p[]) 13 | { 14 | ll ans=0; 15 | while(a>1) 16 | { 17 | ans+=p[a]; 18 | a=a/p[a]; 19 | } 20 | return ans; 21 | } 22 | int main() 23 | { 24 | ios_base::sync_with_stdio(false); 25 | cin.tie(NULL); 26 | cout.tie(NULL); 27 | 28 | ll t, n, i, j, ans, q, u, v, lcm, a, b; 29 | ll p[100001]; 30 | 31 | for(i=0; i<=100000; i++) 32 | { 33 | p[i]=i; 34 | } 35 | 36 | 37 | for(i=2; i<=100000; i++) 38 | { 39 | if(p[i]==i){ 40 | for(j=i*i; j<=100000; j+=i) 41 | { 42 | if(p[j]==j){ 43 | p[j]=i; 44 | } 45 | } 46 | } 47 | } 48 | 49 | cin>>t; 50 | 51 | for(;t--;) 52 | { 53 | cin>>n>>q; 54 | 55 | for(;q--;) 56 | { 57 | cin>>a>>b; 58 | 59 | if(a>b){ 60 | swap(a, b); 61 | } 62 | 63 | if((b%a)==0){ 64 | ans=count(b/a, p); 65 | //cout<0){ 14 | int n=sc.nextInt(); 15 | int q=sc.nextInt(); 16 | for(int i=0;i1) 60 | ans=ans+n; 61 | return ans; 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /Time complexity.txt: -------------------------------------------------------------------------------- 1 | let's break it down into its main components: 2 | 3 | 1. The `hel2` function: 4 | - The loop from 1 to the square root of `x` iterates O(sqrt(x)) times. 5 | - Inside the loop, the conditions `x % i == 0` and `l.pop()` take constant time. 6 | - Sorting the list `l` takes O(sqrt(x) log(sqrt(x))) time. 7 | - The second loop from 1 to the length of `l` also iterates O(sqrt(x)) times. 8 | - Inside this loop, the conditions and operations take constant time. 9 | - Therefore, the overall time complexity of the `hel2` function is O(sqrt(x) log(sqrt(x))). 10 | 11 | 2. The `gcd` function: 12 | - The Euclidean algorithm for calculating the GCD of `x` and `y` takes O(log(min(x, y))) time. 13 | - Therefore, the time complexity of the `gcd` function is O(log(min(x, y))). 14 | 15 | 3. The `fun` function: 16 | - The time complexity of calling `hel2` twice is dominated by the largest input value passed to it, which is the GCD of `u` and `v`. 17 | - Therefore, the time complexity of the `fun` function is determined by the time complexity of the `hel2` function, which is O(sqrt(max(u, v)) log(sqrt(max(u, v)))). 18 | 19 | 4. The main loop: 20 | - The loop that iterates over the test cases runs T times. 21 | - For each test case, the loop that iterates over the queries runs q times. 22 | - The overall time complexity of the main loop is O(T * q). 23 | 24 | Considering all the components, the total time complexity of the code can be approximated as follows: 25 | 26 | O(T * q * sqrt(max(u, v)) log(sqrt(max(u, v)))) 27 | 28 | It's important to note that this analysis assumes that the operations inside the loops and function calls take constant time. If the values of `n`, `u`, and `v` become significantly large, the actual time complexity might be higher due to the calculations involved in the loops and function calls. -------------------------------------------------------------------------------- /Algorithm.txt: -------------------------------------------------------------------------------- 1 | Let's break down the code and explain its algorithm step by step: 2 | 3 | 1. The function `hel2(x, y)` is defined to calculate the minimum distance between two vertices `x` and `y` in the graph. This function first checks if `x` is equal to `y`, in which case the minimum distance is 0. 4 | 5 | 2. Next, an empty list `l` is initialized to store the divisors of `x`. The loop iterates from 1 to the square root of `x` (inclusive) and checks if `i` is a divisor of `x` using the condition `x % i == 0`. If `i` is a divisor, both `i` and `x // i` (the other divisor) are added to the list `l`. However, if `i` is the square root of `x`, it is added twice, so the duplicate entry is removed using `l.pop()`. 6 | 7 | 3. The list `l` is sorted in reverse order. This step is not necessary for finding the minimum distance, but it might be used for some other purposes. 8 | 9 | 4. The variable `cnt` is initialized to 0, which will keep track of the minimum distance. The variable `prev` is set to `x` initially. 10 | 11 | 5. The loop iterates through the elements of `l` from the second element (`i = 1`) to the end. For each element, it checks if `prev` is divisible by `l[i]` and `l[i]` is divisible by `y`. If both conditions are true, it adds `prev // l[i]` to `cnt`, updates `prev` to `l[i]`, and checks if `l[i]` is equal to `y`. If `l[i]` is equal to `y`, it breaks the loop since the minimum distance is found. 12 | 13 | 6. Finally, the function returns the value of `cnt`, which represents the minimum distance between `x` and `y` in the graph. 14 | 15 | 7. The function `gcd(x, y)` calculates the greatest common divisor between `x` and `y` using the Euclidean algorithm. It returns the GCD value. 16 | 17 | 8. The function `fun(n, u, v)` takes three parameters: `n` (number of vertices), `u` (query vertex), and `v` (query vertex). It calculates the GCD of `u` and `v` using the `gcd` function. Then, it calls the `hel2` function twice, passing `u` and the GCD as arguments in the first call, and `v` and the GCD as arguments in the second call. The results are stored in `res1` and `res2`, respectively. Finally, it adds `res1` and `res2` together and returns the result. 18 | 19 | 9. The main code starts by reading the number of test cases, `T`, from the input. Then, it enters a loop that iterates `T` times. 20 | 21 | 10. For each test case, it reads the values of `n` and `q` (number of vertices and number of queries) from the input. 22 | 23 | 11. Inside the nested loop that iterates `q` times, it reads the values of `u` and `v` (vertices corresponding to the query) from the input. 24 | 25 | 12. It calls the `fun` function, passing `n`, `u`, and `v` as arguments, and prints the result. 26 | 27 | The overall algorithm seems to calculate the minimum distance between vertices `u` and `v` in the given graph based on the provided conditions and the concept of --------------------------------------------------------------------------------