The title means to find out that all adjacent numbers are added as a sequence of primes between all sequences from 1 to N. Ps: The title is ring, so the head and tail also to calculate OH ~
Typical DFS, and then prune.
The interesting thing is to use Java to run back to the edge of the tle, the first commit on the TLE (server load Problem bar), the same second commit on the AC, the side also reflects the recursion to stack overhead impact efficiency is also serious. Okay, on the code!!
Topic Portal:
hdu_1016
ImportJava.util.Scanner; Public classMain { Public Static Final int[] prime = {0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0 , 1, 0, 0, 0, 1 }; Public Static Boolean[] visited; Public Static int[] nums; Public Static intN; Public Static voidDfsintDeepth,intCurrent ) { if(Deepth = =N) { if(prime[current+1]==1){ for(inti=0;i<n;i++) {System.out.print (nums[i]); if(i+1!=N) {System.out.print (" " ); }} System.out.println (); } } Else { for(inti=2;i<=n;i++){ if(!visited[i] && prime[current+i]==1) {Visited[i]=true; Nums[deepth]=i; DFS (deepth+1, i); Visited[i]=false; } } } } Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); intC=1; while(Sc.hasnext ()) {n=Sc.nextint (); Nums=New int[N+1 ]; Visited=New Boolean[N+1 ]; nums[0]=1; System.out.println ("Case" +c+ ":"); DFS (The); C++; System.out.println (); } }}
[hdu]1016 DFS entry question