zoukankan      html  css  js  c++  java
  • CodeForces1152CNeko 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 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 (1a,b10^9).

    Output

    Print the smallest non-negative integer k (k0) 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
     
    input
    6 10
    output
    2
    input
    21 31
    output
    9
     
     
    input
    5 10
    output
    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.

    题解

     假设a <= b,根据LCMGCD的关系知:LCM(a,b) = a*b/GCD(a,b),要求最小的k满足最小的LCM(a+k,b+k) = (a+k)*(b+k)/GCD(a+k,b+k),由更相减损法知,GCD(a+k,b+k) = GCD(a+k,b-a)。这样就使得GCD中有一项(即b-a)是固定的,这样有什么好处呢?没错,就是GCD(a+k,b-a)的结果一定是b-a的某个因子,而b-a的所有因子是有限个,且可以在sqrt(b-a)的时间内求出,故枚举b-a所有的因子即可。对于b-a的每一个因子di,都可以求出最小的ki = di - a%d(a%di != 0) 或者ki = 0 (a%di == 0),再代回公式计算,保留使得LCM(a+k,b+k)最小的k即可。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <string>
     6 #include <algorithm>
     7 #define re register
     8 #define il inline
     9 #define ll long long
    10 #define ld long double
    11 const ll MAXN = 1e6+5;
    12 const ll INF = 1e8;
    13 
    14 ll f[MAXN];
    15 
    16 //快读
    17 il ll read()
    18 {
    19     char ch = getchar();
    20     ll res = 0, f = 1;
    21     while(ch < '0' || ch > '9')
    22     {
    23         if(ch == '-')   f = -1;
    24         ch = getchar();
    25     }
    26     while(ch >= '0' && ch <= '9')
    27     {
    28         res = (res<<1) + (res<<3) + (ch-'0');
    29         ch = getchar();
    30     }
    31     return res*f;
    32 }
    33 
    34 //辗转相除法
    35 ll gcd(ll a, ll b)
    36 {
    37     ll mx = std::max(a,b);
    38     ll mi = std::min(a,b);
    39     return mi == 0 ? mx : gcd(mi,mx%mi);
    40 }
    41 
    42 int main()
    43 {
    44     ll a = read();
    45     ll b = read();
    46     ll mx = std::max(a,b);
    47     ll mi = std::min(a,b);
    48     ll tot = 0;
    49     ll c = mx-mi;
    50     ll n = sqrt(c);
    51     //枚举c的所有因子
    52     for(re ll i = 1; i <= n; ++i)
    53     {
    54         if(!(c%i))
    55         {
    56             f[++tot] = i;
    57             f[++tot] = c/i;
    58         }
    59     }
    60     //依次代回c的因子计算
    61     ll k = 0;
    62     ll d = a*b/gcd(a,b);
    63     for(re ll i = 1; i <= tot; ++i)
    64     {
    65         ll tk = !(mi%f[i]) ? 0 : f[i]-mi%f[i];
    66         ll td = (a+tk)*(b+tk)/f[i];
    67         if(td <= d)
    68         {
    69             if(td == d) k = std::min(k,tk);     //二者相同取最小的k
    70             else    k = tk;
    71             d = td;
    72         }
    73     }
    74     printf("%lld\n", k);
    75     return 0;
    76 }
    View Code
  • 相关阅读:
    敏捷开发学习笔记-Agile development(AM)
    WindowsServer --------- 在服务器中安装sqlserver 数据库
    WIndowsServer ---------- 将本地文件映射到服务器
    windowsServer-------- 系统中调出文件扩展名
    SQLServer -------- 解决忘记sa 密码,创建一个新的
    SQLServer ---------- 附加数据库,以及解决附加时出现错误
    AJAX -------------- 如何使用ajax
    网络基础 ----------- osi 与 一些协议
    装系统---------- u盘 安装系统
    网络基础 ----------- 电脑设置为wifi站点
  • 原文地址:https://www.cnblogs.com/BlueHeart0621/p/11566871.html
Copyright © 2011-2022 走看看