मुख्य कंटेंट तक स्किप करें

Divide and Conquer

Madhav
EditReport

Description:

You are given two positive integers xx and yy.

You are allowed to perform the following operation any number of times (possibly zero):

  • Choose any positive integer zz such that zz divides xx.
  • Set x:=xzx := \frac{x}{z}.

Determine whether you can make xx exactly equal to yy using this operation.

Input The first line of the input contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases. The description of each test case follows.

The only line of each test case contains two space-separated integers xx and yy (1x,y1001 \le x, y \le 100).

Output For each test case, print "YES" if you can make xx exactly equal to yy and "NO" otherwise.

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).


Approaches:

1. Optimal Math Approach

The operation replaces xx with x/zx / z where zz is a divisor of xx. This essentially means we can divide xx by any of its factors to reach a smaller integer.

  1. Observation: For xx to become exactly yy through division, yy must be a factor of xx. In other words, xx must be perfectly divisible by yy.
  2. Algorithm: We just need to check if x(mody)==0x \pmod y == 0. If it is, the answer is "YES" (we can achieve this by performing valid divisions). If xx is not a multiple of yy, it is mathematically impossible to reach yy, so the answer is "NO".

Complexity

  • Time Complexity: O(1)O(1) per testcase. We only perform a single modulo operation, which takes constant time.
  • Space Complexity: O(1)O(1) as no extra memory is needed beyond storing the two variables.

Solutions:

C++

#include <iostream>

using namespace std;

void solve() {
int x, y;
cin >> x >> y;

if (x % y == 0) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}

int main() {
// Fast I/O
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}

Java

import java.util.Scanner;

public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()) {
int t = scanner.nextInt();
while (t-- > 0) {
solve(scanner);
}
}
scanner.close();
}

private static void solve(Scanner scanner) {
int x = scanner.nextInt();
int y = scanner.nextInt();

if (x % y == 0) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}

Python

import sys

def solve():
input_data = sys.stdin.read().split()
if not input_data:
return

t = int(input_data[0])
index = 1

results = []
for _ in range(t):
x = int(input_data[index])
y = int(input_data[index+1])
index += 2

if x % y == 0:
results.append("YES")
else:
results.append("NO")
print('\n'.join(results))

if __name__ == "__main__":
solve()

JavaScript

const fs = require('fs');

function main() {
const input = fs.readFileSync(0, 'utf-8').trim().split(/\s+/);
if (input.length === 0 || input[0] === '') return;

let t = parseInt(input[0], 10);
let index = 1;
const results = [];

for (let i = 0; i < t; i++) {
let x = parseInt(input[index++], 10);
let y = parseInt(input[index++], 10);

if (x % y === 0) {
results.push("YES");
} else {
results.push("NO");
}
}
console.log(results.join('\n'));
}

main();
Telemetry Integration

Completed working through this block? Sync progress to workspace.