Codeforces 450E:Jzzhu and Apples(构造,数学)

E. Jzzhu and Apples

time limit per test: 1 seconds

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?≤?10^5)\), 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.

input

6

output

2
6 3
2 4

input

9

output

3
9 3
2 4
6 8

input

2

output

0

题意

给出正整数\(n\),求出\(\left[1,n\right]\)之间的正整数有多少对数字的最大公约数不等于\(1\),输出最多的组数,并按任意顺序输出这些数字

思路

要使\(gcd(x,y)>1\),那么\(x,y\)中的较小的数一定不大于\(n/2\),所以我们首先筛出来\([1,n/2]\)范围内的素数

筛出来素数之后,每次在取数的时候,要保证取完数之后,不会使总的符合要求的数对减少,所以我们从最大的素数(假设为\(x\))开始枚举,能够整除\(x\)的整数一定使最少的,而且不会影响到别的数对(感性理解一下:因为枚举到了\(n/2\),所以\(n/x<=3\),\(2\)的倍数还是很多的减少一个没什么影响,\(n/x=3\)时,大概只有\(n=9\)的时候,那么也是不会产生影响的)

然后去统计当前素数\(p\)的倍数个数\(num\),如果\(num\)是偶数,直接匹配(为了简便,就相邻的两个数组成一对)

如果\(num\)是奇数,我们可以将\(p\)的\(2\)倍和\(num\)倍交换位置,这样匹配完剩下的那个数可以去和\(2\)的倍数来匹配,这样可以达到最优

代码

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
int vis[maxn];
int prime[maxn];
int cnt;
int a[maxn];
void get_prime(int n)
{
    vis[0]=vis[1]=1;
    for(int i=2;2*i<=n;i++)
        if(!vis[i])
            for(int j=2;j*i*2<=n;j++)
                vis[j*i]=1;
    for(int i=2;2*i<=n;i++)
        if(!vis[i])
            prime[cnt++]=i;
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("/home/wzy/in", "r", stdin);
        freopen("/home/wzy/out", "w", stdout);
        srand((unsigned int)time(NULL));
    #endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    get_prime(n);
    ms(vis,0);
    vector<pair<int,int> >ve;
    for(int i=cnt-1;i>=0;i--)
    {
        int tot=0;
        for(int j=prime[i];j<=n;j+=prime[i])
            if(!vis[j])
                a[++tot]=j;
        // 如果倍数有奇数个,交换第二个和最后一个
        if(tot&1)
            swap(a[2],a[tot]);
        for(int j=1;j+1<=tot;j+=2)
        {
            vis[a[j]]=vis[a[j+1]]=1;
            ve.push_back({a[j],a[j+1]});
        }
    }
    cout<<ve.size()<<endl;
    for(auto i:ve)
        cout<<i.first<<" "<<i.second<<endl;
    #ifndef ONLINE_JUDGE
        cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
    #endif
    return 0;
}

原文地址:https://www.cnblogs.com/Friends-A/p/11656141.html

时间: 2024-10-08 06:37:37

Codeforces 450E:Jzzhu and Apples(构造,数学)的相关文章

Codeforces 449C Jzzhu and Apples(构造)

题目链接:Codeforces 449C Jzzhu and Apples 题目大意:Jzzhu从苹果树上获得n个苹果,标号从1~n,现在要将他们以两个为一组卖给商家,要求一组中的两个苹果的编号最大公约数大于1,分的组数尽量多. 解题思路:枚举公约数d,只枚举素数,因为合数的可以在更小的素数被枚举.将所有没用过并且编号为d的倍数的苹果拿出来,两两组队,如果个数为奇数,那么就将2d留出来.因为d>2,所以第2个肯定是2d.并且2d是2的倍数. #include <cstdio> #incl

CodeForces 449C Jzzhu and Apples 数学+素数

这道题目晚上本来就花了很多把都××了,着实觉得自己思路没错啊,回顾一下思路,给你n个数,分成两两组合一对,分成最多组如何分,但是组合的两个数 不能互素,所以呢 偶数肯定是好的了,所以先放着,先把素数给搞定,10^5所以枚举所有包含该素数因子的数,如果刚好分组则最好,不然的话其中有偶数的踢掉一个给下面的偶数处理部分,最后再处理偶数的部分,这样肯定满足组数最多,完全没有问题,后来方法确实是没问题啊,只是代码有问题,我靠!真是脑残!,今天看到一位大牛的想法,我跟他是一样的,只是代码写搓了,后来改了又改

Codeforces Round #257 (Div. 2) E题:Jzzhu and Apples 模拟

E. 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

【矩阵快速幂 】Codeforces 450B - Jzzhu and Sequences (公式转化)

[题目链接]click here~~ [题目大意] Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, please calculate fn modulo1000000007(109?+?7). [解题思路] /*A - Jzzhu and Sequences Codeforces 450B - Jzzhu and Sequences ( 矩阵快速幂 )

CF449C Jzzhu and Apples (筛素数 数论?

Codeforces Round #257 (Div. 1) C Codeforces Round #257 (Div. 1) E CF450E 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 tre

CodeForces 23C Oranges and Apples 抽屉原理

题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <map> #include <set> #include <math.h> using namespace std; #define inf 10000000 #define l

Codeforces 429B Working out bfs构造

题目链接:点击打开链接 题意:给定n*m的矩阵 有一个人a从左上角走到右下角,只能↓或→走 另一个人b从左下角走到右上角,只能↑或→走 使得2个人的路径有且仅有一个格子是相交的. 统计2个人的权值和(相交格子的权值和不计) 问最大的权值和是多少. 思路: 首先转换一下题意,也就是找一个格子与4个角落连不相交的线. 我们观察相交的那个格子,那个格子的上下左右必然对应着一个角落. (i,j)点,那么(i-1,j)必然对应左上角或右上角的其中一个角落. 这样(i,j)点的4个相邻格子各自对应一个角落(

Codeforces 346C Number Transformation II 构造

题目链接:点击打开链接 = = 990+ms卡过 #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<vector> #include<set> using namespace std; #define N 100010 #define L(x) (x<<1) #define R(x) (x<<

Codeforces 449B Jzzhu and Cities(最短路)

题目链接:Codeforces 449B Jzzhu and Cities 题目大意:Jzzhu是一个国家的总统,这个国家有N座城市,以1为首都,已经存在了M条公路,给定M条路.并且还有K条铁轨,铁轨均有首都出发,连接si,距离为yi.现在Jzzhu想要节省经费,拆除一些铁轨,问说最多能拆除多少个铁轨,要求每座城市与首都的最短距离不变. 解题思路:最短路,多加一个标记数组,每个si标记1,如果这些点的最短距离被更新,则将对应si的标记清为0,最后统计一下剩余的标记,即为不能拆除的铁轨. #inc