zoukankan      html  css  js  c++  java
  • Codeforces C.Neko does Maths

    题目描述:

    C. Neko does Maths
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 (1a,b1091≤a,b≤109).

    Output

    Print the smallest non-negative integer kk (k0k≥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
    Note

    In the first test, one should choose k=2k=2, as the least common multiple of 6+26+2 and 10+210+2 is 2424, which is the smallest least common multiple possible.

    思路:

    刚开始拿到题:首先看了看1秒限时,时间快完了,我就抱着试一试的心态,用欧几里得求最大公约数的方法,把k从0一直循环到1000000(大概一秒钟)来求最大值,他时间要是够长我就一直算下去 ,看能够算到那一个测试点。

    结束后:试着推一推。

    设a=x1*g,b=x2*g;(g为a和b的最大公因数,x1,x2为系数)。a'=x1*g+k,b'=x2*g+k,则a'-b'=a-b=g(x1-x2);

    因为a-b是个定值,现在题目要求的是将a和b一起加上某个数值后的公倍数最小。又因为gcd(a',b')=gcd(a'-b',a')=gcd(a-b,b')(证明提示见上方的式子)。

    也就是说,现在我们要求的是(a'*b')/gcd(a',b')=(a'*b')/gcd(b-a,b');要这个式子最小,怎么办?

    已知的是b-a的值,目标式的分母是b-a和b'的最大公因数,那么也是b-a的因数,因为b-a的因数有限,可以枚举出,那么对于每一个因数i,就设它是gcd(b-a,b'),就可以找到相应的b',即让i成为b-a和b'的最大公因数。可以求出k值,也就是b加上k能够使i成为其因数,有了k值就有了目标式的所有值,求出答案。小心数据范围和求因数时选择根号将时间减半以避免超时,还有就是一种特殊情况需要单独讨论,a-b=0时,上面的过程中会出现被0除的错误。

    知识点:gcd

    代码:

     1 #include <iostream>
     2 #include <cmath>
     3 using namespace std;
     4 long long a;
     5 long long b;
     6 long long ab;
     7 long long k;
     8 long long gcd(long long a,long long b)
     9 {
    10     long long r = a%b;
    11     if(r==0) return b;
    12     return gcd(b,r);
    13 }
    14 int main()
    15 {
    16     cin >> a >> b;
    17     if(a<b) swap(a,b);
    18     ab = a-b;
    19     //cout << "ab " << ab << endl;
    20     long long m = a*b/gcd(a,b);
    21     long long p = 0;
    22     if(ab!=0)
    23     {
    24         for(int i = 1; i<=sqrt(ab)+1; i++)
    25         {
    26             if(ab%i==0)
    27             {
    28                 //cout << i << endl;
    29                 k = i-a%i;
    30                 //cout << "k " << k << endl;
    31                 long long nm = (a+k)*(b+k)/gcd(a+k,b+k);
    32                 if(nm<m)
    33                 {
    34                     m = nm;
    35                     p = k;
    36 
    37                 }
    38                 k = ab/i-a%(ab/i);
    39                 //cout << "k" << endl;
    40                 nm = (a+k)*(b+k)/gcd(a+k,b+k);
    41                 if(nm<m)
    42                 {
    43                     m = nm;
    44                     p = k;
    45 
    46                 }
    47             }
    48         }
    49     }
    50     cout << p << endl;
    51     return 0;
    52 }
  • 相关阅读:
    常用的CSS命名规则 (web标准化设计)
    有哪些概率论和数理统计的深入教材可以推荐?
    CV2X国内现状分析
    隐私计算,新能源汽车“安全上路”的“救命稻草”?
    2022年中国车联网行业全景图谱
    2022年十大AI预测:气候独角兽涌现、中美竞争加剧
    OSEK/VDX介绍
    Adaptive Autosar
    基于我国商密算法的车联网5GV2X通信安全可信体系
    行研篇 | 汽车域控制器研究
  • 原文地址:https://www.cnblogs.com/zhanhonhao/p/11219460.html
Copyright © 2011-2022 走看看