http://acm.hdu.edu.cn/showproblem.php?pid=1016
#include<iostream> #include<cstdio> #include<cstring> using namespace std ; int n ; int union_prime( int x ) { for( int i = 2 ; i < x ; ++i ) if( x % i == 0 ) return 0 ; return 1 ; } int arr[ 30 ] , hash[ 30 ] ; void dfs( int order , int index ) { arr[ order ] = index ; hash[ index ] = 1 ; if( order == n ) { // printf( "1" ) ; if( union_prime( arr[ order ] + arr[ 1 ] ) ) { printf( "1" ) ; for( int i = 2 ; i <= n ; ++i ) { printf( " %d" , arr[ i ] ) ; } printf( "\n" ) ; } } for( int i = 1 ; i <= n ; ++i ) { if( !hash[ i ] && union_prime( arr[ order ]+ i ) ) { dfs( order + 1 , i ) ; hash[ i ] = 0 ; } } return; } int main() { int flag = 0; while( cin >> n) { memset( hash , 0 , sizeof( hash ) ) ; printf( "Case %d:\n" , ++flag ) ; if( !n ) break ; dfs( 1 , 1 ) ; printf( "\n" ) ; } return 0 ; }