Codeforces Round #554 (Div. 2) C. Neko does Maths

C. Neko does Maths
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher.

Neko has two integers a and b. His goal is to find a non-negative integer k such that the least common multiple of a+k and b+k is the smallest possible. If there are multiple optimal integers k, 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 a and b (1≤a,b≤109).

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

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

Examples
inputCopy
6 10
outputCopy
2
inputCopy
21 31
outputCopy
9
inputCopy
5 10
outputCopy
0
Note
In the first test, one should choose k=2, as the least common multiple of 6+2 and 10+2 is 24, which is the smallest least common multiple possible.

笔记:
1.longlong最大值范围坑,是(1ll<<62)不是0x3f3f3f3f
2.
gcd(a+k,b+k)=gcd(b+k,a-b)
gcd(a+k,b+k)可以其中一个变为常数,从而缩小常数级别因子枚举范围,枚举所有因子就是所有可能情况,取最优。把用k枚举所有情况转换为用常数范围内的因子枚举所有情况。
3.先判断lcm最小,在判断k最小,可以从小到大枚举k,如果后面的lcm更小则更新,否则不跟新,保证后面lcm和前面相等时不更新,保证lcm相等时取前面的k最小值。
4.当k=0时,直接取得gcd(a,b)求lcm,这时需要特盘一下。因为
k=mod-b%mod不可能为0为大于0的数。

#include
using namespace std;
typedef long long ll;
ll ans,k,dif,mod,t,a,b;
ll minx=(1ll<<62);
vector<ll> vec;
int main()
{
    cin >> a >> b;
    dif=abs(a-b);
    for(int i=1;i*i<=dif;i++)
    {
        if(dif%i==0)
        {
            vec.push_back(i);
            if(i*i!=dif)
                vec.push_back(dif/i);
        }
    }
    sort(vec.begin(),vec.end());
    for(int i=0;i<vec.size();i++)
    {
        mod=vec[i];
        //if(a%mod!=b%mod)
       //     continue;
        if(a%mod==0)
        {
            t=(a*b)/__gcd(a,b);
            if(t<minx)
            {
                minx=t;
                ans=0;
            }
        }
        else
        {
            k=mod-b%mod;
            t=(a+k)*(b+k)/__gcd(b+k,dif);
            if(t<minx)
            {
                minx=t;
                ans=k;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

官方题解:

1152C - Неко занимается математикой

The lcm(a+k,b+k) is equal to (a+k)⋅(b+k)gcd(a+k,b+k). In fact, there are not much possible values for the denominator of this fraction.

Without losing generality, let’s assume a≤b. Applying one step of Euclid’s algorithm we can see, that gcd(a+k,b+k)=gcd(b−a,a+k).

So the gcd is a divisor of b−a. Let’s iterate over all divisors q of b−a. That also means that a(modq)=b(modq). In case a(modq)=0, we can use k=0. Otherwise, the corresponding k should be q−a(modq). Lastly, we need to check whether the value of lcm(a+k,b+k) is the smallest found so far.

Also, one has to be careful when dealing with k=0 answer, which was the reason why many solutions got WA 63.

The complexity is O(b−a−−−−√).

#include
#pragma GCC optimize("O3")
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long rand_seed()
{
	long long a = rng();
    return a;
}
long long a, b;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> a >> b;
    long long dif = abs(a - b);
    vector<int>v;
    for(int i = 1; i * i <= dif; ++i)
    {
        if(dif % i == 0)
        {
            v.push_back(i);
            if(i * i != dif)
                v.push_back(dif / i);
        }
    }
    long long ans = (1LL<<62);
    int vl = 0;
    sort(v.begin(), v.end());
    for(int i = 0; i < v.size(); ++i)
    {
        int nr = v[i];
        if(a % nr != b % nr)
            continue;
        if(a % nr == 0)
        {
            long long pans = (a * b)/__gcd(a, b);
            if(pans < ans)
                ans = pans, vl = 0;
        }
        else
        {
            long long pans = ((nr - a % nr + a) * (nr - b % nr + b))/__gcd((nr - a % nr + a), (nr - b % nr + b));
            if(pans < ans)
                ans = pans, vl = nr - a % nr;
        }
    }
    cout << vl;
    return 0;
}

你可能感兴趣的:(算法竞赛)