Problem Statement

The least common multiple (denoted "lcm") of a non-empty sequence of positive integers is the smallest positive integer that is divisible by each of them. For example, lcm(2)=2, lcm(4,6)=12, and lcm(1,2,3,4,5)=60.

Alice had a positive integer N. Then she chose some positive integer M that was strictly greater than N. Afterwards, she computed two values:

the value A = lcm(N+1, N+2,
..., M) and the value B = lcm(1, 2, ..., M). She was surprised when she saw that A = B.the value A = lcm(N+1, N+2,
..., M) and the value B = lcm(1, 2, ..., M). She was surprised when she saw that A = B.the value A = lcm(N+1, N+2,
..., M) and the value B = lcm(1, 2, ..., M). She was surprised when she saw that A = B.the value A = lcm(N+1, N+2,
..., M) and the value B = lcm(1, 2, ..., M). She was surprised when she saw that A = B.the value A = lcm(N+1, N+2,
..., M) and the value B = lcm(1, 2, ..., M).

You are given the int N. Find and return the smallest M Alice could have chosen. (Such an M will always exist.)

Definition

  • ClassMissingLCM
  • MethodgetMin
  • Parametersint
  • Returnsint
  • Method signatureint getMin(int N)
(be sure your method is public)

Limits

  • Time limit (s)2.000
  • Memory limit (MB)256

Constraints

  • N will be between 1 and 1,000,000, inclusive.

