AcWing 1209. 带分数(java版本,y总思路一致)
原题链接
简单
作者:
Elegant
,
2021-03-09 18:52:25
,
所有人可见
,
阅读 450
import java.io.*;
import java.util.*;
public class Main {
static Scanner in=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
static int answer=0;
static int[] had_use=new int [20];
static int[] ever=new int [20];
static int n=0;
public static boolean check(long a,long c)
{
long b=c*n-c*a;//这里注意一个细节,如果不用long的话可能会爆int
if(a==0||b==0||c==0)
return false;
for(int i=1;i<=10;i++)
ever[i]=had_use[i];
while(b>0)
{
long x=b%10;
b/=10;
if(x==0||ever[(int) x]==1)
return false;
ever[(int) x]=1;
}
for(int i=1;i<=9;i++)
if(ever[i]==0)
return false;
return true;
}
public static void dfs_c(int u,int a,int c)
{
if(u>=10)return;
if(check(a,c))answer++;
for(int i=1;i<=9;i++)
if(had_use[i]==0)
{
had_use[i]=1;
dfs_c(u+1,a,c*10+i);
had_use[i]=0;
}
}
public static void dfs_a(int u,int a)
{
if(a>=n)return;
dfs_c(u,a,0);
for(int i=1;i<=9;i++)
if(had_use[i]==0)
{
had_use[i]=1;
dfs_a(u+1,a*10+i);
had_use[i]=0;//回溯很重要哈
}
}
public static void main(String[] args)throws IOException
{
n=in.nextInt();
dfs_a(0,0);
System.out.println(answer);
}
}