[2016-04-27] [Codeforces] [665d-simple subset]

Source: Internet
Author: User

    • Time: 2016-04-27-15:16:14
    • Title Number: [2016-04-27][codeforces][665d-simple subset]
    • The main idea: Given the n number of set A, ask the subset of the maximum size, so that the sum of 22 of the sub-set is a prime, output and any one such subset
    • Analysis:
      • At most one 1:
        • First take any 3 numbers from a
        • If there are at most 1 of these 3 numbers, 1
        • By the principle of tolerance, there must be two numbers of the sum is even, and the sum of the even greater than 2 (must not be a prime number), so the answer subset, more than 1 of the numbers can only have two,
        • Spread to the whole A, that is, if a is at most 1 1, the subset has a maximum of 2 digits, then the answer is
          1. The sum of no two numbers is a prime number,
          2. There is a sum of two numbers that are primes, (even if there are many pairs, only one pair can be output)
      • If more than 1, then the answer is
        1. It's all 1.
        2. In addition to 1, there is also a number, the number +1 is the prime
  
 
  1. #include<cstdio>
  2. #include<cstring>
  3. using namespace std;
  4. const int maxn = 1E3 + 10;
  5. const int maxp = 1E6*2 + 10;
  6. int isnpri[maxp],a[maxn];
  7. void getpri(){
  8. memset ( isnpri 0 sizeof isnpri
  9. for ( int i = 2 ; I < Maxp + 10 ; ++ i ) {
  10. if(isnpri[i]) continue;
  11. for(int j = i * 2 ; j < maxp; j += i){
  12. isnpri[j] = 1;
  13. }
  14. }
  15. }
  16. int main(){
  17. getpri();
  18. int n,cnt1 = 0;
  19. scanf("%d",&n);
  20. for(int i = 0 ; i < n ; ++i){
  21. scanf("%d",&a[i]);
  22. if(a[i] == 1) ++cnt1;
  23. }
  24. if(cnt1 > 1){
  25. for ( int i = 0 ; I < n ++ i ) {
  26. if ( a [ i != 1 && ! isnpri [ a [ i + 1
  27. printf("%d\n%d",cnt1 + 1,a[i]);
  28. for(int j = 0 ; j < cnt1;++j){
  29. printf(" 1");
  30. }
  31. return 0;
  32. }
  33. }
  34. printf("%d\n",cnt1);
  35. for(int j = 0 ; j < cnt1;++j){
  36. printf("1 ");
  37. }
  38. return 0;
  39. }
  40. for ( int i = 0 ; I < n ++ i ) {
  41. for ( int J = 0 ; J < n ++ j ) {
  42. if ( i != J && ! isnpri [ a [ i + a [ j ]) {
  43. printf("2\n%d %d",a[i] , a[j]);
  44. return 0;
  45. }
  46. }
  47. }
  48. printf("1\n%d",a[0]);
  49. return 0;
  50. }


From for notes (Wiz)

[2016-04-27] [Codeforces] [665d-simple subset]

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.