"Hebei Province team Mutual test" GCD Bzoj 2818

Source: Internet
Author: User
Tags gcd

Description

Given an integer n, 1<=x,y<=n and gcd (x, y) are prime
Number pairs (x, y) how many pairs.

Input

An integer n

Output

Title

Sample Input4

Sample Output4HINT

Hint

For examples (2,2), (2,4), (3,3), (4,2)


1<=n<=10^7

Ideas

I've seen a lot of questions about GCD and mods lately.

Learned a lot from recent topics. =

First there is such a property: if A∈[1,n],b∈[1,m], then GCD (A, B) |k has (n/k) * (m/k) group.

So f[x] is gcd (A, B) ==k the number of groups, f[k]= (n/k) * (m/k)-f[2k]-f[3k]-f[4k] ...

For this question: It seems that it is not possible to live.

Then there is a different nature:

If the number of A,B∈[1,N],GCD (A, B) ==k is equivalent to the number of groups of A,B∈[1,N/K],GCD (A, b) ==1.

This is good to ask, is the 1->n of Phi value (Euler function) *2-1.

First, the number of each group must be counted two times, such as (3,5) and (5,3), so to * *. Then do not count two times, so again-1.

Then it is how to find all the prime numbers and Euler functions in 1-n. On-site learning linear sieve.

In fact, I totally do not understand AH. First of all, remember. The core code is as follows:

1phi[1]=1; memset (Is_prime,true,sizeof(Is_prime));2  for(intI=2; i<=n;i++){3     if(Is_prime[i]) {4phi[i]=i-1;5prime[++cnt]=i; 6     }    7      for(intj=1; j<=cnt&&i*prime[j]<=n;j++){8is_prime[i*prime[j]]=false;9        if(i%prime[j]!=0) phi[i*prime[j]]=phi[i]* (prime[j]-1);Ten        Else{ Onephi[i*prime[j]]=phi[i]*Prime[j]; A           Break; -        } -     } the}
Linear Sieve

It should have been written wrong. In order to strengthen the memory of dictation. If there is a problem, look at the following version of it, that is AC.

1#include <iostream>2#include <cstring>3#include <string>4#include <cstdio>5#include <cstdlib>6#include <cmath>7#include <algorithm>8#include <queue>9#include <stack>Ten#include <map> One#include <Set> A#include <list> -#include <vector> -#include <ctime> the#include <functional> - #definePRITNF printf - #defineScafn scanf - #defineSACNF scanf + #definefor (i,j,k) for (int i= (j); i<= (k);(i) + +) - #defineClear (a) memset (A,0,sizeof (a)) + using namespacestd; Atypedef unsignedintUint; at Const intinf=0x3fffffff; - ///==============struct declaration============== -  - ///==============var declaration================= - Const intmaxn=10000050; - intn,tot=0, ans=0; in intPRIME[MAXN]; - Long LongPHI[MAXN]; to BOOLIS_PRIME[MAXN]; + ///==============function declaration============ - voidInit (); the ///==============main code======================= * intMain () $ {Panax Notoginseng //#define FILE__ - #ifdef file__ theFreopen ("input","R", stdin); +Freopen ("Output","W", stdout); A #endif thescanf"%d",&n); + Init (); -     for(intI=1; i<=n;i++) phi[i]+=phi[i-1]; $    Long Longans=0; $     for(intI=1; i<=tot;i++) -ans+=phi[n/prime[i]]*2-1; -printf"%lld\n", ans); the    return 0; - }Wuyi ///================fuction code==================== the voidInit () { -memset (Is_prime,true,sizeof(Is_prime)); phi[1]=1; Wu     for(intI=2; i<=n;i++){ -       if(Is_prime[i]) { Aboutphi[i]=i-1; $prime[++tot]=i; -       } -        for(intj=1; j<=tot;j++){ -          if(i*prime[j]>n) Break; Ais_prime[i*prime[j]]=false; +          if(i%prime[j]==0){ thephi[i*prime[j]]=phi[i]*Prime[j]; -              Break; $          } the          Else thephi[i*prime[j]]=phi[i]* (prime[j]-1); the       } the    } -}
BZOJ2818

Don't ask me why those qualities are. I also Phuket (╯‵-′) ╯︵┻━┻

"Hebei Province team Mutual test" GCD Bzoj 2818

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.