简单回溯。dfs枚举除数,然后除数乘商,判断被除数是否有重复数字。注意一个剪枝,由于被除数长度至多是10,减去商的长度,就是除数的最大长度。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#define ll long long
#define INF 2139062143
#define MAXN 10000
//ios::sync_with_stdio(false);
using namespace std;
bool Judge(ll val)
{
bool vis[12]= {0};
while(val)
{
if(vis[val%10]) return false;
else vis[val%10]=true;
val=val/10;
}
return true;
}
int Getlong(ll val)
{
int res=0;
while(val)
{
val/=10;
res++;
}
return res;
}
ll val;
int l;
vector<ll> vec;
bool use[12]= {0};
void dfs(int cur,ll res)
{
if(cur==l) return;
for(int i=0; i<=9; ++i)
if(!use[i])
{
if(cur==0&&i==0) continue;
use[i]=true;
if(Judge((res*10+i)*val))
vec.push_back(res*10+i);
dfs(cur+1,(res*10+i));
use[i]=false;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld",&val);
memset(use,0,sizeof(use));
vec.clear();
l=11-Getlong(val);
dfs(0,0);
sort(vec.begin(),vec.end());
for(int i=0; i<vec.size(); ++i)
printf("%lld / %lld = %lld\n",val*vec[i],vec[i],val);
if(T) printf("\n");
}
return 0;
}