Nyoj CO-PRIME Mobius Inversion

Source: Internet
Author: User
CO-PRIME time limit: 1000 MS | memory limit: 65535 kb difficulty: 3
Description

This problem is so easy! Can you solve it?

You are given a sequence which contains N integers A1, A2 ...... An, your task is to find how many pair (AI, AJ) (I <j) That AI and AJ is Co-Prime.

 

 
Input
There are multiple test cases.
Each test case conatains two line, the first line contains a single integer N, the second line contains N integers.
All the integer is not greater than 10 ^ 5.
Output
For each test case, You shoshould output one line that contains the answer.
Sample Input
31 2 3
Sample output
3

Idea: http://blog.csdn.net/lyhvoyage/article/details/38455415should be the answer.

Analysis: Mobius Inversion.

In this question, F (d) indicates the number of pairs in the N number where GCD is a multiple of D, F (d) indicates the number of pairs in which the number of N is exactly D,

Then f (d) = Σ F (N) (N % d = 0)

F (d) = Sigma Mu [N/d] * F (N) (N % d = 0)

The above two formulas are the formulas in the Mobius Inversion.

Therefore, F (1) is used to calculate the number of mutual elements ).

According to the formula above, we can obtain F (1) = Σ Mu [N] * F (n ).

So we can find out Mu [] and enumerate n, where Mu [I] is the Mobius function of I.

1 # include <iostream> 2 # include <stdio. h> 3 # include <cstring> 4 # include <cstdlib> 5 using namespace STD; 6 const int n = 1e5 + 1; 7 8 int vis [N]; 9 int Mu [N]; 10 int prime [N], CNT; 11 int date [N]; 12 long ys [N]; 13 int num [N]; 14 void Init () 15 {16 memset (VIS, 0, sizeof (VIS); 17 Mu [1] = 1; 18 CNT = 0; 19 For (INT I = 2; I <n; I ++) 20 {21 if (! Vis [I]) 22 {23 prime [CNT ++] = I; 24 Mu [I] =-1; 25} 26 for (Int J = 0; j <CNT & I * prime [J] <n; j ++) 27 {28 vis [I * prime [J] = 1; 29 if (I % prime [J]) Mu [I * prime [J] =-mu [I]; 30 else31 {32 Mu [I * prime [J] = 0; 33 break; 34} 35} 36} 37} 38 int main () 39 {40 int N, maxn; 41 Init (); 42 while (scanf ("% d", & N)> 0) 43 {44 memset (Num, 0, sizeof (Num )); 45 memset (ys, 0, sizeof (YS); 46 maxn =-1; 47 for (INT I = 1; I <= N; I ++) {48 scanf ("% d", & date [I]); 49 num [date [I] ++; 50 if (date [I]> maxn) maxn = date [I]; 51} 52/*** calculate F (n) */53 for (INT I = 1; I <= maxn; I ++) 54 {55 for (Int J = I; j <= maxn; j = J + I) 56 {57 ys [I] = ys [I] + num [J]; 58} 59} 60 long sum = 0; 61 for (INT I = 1; I <= maxn; I ++) {62 long TMP = (long) YS [I] * (YS [I]-1)/2; 63 sum = sum + Mu [I] * TMP; 64} 65 66 printf ("% i64d \ n", sum); 67} 68 return 0; 69}

 

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.