Codeforces 449C Jzzhu and Apples (construction)

Source: Internet
Author: User

Title Link: codeforces 449C jzzhu and Apples

The main idea: Jzzhu from the apple tree to obtain n apples, marking from the 1~n, now they will be two for a group to sell to the merchant, asked for a group of two apples in the number greatest common divisor greater than 1, the number of groups as many as possible.

How to solve the problem: Enumerate the number of conventions D, only enumeration primes, because composite can be enumerated in smaller prime numbers. Take out all the apples that have not been used and are numbered in multiples of D, 22 teams, and if the number is odd, leave 2d. Because D>2, so the 2nd one must be 2d. And 2d is a multiple of 2.

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace STD;typedefpair<int,int> PII;Const intMAXN =1e5;BOOLiscomp[maxn+5], vis[maxn+5];voidPrime_table (intN) { for(inti =2; I * I <= N; i++) {if(Iscomp[i])Continue; for(intj = i * I; J <= N; J + = i) iscomp[j] =1; }}intMain () {intNscanf("%d", &n); Prime_table (n); vector<int>G vector<pii>Ans for(inti = n/2; i >1; i--) {if(Iscomp[i])Continue; G.clear (); for(intj = i; J <= N; J + = i) {if(Vis[j] = =0) G.push_back (j); }if(G.size () &1) Swap (g[1], g[g.size ()-1]); for(inti =0; I < g.size ()-1; i + =2) {Ans.push_back (Make_pair (g[i), g[i+1])); Vis[g[i]] = vis[g[i+1]] =1; }    }printf("%lu\n", Ans.size ()); for(inti =0; I < ans.size (); i++)printf("%d%d\n", Ans[i].first, Ans[i].second);return 0;}

Codeforces 449C Jzzhu and Apples (construction)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.