Divide and Conquer
Description:â
You are given two positive integers and .
You are allowed to perform the following operation any number of times (possibly zero):
- Choose any positive integer such that divides .
- Set .
Determine whether you can make exactly equal to using this operation.
Input The first line of the input contains a single integer () â the number of test cases. The description of each test case follows.
The only line of each test case contains two space-separated integers and ().
Output For each test case, print "YES" if you can make exactly equal to 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 with where is a divisor of . This essentially means we can divide by any of its factors to reach a smaller integer.
- Observation: For to become exactly through division, must be a factor of . In other words, must be perfectly divisible by .
- Algorithm: We just need to check if . If it is, the answer is "YES" (we can achieve this by performing valid divisions). If is not a multiple of , it is mathematically impossible to reach , so the answer is "NO".
Complexityâ
- Time Complexity: per testcase. We only perform a single modulo operation, which takes constant time.
- Space Complexity: as no extra memory is needed beyond storing the two variables.
Solutionsâ
- C++
- Java
- Python
- JavaScript
#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;
}
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");
}
}
}
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()
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();
Done with this topic? Mark it as complete to track your progress.
Related Practice Problems
Handpicked problems sharing similar algorithmic topic tags
Bigrams
Solution for Codeforces 2242A: Bigrams, utilizing a greedy frequency counting approach.
Crimson Triples
Solution for Codeforces 2238B: Crimson Triples, utilizing mathematical observations and a greedy approach.
Iskander and Drawings
Solution for Codeforces 2244A: Iskander and Drawings, calculating the maximum time to erase a line.
đŦ Discuss this page
Have a question or spot something confusing in "Divide and Conquer"? Ask below â it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.