Bzoj 1114 number theory (Möbius inversion + preprocessing)

Source: Internet
Author: User

Title Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=71738

Test instructions: give you an integer sequence a1, A2, A3, ..., an. GCD (AI, aj) = 1 and the logarithm of I < J.

Idea: The formula can be obtained quickly using the inverse of the A1, but we need to know the number of multiples of 1, 2, 3, ..., Max (A2, ..., an) in the sequence. We use Num[i]=k to indicate that the number of k in the sequence is a multiple of I, then the effect of this part on the result is mu[i]* (k-1) * K/2. The final result is Sigma (mu[i]* (k-1) * K/2).

Code

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5typedefLong LongLL;6 Const intMAXN =250000;7 intNUM[MAXN];//Num[i] Indicates the number of satisfied (I|ak)8 intTMP[MAXN];//mark which numbers have9 BOOLCHECK[MAXN];Ten intPRIMES[MAXN]; One intMU[MAXN]; A  - voidMoblus () - { thememset (check,false,sizeof(check)); -mu[1] =1; -     intCNT =0; -      for(inti =2; i < MAXN; ++i) { +         if(!Check[i]) { -primes[cnt++] =i; +Mu[i] =-1; A         } at          for(intj =0; J < CNT; ++j) { -             if(i * primes[j] > MAXN) Break; -Check[i * Primes[j]] =true; -             if(i% primes[j] = =0) { -Mu[i * Primes[j]] =0; -                  Break; in}Else { -Mu[i * Primes[j]] =-Mu[i]; to             } +         } -     } the } *  $ intMain ()Panax Notoginseng { - Moblus (); the     intN; +      while(SCANF ("%d", &n)! =EOF) { Amemset (NUM,0,sizeof(num)); thememset (TMP,0,sizeof(TMP)); +         intTmax =0; -          for(inti =0; I < n; ++i) { $             intx; $scanf"%d", &x); -++Tmp[x]; -Tmax =Max (Tmax, x); the         } -          for(inti =1; I <= Tmax; ++i) {Wuyi              for(intj = i; J <= Tmax; J + =i) { theNum[i] + =Tmp[j]; -             } Wu         } -LL ans =0; About          for(inti =1; I <= Tmax; ++i) { $Ans + = (LL) mu[i] * num[i] * (Num[i]-1) /2; -         } -printf"%lld\n", ans); -     } A     return 0; +}

Bzoj 1114 number theory (Möbius inversion + preprocessing)

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.