【codeforces 449C】Jzzhu and Apples

【题目链接】:http://codeforces.com/problemset/problem/449/C

【题意】

给你n个数字;
然后让你选出很多个不相交的对;
要求这些对的最大公因数都大于等于2;
问你最多的对数;

【题解】

首先;
数字1以及大于n/2的质数,它们肯定不能构成答案;(没办法有gcd>1的方案);
然后就枚举小于等于n/2的质数p;
对于它的倍数i*p;
看看哪些数字没被选走;
统计个数len;
如果len为偶数,那么就美滋滋,两两配对就好;
如果len为奇数,那么就把2*p拿走,不配对,剩下的两两配对;
这样就能把除了少部分偶数的数字全都配对了;
剩下的偶数在p==2的时候也能尝试配对;
让配对最大化了;

【Number Of WA

1

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100;

int n;
bool iszs[N],vis[N];
vector <int> v,v1;
vector <pii> ans;

int main(){
    //Open();
    Close();//scanf,puts,printf not use
    //init??????
    cin >> n;
    ms(iszs,1);
    rep1(i,2,n){
        if (iszs[i]) v.pb(i);

        rep1(j,0,(int) v.size()-1){
            int t = v[j];
            if (i*t>n) break;
            iszs[i*t] = false;
            if (i%t==0) break;
        }
    }
    rep2(i,n/2,2)
        if (!vis[i] && iszs[i]){
            v1.clear();
            for (int j = i;j<=n;j+=i)
            if (!vis[j]){
                v1.pb(j);
            }
            int len = v1.size();
            if (len%2==1){
                swap(v1[1],v1[len-1]);
            }
            for (int j = 0;j < len-1;j+=2){
                vis[v1[j]] = vis[v1[j+1]] = 1;
                ans.pb(mp(v1[j],v1[j+1]));
            }
        }
    cout << (int) ans.size() << endl;
    rep1(i,0,(int) ans.size()-1){
        cout << ans[i].fi <<' '<< ans[i].se << endl;
    }
    return 0;
}
posted @ 2017-10-04 18:44  AWCXV  阅读(147)  评论(0编辑  收藏  举报