Codeforces Round #257 (Div. 1) C. Jzzhu and Apples(构造题,想法题,好题)
2017-02-23 22:19
507 查看
题目链接
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
output
input
output
input
output
题意:
给出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_pair
typedef 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;
}
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
2 6 3 2 4
input
9
output
3 9 3 2 4 6 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_pair
typedef 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;
}
相关文章推荐
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(想法题,构造题)
- codeforces 449C C. Jzzhu and Apples(数论+构造)
- CodeForces 449 C.Jzzhu and Apples(构造+数论)
- Codeforces Round #257 (Div. 2) D题:Jzzhu and Cities 删特殊边的最短路
- 【VK Cup 2016 - Round 1 (Div 2 Edition)C Bear and Forgotten Tree 3 (构造)
- 449 C. Jzzhu and Apples
- Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence 构造
- Codeforces Div. 2 #257-A. Jzzhu and Children
- Codeforces Round #257 (Div. 2) C. Jzzhu and Chocolate
- Codeforces Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)
- Codeforces #324 Div2 C Marina and Vasya(构造)
- Codeforces Round #381 (Div. 1) A. Alyona and mex 构造
- Codeforces Round #281 (Div. 2)D. Vasya and Chess(博弈,想法题)
- Codeforces Round #285 (Div. 2) C. Misha and Forest 构造
- cf Round #292 (Div. 2)D. Drazil and Tiles 构造
- Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)
- Codeforces Round #252(Div. 2) 441C. Valera and Tubes 构造
- CF449 C. Jzzhu and Apples
- Codeforces #257 (Div. 2) B. Jzzhu and Sequences
- Codeforces Round #257 (Div. 2) A. Jzzhu and Children