bjfu1109 least common multiple and

Source: Internet
Author: User
Tags greatest common divisor

This problem is really over n years before a. The first was in the 2010 Peking University Training competition saw this question, at that time I will not, bamboo master also won't, but he wrote down, study a period of time will, also put this question add to our school OJ on. After so many years, I search the Internet, the problem of the question of the report is still not, so I spent a few days to do out, published this report.

The topic is to require all numbers from 1 to n with the least common multiple of n, and again except N. When you see the data range at the first glance, you know that you can't do it hard (that is, counting from 1 to n sequentially). So, in order to find the law, we have to formulate a formula.

F (n) = [LCM (1,n) +LCM (2,n) +......+LCM (n,n)]/n
= 1/GCD (1,n) +2/gcd (2,n) +......+n/gcd (n,n)

So f (n) becomes a bunch of numbers.

Obviously, each gcd (k,n) is divisible by n, sorted by the value of GCD (k,n), you can sort f (n) into the following form

F (n) = (...) /1 + (...) /2 +......+ (...) /d

Here d is all integers that can divide n. and (...) /1 of the molecules for all and n coprime number of the and; /2 of the molecules for all greatest common divisor with n is 2, that is, the number of coprime with N/2, and so on, to get the formula

With this formula, although it is a big step forward, but still can't solve the problem. Because this still has to enumerate all the approximations of N, this complexity is still O (n).

The next step is to compare the core of the structure.

F (N) = (g (n) + 1)/2, the G (n) is immediately available f (n).

The reason for constructing G (n) here is because the G (n) is of an integrable nature.

That

For any gcd (m, n) = 1, there is g (M * N) = g (M) * g (n). The proof is slightly off, the reader is self-proof.

With the property of the product is good to do, the N-Factor decomposition into the form of pi^ci multiplication, n =∏ (PI^CI), then g (n) = g (∏ (PI^CI)) =∏ (g (PI^CI))

G (PI^CI) very good calculation. Here D is 1, pi, pi^2, pi^3, ..., Pi^ci, and phi (PI^CI) = (pi-1) *pi^ (ci-1), which together is a geometric series, can be introduced to a formula. In other words, g (pi^ci) can be calculated in O (1) time.

As a result, the whole problem can be solved.

However, this problem is still very abnormal, because the data there are 50000 groups, only decomposition factorization write bad, may hang out. Finally, I was past the code as follows:

/** bjfu1109 * Author:ben*/#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<ctime>#include<iostream>#include<algorithm>#include<queue>#include<Set>#include<map>#include<stack>#include<string>#include<vector>#include<deque>#include<list>#include<functional>#include<numeric>#include<cctype>using namespacestd; #ifdef on_local_debug#else#endiftypedefLong LongLl;typedefintTypec; LL Getpow (intAintb) {LL res, temp; Res=1, temp =(LL) A;  while(b) {if(B &1) {res= Res *temp; } b>>=1; Temp= Temp *temp; }    returnRes;}intGet_int () {intres =0, ch;  while(! (ch = getchar ()) >='0'&& CH <='9')) {        if(ch = =EOF)return-1; } Res= CH-'0';  while(ch = getchar ()) >='0'&& CH <='9') Res= Res *Ten+ (CH-'0'); returnRes;}Const intN =100000;BOOLIsprime[n +3];//use two more elements to avoid judging boundariesvector<int>pt;voidinit_prime_table () {memset (IsPrime,true,sizeof(IsPrime)); intp =2, Q, del; Doubletemp;  while(P <=N) { while(!isprime[p]) {p++; }        if(P > N) {//is over             Break; } temp= (Double) p; Temp*=p; if(Temp >N) Break;  while(Temp <=N) {del= (int) temp; Isprime[del] =false; Temp*=p; } q= p +1;  while(Q <N) { while(!isprime[q]) {q++; }            if(q >= N) { Break;} Temp= (Double) p; Temp*=Q; if(Temp > N) Break;  while(Temp <=N) {del= (int) temp; Isprime[del]=false; Temp*=p; } q++; } P++; }     for(inti =2; I <= N; i++) {        if(Isprime[i]) {pt.push_back (i); }    }}/** * P is a prime number table, should include at least sqrt (N) of all primes, F Save the result * F[i].first represents a factor of N, F[i].second of the element of the factor * Typec can be int, long, long long, etc. */typedef vector<pair<typec,int> >factorlist;voidGet_prime_factor (ConstTypec &n_, Factorlist &f,Constvector<int> &p) {intI, t, n, pl =p.size (); Typec N=N_;    F.clear ();  for(i =0; i < pl; i++) {T=P[i]; if(T * t >n_) {             Break; }        if(N% T = =0) {n=0;  while(N% T = =0) {n++; N/=T;        } f.push_back (Make_pair (t, N)); }        if(N = =1) { Break; }    }    if(N >1) {f.push_back (Make_pair (N,1)); }}inline ll G (ll P,intc) {LL ret= Getpow (P,2* C)-1; RET= RET/(p +1) * p +1; returnret;} Factorlist FL; LL g (intN) {get_prime_factor (n, FL, PT); intLen =fl.size (); LL ret=1;  for(inti =0; i < Len; i++) {ret*=g (Fl[i].first, Fl[i].second); }    returnret;}intMain () {#ifdef on_local_debug freopen ("data.in","R", stdin);#endifLL ans, N;    Init_prime_table ();  while((N = Get_int ()) >0) {ans= g (N) +1; Ans/=2; printf ("%i64d\n", ans); }    return 0;}

bjfu1109 least common multiple and

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.