E. 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
26 32 4
Input
9
Output
39 32 46 8
Input
2
Output
0
Idea: This problem is really unexpected at first. I didn't know how to do it until I saw someone else's problem solving report.
Because as long as greatest common divisor >1, so the combination of even can certainly. But the odd is a bit difficult. It is not the most common case to make a pair in a double way. But more than two is the most common case, which is the practice in the top 20 code. I'm not trying to understand. It was only after I felt it. Since an odd plus two bit is less likely to be an even number, it repeats the combination of even numbers, and then the odd group can be combined into a pair. It's too witty. It's really hard to figure out the game.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map > #include <queue> #include <set> #include <bitset> #define MEM (b) memset (A,b,sizeof (a)) #define INF 1000000070000using namespace Std;typedef long long ll;typedef unsigned long long ull;int vis[100005],is[100005];vecto r<pair<int,int> >v;int Main () {int n,i,j; cin>>n; for (i=4;i<=n;i+=2) vis[i]=1; for (i=3;i<=n;i+=2) {if (!vis[i]) {if (i*2>n) break; vector<int>a; for (j=i;j<=n;j+=2*i) {vis[j]=1; if (!is[j]) A.push_back (j), Is[j]=1; } for (J=a.size () -1;j>0;j-=2) {V.push_back (Make_pair (a[j],a[j-1])); Is[a[j]]=is[a[j-1]]=1; } if (A.size () &1) {V.push_back (Make_pair (a[0],a[0]*2)); IS[A[0]]=IS[A[0]*2]= 1; }}} if (n&1) n--; int x=0,y; for (i=n;i>0;i-=2) {if (is[i]) continue; if (!x) y=i,x=1; Else X=0,v.push_back (Make_pair (i,y)); } printf ("%d\n", V.size ()); For (I=0;i<v.size (); i++) printf ("%d%d\n", v[i].first,v[i].second); return 0;}