from math import sqrt, ceil a, b = map(int, input().strip().split()) if a < b: # a比b还小,肯定无解 print(0) elif a == b: # a=b有无穷多解 print("inf") else: # a>b正常求解 diff = a - b # 先将余数减去 upper_bound = ceil(sqrt(diff)) # x的搜索上界 res = 0 # 遍历x的可能性 for x in range(1, upper_bound + 1): if diff % x == 0: # 在可以整除x的情况下才计算次数 if diff != x**2: # 当diff != x**2时,除数和商可以通过交换位置形成两个解 # 但需要它们两个至少有一个大于b才能增加一种可能的解,如果除数和商都小于等于b,余数是不可能为b的 if x > b: res += 1 if diff // x > b: res += 1 else: # 当diff = x**2时,除数和商相等,最多只能增加一个解,当且仅当它们大于b的时候取得 if x > b: res += 1 print(res)
1. a<b 无解
2. a=b 无穷解
3. a>b 需要求解,首先要明确x的值要大于b,然后我们去求x的所有可能值,再除去小于等于b的值,即可求得答案。x的所有可能值可以通过求a-b的所有因子来获得,求因子的过程中,我们每次循环可以求到两个因子(可能会有相同因子),然后分别判断是否大于b即可。(本题测试用例较弱,没有考虑到相等因子也能过)
#include<bits/stdc++.h> using namespace std; int main(){ int a,b; cin>>a>>b; int ans=0; if(a>b){ int m=a-b; int t=sqrt(a-b); for(int i=1;i<=t;i++){ if(m%i==0){ int temp=m/i; if(i>b) ans++; if(i!=temp&&temp>b) ans++; } } cout<<ans; } else if(a==b) cout<<"inf"; else cout<<0; }