Codeforces 449C Jzzhu and Apples 贪心 (看题解)
从大的质因子开始贪心, 如果有偶数个则直接组合, 如果是奇数个留下那个质数的两倍, 其余两两组合。
#include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ull unsigned long long using namespace std; const int N = 1e5 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); int n; bool vis[N]; bool prime[N]; queue<int> que; vector<PII> ans; int main() { memset(prime, true, sizeof(prime)); prime[0] = prime[1] = false; for(int i = 2; i < N; i++) { if(!prime[i]) continue; for(int j = i * 2; j < N; j += i) prime[j] = false; } scanf("%d", &n); for(int i = n; i >= 2; i--) { if(!prime[i]) continue; for(int j = 3 * i; j <= n; j += i) if(!vis[j]) que.push(j); que.push(i); if(2 * i <= n && !vis[2 * i]) que.push(2 * i); while(SZ(que) >= 2) { int x = que.front(); que.pop(); int y = que.front(); que.pop(); vis[x] = vis[y] = true; ans.push_back(mk(x, y)); } while(SZ(que)) que.pop(); } printf("%d\n", SZ(ans)); for(auto& t : ans) printf("%d %d\n", t.fi, t.se); puts(""); return 0; } /* */