You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <bits/stdc++.h>
using namespace std ;
const int N = 30 ;
const int INF = 0x3f3f3f3f ;
int a [ N ] ;
bool st [ N ] ;
int n , k ;
int cnt ;
vector < int > path ;
bool isPrime ( int n ) {
if ( n < 2 ) return false ;
for ( int i = 2 ; i < = n / i ; i + + )
if ( n % i = = 0 ) return false ;
return true ;
}
/**
* 功能:遍历每个箱子,向箱子里装卡片
* @param step 走在第几个箱子面前
* @param sum 已经获得的数字和
* @param start 从哪个数字可以开始进行选择
* 举个栗子:
* 3+7+19
* 3+19+7
* 7+3+19
* 7+19+3
* 19+3+7
* 19+7+3 这样就出来6个, 肯定不行, 需要考虑只出1个!
* 方法就是“从上一次选择的数字后面一位进行再次选择!”
* @return 获取可行方法数
*/
void dfs ( int step , int sum , int start ) {
//当走到第k+1个虚拟箱子面前, 表示前面k个箱子已经装完
if ( step = = k + 1 ) {
if ( isPrime ( sum ) ) {
cnt + + ;
for ( int i = 0 ; i < path . size ( ) ; i + + )
cout < < path [ i ] < < " " ;
cout < < endl ;
}
return ;
}
//在没有走完箱子时,考虑当前箱子放哪张卡片
for ( int i = start ; i < = n ; i + + ) {
if ( ! st [ i ] ) {
st [ i ] = true ;
path . push_back ( i ) ;
dfs ( step + 1 , sum + a [ i ] , i + 1 ) ;
path . pop_back ( ) ;
st [ i ] = false ;
}
}
}
/*
4 3
3 7 12 19
答案: 1
*/
int main ( ) {
cin > > n > > k ;
for ( int i = 1 ; i < = n ; i + + ) cin > > a [ i ] ;
dfs ( 1 , 0 , 1 ) ;
cout < < cnt < < endl ;
return 0 ;
}