[Los] Valley p1036 number of election

(I must declare too betel up , the title buckle for a morning ......)

Algorithms Tags:

……


 

dfs really not what I was good at qwq, the idea of ​​solving the problem is actually very simple, is the first dfs search all possible and then judgment is not a prime number. Said say, then the goose is not good to write:

The first idea is to use the linear sieve 2-1e8 are prime numbers between sieve out and then compared with the searched-out table after a prime ANS (fast feeling because after O (n) is incorrect) sieve and then found using linear pre- sieve not as direct prime O (root n) it qwq,

It is determined whether or not a prime number part:

bool pan(int g){
    for(int i=2;i<=sqrt(g);i++)
      if(g%i==0) return 0;
    return 1;
}

Then there is the big bow to the ground slippery world of dfs I learned a qwq:

dfs, there are three quantities: 1 every dfs algebra and the sum recorded; 2 records are now selected a few cnt number; 3 now selected a few to choose the final position... Last (designated focus);

last use:

Will not be used to ensure that both calculate 1 (1) + 5 (3) and calculates 5 (3) + 1 (1) case, where (x) represented by the following standard. last use: when dfs for loop, beginning from the last cycle, n to stop, so that both can be guaranteed not to calculate 1 (1) + 5 (3) and calculates 5 (3) + 1 (1) of the case (God knows how last used)

In short it is amazing, review the dfs:

1. often used in conjunction with recursion;

2. General dfs format:

int DFS ( int K) {
     IF (final conditions satisfied) { 
       output / storage ...... solution;
        return  0 ; 
    } 
    for ( int I = . 1 ; I <= a condition; I ++ ) {
          IF (condition satisfied) { 
            Save Results ; 
            DFS (next k); // this k may be different depending on the title solution; 
            backtracking; // put out before modifying the value change back again; 
          } 
     } 
}

 

in--------

answer:

#include <bits / STDC ++ H.> the using namespace STD; int n-, K, ANS;
 int X [ 30 ];
 BOOL D [ 30 ]; // determine whether the calculation has been BOOL PAN ( int G) { // judgment primes for ( int I = 2 ; I <= sqrt (G); I ++ )
       IF (% I G == 0 ) return 0 ;
     return . 1 ; 
} int DFS ( int SUM, int C, int Last) {
     IF

 




      

(C == k) { // If the number has been selected k 
        IF (PAN (SUM)) // determination thereof is prime and 
            ANS ++; // if, ANS ++ 
        return  0 ; // Return to the previous cycle 
    }
     for ( int I = last; I <= n-; I ++ ) {
         IF {(D [I]!) // If the number has not been counted 
            D [I] = . 1 ; last ++; // last ++ represents a last before the number has been added over again, and will not be coupled with the 
            dfs (X + SUM [I], C + . 1 , Last); // the dfs layer 
            D [I] = 0 ; 
        } 
    } 
    return  0;
}

int main(){
    
    scanf("%d%d",&n,&k);
    
    for(int i=1;i<=n;i++)
      scanf("%d",&x[i]);
    
    dfs(0,0,1);
    
    cout<<ans<<endl;
    
    return 0;
}

In addition, I want to attach some data, live up yky students to help me keep a noon:

100 5
3 3 2 5 6 1 5 4 55 6 156 89 89 262 1115 5626 48 44 665 92 15 1152 62 95 4 8 11 526 59 8 46 49 26 55 2 55 4 6 54 962 11 56 655 66 898 226 1 5 41 5 56 26 4 44 668 9 454 6 21 415 1 2563 96 4 5223 2 65 1 51 8 65 2 65 3 56 126 33 256 89 415 1 6 3 558 9 11 55 66 88 9 1 2 6 66 645 46 56 5 89 64 56 4 546 4651 5646 463 45

Guess you like

Origin www.cnblogs.com/zhuier-xquan/p/10959037.html