0%

[題解]除法

a154. 除法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define Orz ios::sync_with_stdio(0),cin.tie(0)
#define N 51
#define FOR(i,n) for(int i=0;i<n;i++)
#define pii pair<int,int>
#define pid pair<int,double>
#define pdi pair<double,int>
using namespace std;
int n,m;
bool used[10],f;

bool check(int t){
bool cur_used[10]={0};
if(t<10000&&used[0]==1)return 0;
else if(t<10000)cur_used[0] = 1;

while(t>0){
int d = t%10;
if(cur_used[d])return 0;
else if(used[d])return 0;
cur_used[d] = 1;
t /= 10;
}
return 1;
}

void dfs(int first,int id){
if(id>=5){
if(first%n!=0)return;
int second = first/n;
if(second<1234)return;
if(check(second)){
f = 1;
cout<<first<<" / ";
if(second<10000)cout<<0;
cout<<second<<" = "<<n<<endl;
return;
}
}
for(int i=0;i<10;i++){
if(used[i])continue;
used[i] = 1;
dfs(first*10+i,id+1);
used[i] = 0;
}
}

void solve(){
while(cin>>n){
if(n==0)return;
memset(used,0,sizeof(used));
f=0;
dfs(0,0);
if(f==0)cout<<"There are no solutions for "<<n<<"."<<endl;
cout<<endl;
}
}

signed main(){
Orz;
int t = 1;
while(t--){
solve();
}
}