codeforces 1152C C. Neko does Maths

 

题目:https://codeforces.com/contest/1152/problem/C

Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher.

Neko has two integers aa and bb. His goal is to find a non-negative integer kk such that the least common multiple of a+ka+k and b+kb+k is the smallest possible. If there are multiple optimal integers kk, he needs to choose the smallest one.

Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it?

Input

The only line contains two integers aa and bb (1≤a,b≤1091≤a,b≤109).

Output

Print the smallest non-negative integer kk (k≥0k≥0) such that the lowest common multiple of a+ka+k and b+kb+k is the smallest possible.

If there are many possible integers kk giving the same value of the least common multiple, print the smallest one.

Examples

input

Copy

6 10

output

Copy

2

input

Copy

21 31

output

Copy

9

input

Copy

5 10

output

Copy

0

大意:求当(a+k)与(b+k)的lcm最小时,k的值

若对统一lcm有多个k,输出k的最小值。

思路:

 lcm(a+k,b+k)=\frac{(a+k)*(b+k)}{gcd(a+k,b+k)}=\frac{(a+k)*(b+k)}{gcd(a+k,b-a)}

因此我们暴力枚举b-a的因子(实际上是暴力枚举a+k与b-a的最大公因数),比较lcm的值同时记录lcm、k即可

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll fac[200005];
int main()
{
    ll n,m,i,j,k,ans,num,sum,a,b,c,tot,lcm;
    scanf("%lld%lld",&a,&b);
    if(a>b) swap(a,b);
    c=b-a,tot=0;
    for(i=1;i<=sqrt(c);i++) //因子
    if(c%i==0)  fac[tot++]=i,fac[tot++]=c/i;
    if(!tot)
    {
        printf("0\n");
        return 0;
    }
    ans=0x3f3f3f3f3f3f3f3f;
    for(i=0;i<tot;i++)
    {
        num=(a%fac[i]==0?0:(a/fac[i]+1)*fac[i]-a);
        lcm=(a+num)*(b+num)/fac[i];
        if(lcm<ans) k=num,ans=lcm;
        else if(lcm==ans&&num<k) k=num; 
    }
    printf("%lld\n",k);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值