1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| import java.io.*; import java.util.*; public class Main { static int[]shu={1,2,3,4,5,6,7,8,9}; public static void main(String[] args) { Scanner sc=new Scanner(System.in); PrintWriter pw=new PrintWriter(System.out); Permutation p=new Permutation(); while(sc.hasNext()){ int n=sc.nextInt(); int count=0; for(int i=0;i<9;i++) shu[i]=i+1; while(check()){ int left=0; for(int i=0;i<String.valueOf(n).length();i++){ left=cal(0,i); for(int j=i+1;j<=7;j++){ int up=0; int down=0; up=cal(i+1,j); down=cal(j+1,8); if(up%down==0) if(n==left+up/down) count++; } } p.nextPermutation(shu); } pw.println(count); pw.flush(); } } static int cal(int start,int end){ int sum=0; for(int t=start;t<=end;t++){ sum=sum*10+shu[t]; } return sum; } static boolean check(){ for(int i=0;i<shu.length;i++){ if(shu[i]!=9-i)return true; } return false; } } class Permutation { public void nextPermutation(int[] nums) { if(nums.length <= 1){ return; } int i = nums.length - 2; while(i >= 0 && nums[i] >= nums[i + 1]){ i--; } if(i >= 0){ int j = nums.length - 1; while(j > i && nums[j] <= nums[i]){ j--; } swap(nums, i, j); } reverse(nums, i + 1, nums.length - 1); } private void swap(int[] nums, int i, int j){ int tmp = nums[j]; nums[j] = nums[i]; nums[i] = tmp; } private void reverse(int[] nums, int left, int right){ while(left < right){ swap(nums, left, right); left++; right--; } } }
|