Codeforces Round #257 (Div. 1) C. Jzzhu and Apples(构造题,想法题,好题)

来源:互联网 发布:游族网络最新消息 编辑:程序博客网 时间:2024/06/12 00:55

题目链接
C. Jzzhu and Apples
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to an apple store. 

Jzzhu will pack his apples into groups and then sell them. Each group must contain two apples, and the greatest common divisor of numbers of the apples in each group must be greater than 1. Of course, each apple can be part of at 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 two integers — the numbers of apples in the current group.

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

Examples
input
6
output
26 32 4
input
9
output
39 32 46 8
input
2
output
0

题意:

给出n(1 <= n <= 10^5)个数,从1到n,求出不互质的数对最多有多少对(每个数只能用一次,且最大公约数大于1)并将这几对数输出。



题解:

首先,1 和大于 n / 2 的素数,这些数可以直接删掉,因为它们不可能和其它任何数配对。
由于2的倍数的数非常多,而且任意两个都能成对,为了不影响后面的配对,我们最后考虑2的倍数。
因此,接下来先对于所有素数 小于n的素数x(2 <  x <= n / 2),在剩下的未选的数中找出 x 的倍数,看这些数的数量是否为偶数,为偶数可以直接两两配对,如果为奇数,则忽略 x*2,将剩下的数两两配对。
最后只剩下2的倍数的数没有被配对,将他们两两配对即可。

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<queue>#include<stack>#include<set>using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define pb push_back#define fi first#define se second#define mp make_pairtypedef vector<int> VI;typedef long long ll;typedef pair<int,int> PII;const int inf=0x3fffffff;const ll mod=1000000007;const int maxn=1e5+100;int prime[maxn];int vis[maxn];void getPrim(){    for(int i=2;i<maxn;i++)    {        if(!prime[i])        {            prime[++prime[0]] = i;        }        for(int j=1;(j<=prime[0])&&(i*prime[j]<maxn);j++)        {            prime[prime[j]*i] = 1;            if(i%prime[j]==0) break;        }    }}int n;VI V;vector<PII> ans;int main(){    getPrim();    while(~scanf("%d",&n))    {        memset(vis,0,sizeof(vis));        V.clear();        ans.clear();        vis[1]=1;        int j=1;        while(prime[j]*2<=n) j++;        while(prime[j]<=n&&j<=prime[0]) vis[prime[j]]=1,j++;        int res=0;        for(int j=2;prime[j]*2<=n;j++)        {            int num=0;            int i=prime[j];            while(i<=n)            {                if(!vis[i]) num++;                i+=prime[j];            }            if(num&1)            {                vis[prime[j]*2]=1;                V.pb(prime[j]*2);                num--;            }            if(num>=2)            {                int cnt=1;                int p=prime[j];                int t=prime[j];                while(p<=n)                {                    p+=prime[j];                    if(!vis[p])                    {                        cnt++;                        if(cnt&1) t=p;                        else                        {                            ans.pb(make_pair(t,p));                            vis[t]=vis[p]=1;                        }                    }                }            }            res+=num/2;        }        for(int i=2;i<=n;i+=2)            if(!vis[i]) V.pb(i);        res+=V.size()/2;        printf("%d\n",res);        for(int i=0;i<ans.size();i++)            printf("%d %d\n",ans[i].first,ans[i].second);        for(int i=0;i+1<V.size();i+=2)            printf("%d %d\n",V[i],V[i+1]);    }    return 0;}


0 0
原创粉丝点击