Codeforces 449C Jzzhu and Apples math + primes

Source: Internet
Author: User

This topic at night originally spent a lot of XX, really feel that their thinking is right ah, review the idea, give you n number, divided into 22 pairs, divided into the most group how to divide, but the combination of two number can not be mutually, so that even is sure is good, so the first place, first put the prime number to fix, 10^ 5 So enumerate all the number that contains the prime factor, if the group is the best, otherwise, there are even to kick off a given to the following even processing part, and finally deal with even parts, so it is sure to meet the largest number of groups, there is no problem, and then the method is no problem ah, just the code has a problem, I am! That's a brain residue! , today I see a Daniel's idea, I and he is the same, but the code to write rubbing, and then changed and changed, but the original code really can't see Ah, alas write too cumbersome, or use STL convenient point, 10^5 even if time-consuming point also hurt, so knocked a STL, of course knocked several times to solve the error, really weak |! Clear and concise, not easy to make mistakes, but also a long memory good topic,


Title: Http://codeforces.com/problemset/problem/449/C

Recommend a great Daniel in this field: http://blog.csdn.net/houserabbit/article/details/37992617


#include <iostream> #include <cstdio> #include <list> #include <algorithm> #include <cstring > #include <string> #include <queue> #include <stack> #include <map> #include <vector># include<cmath> #include <memory.h> #include <set> #define LL long long#define eps 1e-8//const int inf = 0xfffffff;const ll inf = 1ll<<61;using namespace std;//vector<pair<int,int> > G;//typedef pair<int , int > p;//vector<pair<int,int> >:: Iterator Iter;////map<ll,int >mp;//map<ll,int;:: Iterator P;bool isprime[100000 + 5];int prime[100000 + 5];int k;void init () {memset (isprime,false,sizeof (IsPrime)); for ( int i=2;i<100005;i++) if (!isprime[i]) for (int j=i*2;j<100005;j+=i) isprime[j]=true;for (int i=2;i<100005;i++ ) if (!isprime[i]) prime[k++]=i;} int N;vector<pair<int,int> > G;void Clear () {memset (isprime,false,sizeof (IsPrime)); G.clear ();} int main () {init (); while (scanf ("%d", &n) = = 1) {clear (); int aNS = 0;vector<int > Tmp;vector<int >:: Iterator it;for (int i=1;i<k;i++) {if (Prime[i] > N) Break;tmp.clea R (); for (int j=prime[i];j<=n;j+=prime[i]) if (!isprime[j]) Tmp.push_back (j); int len = Tmp.size (); if (len = = 1) Continue if (len&1) {for (it = Tmp.begin (); It! = Tmp.end (); it++) if (*it%2 = = 0) {tmp.erase (it); break;}} Len = Tmp.size (); for (int i=0;i<len;i+=2) {ans++;isprime[tmp[i]]= true;isprime[tmp[i + 1]] = true; G.push_back (Make_pair (tmp[i],tmp[i + 1]));}} int TP = -1;for (int i=2;i<=n;i+=2) {if (!isprime[i]) {if (tp! =-1) {ans++;isprime[i] = true;isprime[tp] = true; G.push_back (Make_pair (tp,i)); tp =-1;} else TP = i;}} printf ("%d\n", ans); for (int i=0;i<ans;i++) printf ("%d%d\n", G[i].first,g[i].second);} return 0;}





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.