Cho A, B, đếm số giá trị X thỏa mãn A % X == B.
Ví dụ: A=26, B=2 -> X ∈ {3,4,6,8,12,24} -> 6 nghiệm.
Thuật toán
possibleWayCount(a,b)
NẾU a==b: VÔ SỐ NGHIỆM
NẾU ab: TÌM ƯỚC CỦA (a-b) LỚN HƠN b
Chương trình C++ ví dụ
#include <iostream>
#include <cmath>
using namespace std;
int findDivisors(int A, int B) {
int N = A-B, cnt=0;
for (int i=1;i*i<=N;i++) if (N%i==0) {
if (i>B) cnt++;
if (N/i!=i && N/i>B) cnt++;
}
return cnt;
}
int possibleWayCount(int A, int B) {
if (A==B) return -1;
if (A
Kết quả
For A: 26 and B: 2, X can take 6 values