Topcoder SRM 661 (Div.1) MISSINGLCM-Number theory

Source: Internet
Author: User

Test instructions

Give you a number N (1<=n<=10^6), requiring the smallest m (m>n), so that the LCM (n+1,n+2,... m) =lcm (,..., m)

Ideas

The hand is too slow, and when the code is finished, I find the game is over.

At first I wanted to enumerate m directly and determine if the LCM (1,.., m) was equal to the LCM (n+1,n+2,..., m), but found that it would have been the same when the LCM (1,..., 40) was obtained.

Obviously, it can't be asked.

In other words, it is difficult to ask for the value of the specific LCM (,..., m)

How to ask

It can be decomposed into factorization, decomposed into several prime-number multiplication forms.

Determine if the quality factor of the LCM (1,..., m) and LCM (n+1,n+2,..., m) is exactly the same.

But only 1~1000000 prime numbers have 80,000.

Enumerating m again enumerating prime numbers is obviously unbearable.

However, I notice that there is a nature (I don't know if it counts)

Assuming there is a prime number k, you can find T, so that the T-square of K is just less than M (k^t<=m)

Then the LCM (,..., m) decomposes factorization and has a maximum of T prime number k multiplication,

So you can quickly. LCM (,..., m) decomposition factorization

So how to decompose the LCM (n+1,n+2,..., m) into factorization?

Still assuming prime number K, you can find the largest t, and a constant C (1<=c<k), making n+1<=c*k^t<=m
Then the LCM (n+1,n+2,..., m) decomposition factorization must have a maximum of T prime number k multiplication.

For example, Prime 3,n=16,m=22, can be c=2,t=2, that is, 17<=2*3^2=18<=22, so that the LCM (17,18,19,20,21,22) of up to 2 prime number 3 multiply

Now that we know how to find the LCM (n+1,n+2,.., m) and LCM (,.., m)

To discuss how to find the smallest m.

We want the two LCM to decompose factorization exactly the same, that is to say, the number of mass of the multiplication is exactly equal.

That is, for each prime number K can be satisfied, the existence of C and the largest t makes N+1<=c*k^t<=m

For prime numbers greater than n less than M, we assume that P, then certain n+1<=p<=m, must satisfy the condition

So we just look at prime numbers that are less than or equal to N.

Because to make every prime number K less than n, there are C and the largest t makes N+1<=c*k^t<=m,

We enumerate each prime number and calculate C and T, making just c*k^t>=n

The answer is Max (c*k^t)

#include <cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<Set>#include<map>#include<stack>#include<vector>#include<queue>#include<string>#include<sstream>#defineEPS 1e-9#defineAll (x) X.begin (), X.end ()#defineINS (x) Inserter (X,x.begin ())#definefor (i,j,k) for (int i=j;i<=k;i++)#defineMAXN 1005#defineMAXM 40005#defineINF 0X3FFFFFFFusing namespaceStd;typedefLong LongLL; LL I,j,k,n,m,x,y,t,big,cas,num;BOOLFlag; LL Cur,ans;BOOLprim[2000005]; LL ver[2000005]; voidGetprim (ll size) {ll m=SQRT (size+0.5); memset (Prim,0,sizeof(prim));//can be emptied according to the situationnum=0;//put the found prime number in the Ver array, num is the length of the ver array//If you want to get an array of prime numbers, I will enumerate to size, and if it's just an prim array, enumerate to M     for(LL i=2; i<=size;i++)    {        if(!Prim[i]) {ver[++num]=i; if(i<=m) for(LL j=i*i;j<=size;j+=i) prim[j]=1; }    }}classmissinglcm{ Public:        intGetmin (intN) {LL n=N;            Getprim (n); LL ans=n+1;  for(i=num;i>=1; i--) {LL u=Ver[i];  for(j=1; j*u<=n;j*=u); Ans=max (ans, (n/j+1)*j); }            returnans; }};

Topcoder SRM 661 (Div.1) MISSINGLCM-Number theory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.