AcWing 1209. java版带分数先求a c 后求 b
原题链接
简单
作者:
北比臼
,
2021-03-01 21:20:19
,
所有人可见
,
阅读 344
import java.io.*;
public class Main{
static final int N = 10;
static int ans, n;
static boolean[] st = new boolean[N];
static boolean[] backup = new boolean[N];
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static boolean check(int a, int c) throws Exception{
long b = n * (long)c - c * a;
if(a <= 0 || b <= 0 || c <= 0) return false;
System.arraycopy(st, 1, backup, 1, N-1);
while(b > 0){
int x = (int)(b % 10);
b /= 10;
if(x == 0 || backup[x]) return false;
backup[x] = true;
}
for (int i = 1; i <= 9; i ++ )
if (!backup[i])
return false;
return true;
}
static void dfs_c(int u, int a, int c) throws Exception{
if(u > 9) return;
if(check(a, c)) ans++;
for(int i = 1; i <= 9; i++){
if(!st[i]){
st[i] = true;
dfs_c(u + 1, a, c * 10 + i);
st[i] =false;
}
}
}
static void dfs_a(int u, int a) throws Exception{
if(a >= n) return;
if(a > 0) dfs_c(u, a, 0);
for(int i = 1; i <= 9; i++){
if(!st[i]){
st[i] = true;
dfs_a(u + 1, a * 10 + i);
st[i] = false;
}
}
}
public static void main(String[] args) throws Exception{
StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
//PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
st.nextToken();
n = (int)st.nval;
dfs_a(0, 0);
bw.write(Integer.toString(ans));
bw.flush();
bw.close();
}
}