problem Description
CDFPYSW loves playing a card game called "Hearthstone". Now he had N cards, he wants to split these cards into 4 piles. Let's assume the number of cards in each pile is A1, A2, A3, A4. It must be satisfied that:a1 * K1 = a2 + a3 + a4 A2 * k2 = a1 + A3 + a4 a3 * K3 = A1 + a2 + A4 A1, a2, A3, A4 Must be positive Because cdfpysw are clever, there must be a-to split there card. Can you tell CDFPYSW?
Input
The first line was an integer T, means the number of cases. Then T-lines, each line contains 4 integers, N, K1, K2, K3, meaning of these has been shown in the description. T <= 1 <= N <= 109 1 <= K1, K2, K3 <= 200
Output
For each case, you should output "case #i:" First, I am the case number. Then output 4 integers a1, a2, A3, A4, represent the number of cards in each pile.
Sample Input
1120 2 3 4
Sample Output
Case #1:40 30 24 26
Import Java.util.scanner;public class Hearthstone {public static int[] A = {1,1,1,1,1};//based on depth search first backtracking public static void DF S (int deep,int n,int[] k,int i) {if (a[1] = = N) return;if (a[deep]==n) {///when child nodes traverse to N, backtrack a[deep-1]++;D FS (deep-1,n,k,i);} To the fourth layer, if there is a solution output and return, no solution continues to the right if (deep = = 4) {if ((a[1]+a[2]+a[3]+a[4]) = = N) {System.out.println ("Case #" +i+ ":" +a[1]+ "" +a[2]+ "+a[3]+" "+a[4"); return;} else{a[deep]++;D FS (deep,n,k,i);}} If the condition is satisfied then the search is not satisfied, then the pruning if (A[deep] < n && Deep < 4) {if (a[deep]*k[deep-1] = = (N-a[deep])) {DFS (deep+1,n,k,i);} else{a[deep]++;D FS (deep,n,k,i);}}} public static void Main (string[] args) {int t;int n[] = new Int[100];int k1[] = new Int[100];int k2[] = new Int[100];int k 3[] = new Int[100];int a=1,b=1,c=1,d=1; Scanner Scanner = new Scanner (system.in); T = Scanner.nextint (); for (int i = 1;i <= t;i++) {N[i] = Scanner.nextint (); K1[i] = Scanner.nextint (); K2[i] = Scanner.next Int (); K3[i] = Scanner.nextint ();} Solve for each set of instances for (int i = 1;i <= t;i++) {int[] k = {K1[i],k2[i],k3[i]};D FS (1,n[i],K,i);} The first five-story cycle. Drunk/*for (int i = 1;i <= t;i++) {for (a = 1;a<=n[i];a++) {if (a*k1[i] = = (n[i]-a)) for (b = 1;b<=n[i]-a;b++) {if (b*k2[i] = = (N[i]-b)) (c = 1;c<=n[i]-a-b;c++) {if (c*k3[i] = = (n[i]-c)) for (d = 1;d<=n[i]-a-b-c;d++) {if ((a+b+c+d) = = N[i]) { System.out.println ("Case #" +i+ ":" +a+ "" +b+ "" +c+ "" +d);}}}}} */}}
Hangzhou Electric OJ 15th ACM first question Hearthstone