Bzoj 2154 Crash Digital table (Möbius inversion and optimization)

Source: Internet
Author: User

Description

Today's math class, crash children learn least common multiple (Least Common multiple). For two positive integers a and B,LCM (A, b) represent the smallest positive integers that can be divisible by A and b at the same time. For example, the LCM (6, 8) = 24. Back home, crash still thinking about class school, in order to study least common multiple, he drew a n*m form. A number is written in each lattice, where the lattice in column J of Row I is written in the LCM (i, J). A 4*5 table is as follows: 1 2 3 4 5 2 2 6 4 10 3 6 3 12 15 4 4 12 4 20 Looking at this form, crash thought of a lot of things to think about. But the question he most wants to solve is a very simple question: how much is there in this table? When N and M are big, crash is helpless, so he finds the smart you use the program to help him solve the problem. Since the end result can be large, crash only wants to know the values of all the numbers in the table and mod 20101009.

Input

The first line of input contains two positive integers, representing N and M., respectively.

Output

Outputs a positive integer that represents the number of values in the table and mod 20101009.

Sample Input4 5
Sample Output122
"Data size and conventions"
100% of the data satisfies N, m≤107.
"Ideas"This blog was torn down in a very detailed click here    I am the porter of the pictureSo far, as long as two loops with a block can solve 2154."Code"
1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd;5 6typedefLong Longll;7 Const intN = 1e7+Ten;8 Const intMOD =20101009;9 Ten ll Mu[n],su[n],sz,np[n]; One intn,m,mx; A  - voidGet_mu () - { the     inti,j; -mu[1]=1; -      for(i=2; i<=mx;i++) { -         if(!Np[i]) +su[++sz]=i,mu[i]=-1; -          for(j=1; j<=sz&&i*su[j]<=mx;j++) { +np[i*su[j]]=1; A             if(i%su[j]==0) atmu[i*su[j]]=0; -             Else -mu[i*su[j]]=-Mu[i]; -         } -     } -      for(i=1; i<=mx;i++) inMu[i]= (mu[i-1]+ (LL) (mu[i]*i*i)%mod)%MOD; - } to ll T (ll X,ll y) + { -     return(ll) (X* (x+1)/2%MOD) * (LL) (y* (y+1)/2%MOD)%MOD); the } *ll F (intNintm) $ {Panax Notoginseng     intI,last; ll ans=0; -      for(i=1; i<=n;i=last+1) { theLast=min (n/(n/i), m/(m/i)); +Ans= (ans+ (LL) (mu[last]-mu[i-1]) *t (n/i,m/i)%mod)%MOD; A     } the     returnans; + } - intMain () $ { $     //freopen ("in.in", "R", stdin); -     //freopen ("Out.out", "w", stdout); -scanf"%d%d",&n,&m); the     if(n>m) swap (n,m); -mx=N;Wuyi Get_mu (); the     intLast ll ans=0; -      for(intI=1; i<=n;i=last+1) { WuLast=min (n/(n/i), m/(m/i)); -Ans= (ans+ (LL) (i+last) * (last-i+1)/2*f (n/i,m/i)%mod))%MOD; About     } $printf"%lld", (ans+mod)%MOD); -     return 0; -}

Optimization

  I am the porter of the picture

  

(H hit error is F 233)

The approximate and also the integrable function of the integrable function, H (D) can be obtained by a linear sieve, and then the prefix is obtained.

So far, 2693 of the multi-query problem can be solved.

"Code"
1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd;5 6typedefLong Longll;7 Const intN = 1e7+Ten;8 Const intMOD =20101009;9 Ten ll Sum[n],su[n],sz,np[n]; One intn,m,mx; A  - voidGet_mu () - { the     inti,j; -sum[1]=1; -      for(i=2; i<=mx;i++) { -         if(!Np[i]) { +su[++sz]=I, -Sum[i]= (I-(ll) i*i)%MOD; +         } A          for(j=1; j<=sz&&i*su[j]<=mx;j++) { atnp[i*su[j]]=1; -             if(i%su[j]==0) -Sum[i*su[j]]= (Su[j]*sum[i])%MOD; -             Else -Sum[su[j]*i]= (Sum[su[j]]*sum[i])%MOD; -         } in     } -      for(i=1; i<=mx;i++) toSum[i]= (sum[i]+sum[i-1])%MOD; + } - ll S (ll X,ll y) the { *     return(ll) (X* (x+1)/2%MOD) * (LL) (y* (y+1)/2%MOD)%MOD); $ }Panax Notoginseng  - intMain () the { +     //freopen ("in.in", "R", stdin); A     //freopen ("Out.out", "w", stdout); themx=1e7; + Get_mu (); -     intT scanf"%d",&T); $      while(t--) { $scanf"%d%d",&n,&m); -         if(n>m) swap (n,m); -mx=N; get_mu (); the         intLast ll ans=0; -          for(intI=1; i<=n;i=last+1) {WuyiLast=min (n/(n/i), m/(m/i)); theAns= (ans+ (LL) S (n/i,m/i) * (sum[last]-sum[i-1])%mod)%MOD; -         } Wuprintf"%lld\n", (ans+mod)%MOD); -     } About     return 0; $}

twice times experience get:)finally throw on POPOQQQ god Ben ppt Click here

Bzoj 2154 Crash Digital table (Möbius inversion and optimization)

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.