Hdu.5212.code (Möbius inversion && linear sieve)

Source: Internet
Author: User

Code

Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others) total submission (s): Accepted s Ubmission (s): 124

Problem Descriptionwld likes playing with codes. One day and he is writing a function. Howerver,his computer breaks down because the function is too powerful. He is very sad. Can you help him?
The function:
int calc {int res=0;              for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) {RES+=GCD (A[i],a[j]) * (GCD (A[i],a[j])-1);          res%=10007; } return res;
}

Inputthere is multiple Cases. (At mostTen)
For each case:
The first line contains an integer n(1≤n≤10000) .
The next line containsNIntegers a1,a2,.. . ,aN(1≤ai≤10000) .

Outputfor each case:
Print an integer,denoting what the function returns.

Sample Input51 3 4 2 4

Sample Output64 HintGCD (x, y) means the greatest common divisor of x and Y.
1#include <stdio.h>2#include <string.h>3 intprime[10000+Ten] ;4 inta[10000+Ten] ;5 intmui[10000+Ten] ;6 BOOLvis[10000+Ten] ;7 8 intMain ()9 {Tenmemset (Prime,0,sizeof(prime)); OneMemset (MUI,0,sizeof(MUI)); Amemset (Vis,0,sizeof(Vis)); -      for(inti =1; I <=10000; i + +) a[i] =i; -      for(inti =2; I <=10000; i + +) { the          for(intj = i; J <=10000; J + =i) { -             if(A[j]% i = =0&&!Vis[j]) { -                 intCNT =0 ; -                  while(A[j]% i = =0) { +A[J]/=i; -CNT + + ; +                 } A                 if(CNT >1) {Vis[j] =1; MUI[J] =0 ;} at                 ElseMUI[J] + + ; -             } -         } -     } -   /*printf ("μ_source:\n"); - for (int i = 2; I <= 5; i + +) printf ("ID%d:%d\n", I, Mui[i]); puts ("");*/ inmui[1] =1 ; -      for(inti =2; I <=10000; i++) { to         if(Mui[i]) mui[i] = (int) Pow (-1, Mui[i]); +     } -}
View Code

I learned from the Jiege a kind of and Baidu on the different Möbius inversion (personal feeling good):

n is all multiples of d.

The

μ (1) = 1;

K = P1 * P2 * P3 ... *pr (k consists of r different prime numbers) μ (k) = -1^k;

In other cases, μ (k) = 0;

The question F (x) refers to the number of logarithms in multiples of X.

f (x) = Greatest common divisor is the number of the majority of X.

Like what:

F (1) = f (1) + F (2) + F (3) + f (4) = 7 + 2 + 0 + 1 = 10

Getting f (x) is very easy to count the number of x multiples, such as =cnt;

then f (x) = (CNT * cnt-cnt)/2;(think about it a little bit)

Then, it's the time for .....

Hdu.5212.code (Möbius inversion && linear sieve)

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.