去年做这题时,我对BigInteger的用法还不太熟练,但是知道该使用这个,一直以为这题结果就是A*B,觉得是这道题让自己止步省二,如今算法水平已经提升了很多,再一次看这道题,发现没那么简单,终于算是释怀了

好了,讲讲我现在对这题的理解

既然N+A是B的倍数,N+B是A的倍数,那么我们就设N+A=K*B,那么N=K*B-A,把它带入到N+B中去,得到K*B-A+B也就是(K+1)*B-A是A的倍数,那么减去的A由于已经是A的倍数,就可以舍掉,就只用去研究(K+1)*B,既然这个是A的倍数,那么这个式子最小应该就是A与B的最小公倍数,而A与B的最小公倍数我们是可以求出来的, 然后拿最小公倍数除以B减去1,就得到了K,然后把这个K拿到N=K*B-A中求出N即可


import java.math.BigInteger;
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		BigInteger a=new BigInteger("20250412");
		BigInteger b=new BigInteger("20240413");
		BigInteger res=lcm(a,b);
		res=(res.divide(b).subtract(BigInteger.ONE)).multiply(b).subtract(a);
		System.out.println(res);
		

	}
	public static BigInteger lcm(BigInteger a,BigInteger b) {
		BigInteger l=a;
		BigInteger r=b;
		BigInteger gcd=l.gcd(r);
		return l.multiply(r).divide(gcd);
	}

}