C. Neko does Maths(Gcd+数论)

C. Neko does Maths

 

思路:

x%k-y%k=0;

所以:abs(x-y)%k=0;

考虑y%x==0的情况,则lcm(x,y)=y(假设y最大),所以k  = 0.

如果y%x!=0,假定y>x,(y-x)%k=0;

因为Gcd(x,y)=Gcd(y-x,x)=Gcd(y-x,y);

所以在这道题中y-x已经确定,只要找到满足条件的t即可,已知所有的k一定是y-x的因子。

求出所有k的因子,凑出(x+t)%k==0,t = x%t,因为要满足x>0,所以t = x-x%t.

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
using namespace std;
typedef long long LL;
vector <LL> vc;
LL Gcd(LL x,LL y){
	return y==0?x:Gcd(y,x%y); 
}
LL lcm(LL x,LL y){
	return x*y/Gcd(x,y);
}
void SP(LL &x,LL &y){
	LL tp = x;
	x = y;
	y = tp;
}
void get_f(LL x){
	vc.clear();
	for(LL i=1;i<=sqrt(x);i++)
	if(x%i==0){
		vc.push_back(i);
		if(i*i!=x) vc.push_back(x/i);
	}
}
int main(void)
{
	LL x,y;
	scanf("%lld%lld",&x,&y);
	if(x>y) SP(x,y);
	if(y%x==0){
		printf("0\n");
		return 0;
	}
	LL cha = y-x;
	get_f(cha);
	int len = vc.size();
	LL ans = 0,tmp = 1e18+10;
	for(int i=0;i<len;i++){
		LL tt = 0;
		if(x%vc[i]){
			tt = vc[i]-x%vc[i];
		}
		//printf("%lld . ",tt);
		LL tp = lcm(x+tt,y+tt);
		if(tp<tmp){
			tmp = tp;
			ans = tt;
		}
	}
	printf("%lld\n",ans);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值