zoukankan      html  css  js  c++  java
  • (HDU 1016) Prime Ring Problem

    (HDU 1016) Prime Ring Problem

    思路:

    素数环,用数组来存储,相邻的两个数a[x] + a[x-1]要为素数;

    DFS, a[0] = 1, 从位置1开始尝试填入数字,填入的数字和前一个数字之和要为素数且填过数字不能重复,直到a[n-1]放好数字之后, 判断a[n-1]+a[0]是否为素数,如果是则输出结果。

    代码:

    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Main{
    	public static int a[];
    	public static boolean vis[];
    	public static int prime[] = new int[50];
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		gernerate_prime();//生成素数表
    		int cnt = 0;
    		while(in.hasNext()){
    			cnt++;
    			int n = in.nextInt();
    			a = new int[n];
    			vis = new boolean[n+1];
    			a[0] = 1;
    			vis[1] = true;
    			System.out.println("Case "+cnt+":");
    			dfs(1, n);
    			System.out.println();
    		}
    	}
    	private static void dfs(int i, int n) {
    		if(i == n && prime[a[n-1] + a[0]] == 1){
    			for(int j = 0; j < n-1; j++) 
    				System.out.print(a[j]+" ");
    			System.out.println(a[n-1]);
    			return ;
    		}
    		
    		for(int j = 1; j <= n; j++){
    			if(!vis[j] && prime[j+a[i-1]] == 1){ //判断数字是否填过,并且这个数字填入是否和前面数字之和为素数
    				vis[j] = true;
    				a[i] = j;
    				dfs(i+1, n);
    				vis[j] = false;
    			}
    		}
    	}
    	private static void gernerate_prime() {
    		int size = prime.length - 1;
    		Arrays.fill(prime, 1);
    		prime[1] = 0;
    		prime[2] = 1;
    		for(int i = 2; i < size; i++){
    			if(prime[i] == 1){
    				for(int j = i + i; j < size; j += i)
    					prime[j] = 0;
    			}
    		}
    	}
    }
    

     相似题目:

    HDU 1015 Safecracker

    用大小为5数组的存放数字,可选数字有12个,和上面一样先放0位置,直到5位置填充,判断是否符合题目条件v - w^2 + x^3 - y^4 + z^5 = target

    5个位置5层循环也可以代码:

    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Main{
        public static char a[];
        public static char c[];
        public static boolean ans;
        public static boolean vis[];
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            
            while(in.hasNext()){
                int n = in.nextInt();
                String s = in.next();
                if(n == 0 && s.equals("END"))
                    break;
                c = s.toCharArray();
                a = new char[5];
                vis = new boolean[c.length];
                Arrays.sort(c);
                ans = false;
                dfs(0, 5, n);
                if(ans){
                    for(int i = 0; i < 5; i++) 
                        System.out.print(a[i]);
                    System.out.println();
                }
                else 
                    System.out.println("no solution");
            }
        }
        
        private static void dfs(int i, int n, int target) {
            if(i == n){
                int sum = 0;
                for(int j = 0; j < 5; j++){
                    sum += pow(a[j]-'A'+1, j+1);
                }
                if(target == sum) 
                    ans = true;
                return ;
            }
            
            for(int j = c.length-1; j >= 0; j--){
                if(!vis[j]){
                    vis[j] = true;
                    a[i] = c[j];
                    dfs(i+1, n, target);
                    if(ans)
                        return ;
                    
                    vis[j] = false;
                }
            }
        }
        
        private static int pow(int x, int n) {
            int sum = 1;
            for(int i = 0; i < n; i++)
                sum *= x;
            
            if((n&1) == 1) 
                return sum;
            else 
                return -sum;
        }
    }
    
  • 相关阅读:
    B.Icebound and Sequence
    Educational Codeforces Round 65 (Rated for Div. 2) D. Bicolored RBS
    Educational Codeforces Round 65 (Rated for Div. 2) C. News Distribution
    Educational Codeforces Round 65 (Rated for Div. 2) B. Lost Numbers
    Educational Codeforces Round 65 (Rated for Div. 2) A. Telephone Number
    Codeforces Round #561 (Div. 2) C. A Tale of Two Lands
    Codeforces Round #561 (Div. 2) B. All the Vowels Please
    Codeforces Round #561 (Div. 2) A. Silent Classroom
    HDU-2119-Matrix(最大匹配)
    读书的感想!
  • 原文地址:https://www.cnblogs.com/IwAdream/p/5523559.html
Copyright © 2011-2022 走看看