很久之前做到过这个题,相较于上次,现在熟悉了dfs的模板后会更加轻松,但是在分块判断上出了一些问题。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(System.out);
static boolean st[] = new boolean[10];
static int a[] = new int[10];
static int res = 0, input = 0;
public static int cal(int l, int r) {
int ans = 0;
for (int i = l; i <= r; i++) ans = ans * 10 + a[i];
return ans;
}
public static void dfs(int u) {
if (u == 10) {
for (int i = 1; i <= 6; i++)
for (int j = i + 1; j < 10; j++) {
int zheng = cal(1, i);
int up = cal(i + 1, j);
int down = cal(j + 1, 9);
if (input * down == zheng * down + up) res++;
}
return;
}
for (int i = 1; i <= 9; i++) {
if (!st[i]) {
st[i] = true;
a[u] = i;
dfs(u + 1);
st[i] = false;
}
}
}
public static void main(String[] args) throws IOException {
input = Integer.parseInt(br.readLine());
dfs(1);
pw.println(res);
pw.flush();
pw.close();
br.close();
}
}