Test cases

    • N1

    Returns2

    Alice needs to choose an M > 1 such that lcm(2,...,M) = lcm(1,...,M). We can see M=2 is the minimum value that works, since lcm(1,2) = lcm(2) = 2.
    • N2

    Returns4

    • N3

    Returns6

    We have lcm(4,5,6) = lcm(1,2,3,4,5,6) = 60.
    • N4

    Returns8

    • N5

    Returns10

    • N42

    Returns82

    Oh... that doesn't fit the pattern.
      • N999999

      Returns1999966

    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  1. In this problem the actual LCM values used can be notably large. It is best to avoid approaches that calculate them directly. Then how about we think of this problem in terms of the prime factorization of the numbers. For example, consider two numbers: 12 and
    135. Their prime factorizations are: 22⋅3 and 33⋅5.
    The prime factorization of the LCM will be: 22⋅33⋅5.
    In other words, for each prime number , we take the maximum exponent of the prime number among the two numbers we are calculating the LCM for. How does this translate to the two LCMs used?

    A=LCM(N+1,N+2,...,M) 

    B=LCM(1,2,...,M) 

    A=B

    We need to translate this into distinct conditions, one for each relevant prime number:

    a=max(ep(N+1),ep(N+2),...,ep(M)) 

    b=max(ep(1),ep(2),...,ep(M)) 

    a=b

    Where ep(x) is
    the exponent of prime number p in
    the prime factorization of x,
    the maximum number of times you can repeatedly divide x by p.
    In words we just want the maximum exponent of prime p among
    all the factorizations between N+1 and M and
    between 1 and M to
    be equal. Try this:

    b=max(ep(1),ep(2),...,ep(N),ep(N+1),...,ep(M)) 

    b=max(ep(1),ep(2),...,ep(N),max(ep(N+1),...,ep(M))) 

    b=max(ep(1),ep(2),...,ep(N),a)

    What happens here is we take advantage that the maximum operation is associative. This means max(a,b,c)=max(a,max(b,c))=max(max(a,b),c).
    The useful thing to conclude about this: b≥a.
    There's more:

    b=max(max(ep(1),ep(2),...,ep(N)),a) 

    c=max(ep(1),ep(2),...,ep(N)) 

    b=max(c,a)

    We want a=b=max(c,a):
    This means we want c≤a.
    Note that c is
    constant, as it's determined by the numbers between 1 and N.
    So we just need to look for a value of M such
    that the maximum exponent of p among N+1,N+2,...M is
    greater than or equal to c.

    There must be a number x>N for
    which ep(x)≥c,
    this means that the exponent of p in
    the prime factorization of x.
    If we take the maximumep(i) for
    all i between N+1,...,M,
    the result would be at least c,
    meaning that using M=x would
    be correct. M=x+1 and
    any Mgreater
    than x would
    also be correct. If we find the minimum M that
    is valid for p,
    then we can assume that all greater numbers will also be valid for p.
    This minimum M will
    be the smallest x>N for
    which ep(x)≥c.

    Did you notice that in the examples it initially appears that the result is always 2N and
    then the result seems to be smaller than 2N but
    not far apart?

    There is a good explanation for this. c is
    the maximum exponent of p for
    some number less than or equal to N,
    let's call that number m. 2m will
    also have that exponent for p (unless p=2,
    in which it will have an even larger exponent). Making 2m a
    valid value for M.
    If we pick M=2N,
    we will guarantee that this happens for all relevant prime numbers. This is useful because it means we only need to search for xamong
    numbers less than or equal to 2N.

    For each prime p,
    there will be a distinct minimum valid M ,
    we should pick the maximum out of all of them. This number will be valid for all primes we try. Note that when p>N,
    any M>N will
    be correct. Because when p>N , c is
    zero, none of the numbers smaller than or equal to N will
    be multiples of p,
    so the maximum exponent will be 0. This means we only need to repeat this for all the prime numbers that are less than or equal to N.

    For each prime number we need to find c and
    also find the minimum x such
    that: ep(x)>c and N<x≤2N.
    The final improvement we need is to notice that given p,
    we only need to think in terms of numbers that are multiples of p.
    For i≤N,
    only values of i that
    are multiples of pwill
    have an exponent of p in
    their prime factor, so only they are relevant for the calculation of c.
    For i>N,
    only multiples of p may
    have an exponent larger than or equal to c.
    In total we will try all the multiples of p less
    than or equal to 2N.
    This is repeated for each p<N.
    The final number of steps needed is: 2N2+2N3+2N5+...+2NP where P is
    the maximum prime that doesn't exceed N.
    This number of steps is very good and the complexity would be similar to the Sieve of Eratosthenes'. A simple way to tell that complexity is O(NN−−√) :
    For all P>2N−−−√, 2NP=1.
    There are 2N−2N−−−√ such
    values, this is O(N).
    For the other O(N−−√) values
    of p,
    even if we assumed O(N) steps,
    the total complexity would still be: O(N+NN−−√).
    We are ready to implement this solution:

    vector<int> get_primes(int N)
    {
    // Sieve of Erathostenes to find all the necessary prime numbers:
    vector<int> res;
    vector<bool> composite(N + 1, false); for (int p = 2; p <= N; p++) {
    if (! composite[p]) {
    for (int i = p+p; i <= N; i += p) {
    composite[i] = true;
    }
    res.push_back(p);
    }
    }
    return res; } int get_exponent(int x, int p)
    {
    int r = 0;
    while (x % p == 0) {
    r++;
    x /= p;
    }
    return r;
    } int getMin(int N)
    {
    int res = 2;
    // For each prime number <= N:
    for (int p: get_primes(N) ) {
    // first find the maximum exponent of p among numbers <= N
    // (in the explanation , this max_exp is c)
    int max_exp = 0;
    int i = p;
    while (i <= N) {
    max_exp = std::max(max_exp, get_exponent(i,p) );
    i += p;
    }
    // seek the minimum i such that get_exponent(i,p) >= max_exp:
    while (get_exponent(i,p) < max_exp) {
    i += p;
    }
    // the maximum for all ps is the result:
    res = std::max(res, i);
    }
    return res;
    }

