cf449c Jzzhu and Apples (number theory of sieve primes?)

Source: Internet
Author: User
Tags greatest common divisor cmath

Codeforces Round #257 (Div. 1) C

Codeforces Round #257 (Div. 1) E

cf450e

C. Jzzhu and Applestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Jzzhu have picked n apples from the his Big Apple tree. All the apples is numbered from 1 to n. Now he wants to sell them to an Apple store.

Jzzhu'll pack his apples to groups and then sell them. Each group must contain-apples, and the greatest common divisor of numbers of the apples in each group must be greater than 1. Of course, each Apple can is part of the at the most one group.

Jzzhu wonders how to get the maximum possible number of groups. Can you help him?

Input

A Single integer n (1≤ n ≤105), the number of the apples.

Output

The first line must contain a single integer m, representing the maximum number of groups he can get. Each of the next m lines must contain and integers-the numbers of apples in the current group.

If there is several optimal answers you can print any of them.

Sample Test (s) Input
6
Output
2
6 3
2 4
Input
9
Output
3
9 3
2 4
6 8
Input
2
Output
0

Test Instructions: There are N apples, numbered 1~n. There are several pairs of apples now to be made, and the minimum number of conventions per Apple is not 1. Can be divided into how many pairs, the output of the composition of the pair.

The first number of sieve, then engage.

The first thing we're afraid of is a random selection of two numbers that make up a couple of 1 of the conventions, but this leads to a total logarithmic decrease (these two numbers are required by other numbers, and they make up a pair of not optimal solutions). To prevent this, we have to find a way to make the total logarithm not decrease.

We find that multiples of 2 are very pillbox fried, and any 2 can make up 1 pairs, so let's start with the other numbers and then the multiples of 2.

We found a prime number x 1 time times, twice times, 3 times times 、......? The number of unused components in the fold can also be any combination of 22, but if there is an odd number of elements in this set between 1~n, there will be one more. In order not to cause unnecessary effects, we put 2*x as a more than one, and throw it in multiples of 2. In this way, the various sets of the more out of one, certainly can find pairing. Mark the number we use to prevent the repetition of a number when doing other prime numbers.

First of all 3 to less than Equals (N/2) of the prime number (greater than N/2, it is the set of multiples of its own, can not play), and then make a multiple of 2, the 2*x and other 2*y (Y is composite) to form a large set, 22 pairs, and finally more out of a no way, This is already the most paired.

The solution is not unique, we can certainly find the most logarithm, pillbox fry.

Code:

1 //#pragma COMMENT (linker, "/stack:102400000,102400000")2#include <cstdio>3#include <cmath>4#include <iostream>5#include <cstring>6#include <algorithm>7#include <cmath>8#include <map>9#include <Set>Ten#include <stack> One#include <queue> A using namespacestd; - #definell Long Long - #defineUSLL unsigned ll the #defineMZ (Array) memset (array, 0, sizeof (array)) - #defineMinf (array) memset (array, 0x3f, sizeof (array)) - #defineREP (I,n) for (i=0;i< (n); i++) - #definefor (I,x,n) for (i= (x); i<= (n); i++) + #defineRD (x) scanf ("%d", &x) - #defineRD2 (x, y) scanf ("%d%d", &x,&y) + #defineRD3 (x, Y, z) scanf ("%d%d%d", &x,&y,&z) A #defineWN (x) prllf ("%d\n", X); at #defineRE freopen ("d.in", "R", stdin) - #defineWE freopen ("1biao.out", "w", stdout) - #defineMP Make_pair - #definePB Push_back -  - Const LongN =100001; in intPRIME[N],PN =0; - BOOLIsnp[n]; to voidShai () { +     inti,j; -memset (Prime,0,sizeof(prime)); thememset (ISNP,0,sizeof(ISNP)); *isnp[0]=1, isnp[1]=1; $pn=0;Panax Notoginseng      for(i =2; i < N; i + +) { -         if(!Isnp[i]) thePRIME[PN ++]=i; +         //key points 1 A          for(j =0; J < pn && I * prime[j] < N; J + +) { theIsnp[i * Prime[j]] =1; +             if( ! (i% prime[j]))//Key points 2 -                  Break; $         } $     } - } -  the intN; -vector<int>a,a2;Wuyivector<pair<int,int> >v; the BOOLUsed[n]; - intMain () { Wu     inti,j,k; -     intL,r,mid; About     intPre; $     intans; - Shai (); -      while(SCANF ("%d", &n)! =EOF) { -ans=0; A v.clear (); + a2.clear (); thememset (Used,0,sizeof(used)); -  $          for(i=1; prime[i]<=n/2; i++) { the a.clear (); the A.PB (Prime[i]); the              for(j=3*prime[i]; j<=n; J+=prime[i])if(!Used[j]) A.PB (j); the             if(A.size ()%2==0) A2.PB (2*prime[i]); -             ElseA.PB (2*prime[i]); in             intmaxj=a.size (); the              for(j=0; j+1<maxj; j+=2) { theV.PB (MP (a[j],a[j+1])); Aboutused[a[j]]=1; theused[a[j+1]]=1; the             } the         } +  -         if(n>=2) A2.PB (2); the         if(n>=4) A2.PB (4);Bayi          for(i=4; i+i<=n; i++) { the             if(!used[i+i] && isnp[i]) A2.PB (i+i); the         } -         intMaxi=a2.size (); -          for(i=0; i+1<maxi; i+=2) V.PB (MP (a2[i],a2[i+1])); theprintf"%d\n", V.size ()); theMaxi=v.size (); theREP (I,maxi) printf ("%d%d\n", V[i].first,v[i].second); the     } -     return 0; the}
View Code

Related Article

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.