AcWing 1209. 带分数
原题链接
简单
作者:
Value
,
2020-07-03 14:26:42
,
所有人可见
,
阅读 572
/*
n = a + b / c
=> n * c = a * c + b
=> n * c - a * c = b(用于验证)
*/
#include <iostream>
#include <cstring>
using namespace std;
const int N = 10;
bool flag[N], backup[N];
typedef long long ll;
ll n, cnt;
bool check(ll a, ll c){
ll b = (n - a) * c;
if(!b) return false;
memcpy(backup, flag, sizeof flag);
bool success = true;
while(b){
if(!(b % 10) || backup[b % 10]){
success = false;
break;
}
backup[b % 10] = true;
b /= 10;
}
for(int i = 1; i < 10; i ++ ){
if(!backup[i]){
success = false;
break;
}
}
return success;
}
void dfs_c(ll a, ll c){
if(c && check(a, c)){
cnt ++ ;
return ;
}
for(int i = 1; i < 10; i ++ ){
if(!flag[i]){
flag[i] = true;
dfs_c(a, c * 10 + i);
flag[i] = false;
}
}
}
void dfs_a(ll a){
if(a >= n) return ;
if(a) dfs_c(a, 0);
for(int i = 1; i < 10; i ++ ){
if(!flag[i]){
flag[i] = true;
dfs_a(a * 10 + i);
flag[i] = false;
}
}
}
int main(){
cin >> n;
dfs_a(0);
cout << cnt << endl;
return 0;
}