最小公倍数 SRM 661 Div1 250: MissingLCM的更多相关文章

  1. Topcoder SRM 643 Div1 250<peter_pan>

    Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...

  2. topcoder srm 661 div1

    problem1 link $N+1$到$M$ 之间的数字要包含所有1到$N$之间出现的质因子的最高幂即可. problem2 link 从第一个节点到第$N$个节点依次考虑.对于第$i$个节点来说, ...

  3. SRM 595 DIV1 250

    挺简单的组合把. #include <cstdio> #include <cstring> #include <iostream> #include <vec ...

  4. SRM 594 DIV1 250

    可能开始宿舍比较乱,思绪静不下来...想了大半个小时,终于确定了应该暴力+DP,然后写了枚举除数,和被除的版本..这样,还敲错了个字母,第一次提交还100多,修改提交还有75.多,最后想到,貌似不打对 ...

  5. TC SRM 593 DIV1 250

    我只能说的亏没做,要不就挂0了.. 本来想四色定理,肯定4就可以的...然后准备爆,发现3的时候不好爆,又想了老一会,嗯,数据范围不小,应该不是暴力,直接找规律,貌似最大就是3,有一个3连块,输出3, ...

  6. TC SRM 593 DIV1 250(dfs)

    这图最多3色就可以 搜2就行了 #include <iostream> #include<cstdio> #include<cstring> #include< ...

  7. Topcoder SRM 698 Div1 250 RepeatString(dp)

    题意 [题目链接]这怎么发链接啊..... Sol 枚举一个断点,然后类似于LIS一样dp一波 这个边界条件有点迷啊..fst了两遍... #include<bits/stdc++.h> ...

  8. TopCoder SRM500 Div1 250 其他

    原文链接https://www.cnblogs.com/zhouzhendong/p/SRM500-250.html SRM500 Div1 250 题意 (看题用了半个小时--) 有 n 个人(编号 ...

  9. Topcoder Srm 726 Div1 Hard

    Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...

随机推荐

  1. ASP.Net中关于WebAPI与Ajax进行跨域数据交互时Cookies数据的传递

    本文主要介绍了ASP.Net WebAPI与Ajax进行跨域数据交互时Cookies数据传递的相关知识.具有很好的参考价值.下面跟着小编一起来看下吧 前言 最近公司项目进行架构调整,由原来的三层架构改 ...

  2. Android adb logcat使用技巧

    前言 新买的笔记本E431装了最新版的Eclipse,搞定了Android开发环境,可是logcat里查看东西居然仅仅显示level,没有错误的具体信息.我本身也不是一个愿意折腾图形界面,更喜欢纯命令 ...

  3. [转发]将Delphi的对象方法设为回调函数

    心血来潮,为了实现更好的通用性和封装性,需要把类方法作为回调函数,搜得一篇好文,节选转发.命名似乎应该是MethodToCallback才合适,可惜调试时总是报错,debugging. 原文地址:ht ...

  4. iOS内存管理策略和实践

    转:http://www.cocoachina.com/applenews/devnews/2013/1126/7418.html 内存管理策略(memory Management Policy) N ...

  5. iOS-Xcode必备插件XAlign:瞬间优化你的代码

    今天向大家介绍一个非常好用的Xcode代码编辑插件,这个插件可以很快速地使代码对齐,有3种模式:“=”对齐.宏定义对齐和属性对齐 XAlign效果图 1.“=”对齐   2.宏定义对齐     3.属 ...

  6. C#编程(十七)----------Object类

    Object类 它是.NET Framework 中所有类的最终基类:它是类型层次结构的根.也就是说所有的类都拥有object类的方法,并能重写,调用. object的构造函数:public Obje ...

  7. hibernate 注解 联合主键映射

    联合主键用Hibernate注解映射方式主要有三种: 第一.将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并重写equals和hascode,再将 该类注解 ...

  8. JAVA card 应用开发(二) 在项目添加APPLET

    在上篇博文中.<JAVA card 应用开发创建第一个APPLET>.介绍了一个项目从无到有. 那么.我们建立了这个项目后,仅仅有一个应用(一个可选AID),假设我希望这个项目能够有多个应 ...

  9. SharePoint Designer 配置工作流后需要重启的问题

    前言 最近,很多朋友配置SharePoint工作流以后,用SharePoint Designer打开站点,创建SharePoint 2013 工作流的时候,都会报一个错误. 查了很多帖子,发现是个De ...

  10. maven的groupid和artifactId

    groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找. groupId一般分为多个段 ...