Different Digits
Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1129 Accepted Submission(s): 290
Problem Description
Given a positive integer n, your task is to find a positive integer m, which is a multiple of n, and that m contains the least number of different digits when represented in decimal. For example, number 1334 contains three different digits 1, 3 and 4.
Input
The input consists of no more than 50 test cases. Each test case has only one line, which contains a positive integer n ( 1<=n < 65536). There are no blank lines between cases. A line with a single `0’ terminates the input.
Output
For each test case, you should output one line, which contains m. If there are several possible results, you should output the smallest one. Do not output blank lines between cases.
Sample Input
7
15
16
101
0
Sample Output
7
555
16
1111
Source
2004 Asia Regional Shanghai
Recommend
xhd | We have carefully selected several similar problems for you: 1667 1668 1666 1661 1662
感觉是ZOJ1136 的高级版http://blog.csdn.net/u013167299/article/details/47170483
#include<bits/stdc++.h>
#define cl(a,b) memset(a,b,sizeof(a));
#define LL long long
#define out(x) cout<<x<<endl;
using namespace std;
const int maxn=65540;
const int inf=9999999;
int n;
int a[2];
int used[maxn];
struct node{
int pre,d,mod,id;//pre是该节点的前驱节点,d是当前加在末尾的数,mod是到目前为止组合的数对n的余数,id是编号,便于查找,
node(){}
node(int a,int b,int c,int tt){
pre=a;d=b;mod=c;id=tt;
}
}q[maxn];//直接模拟队列,便于记录路径
string ans,tmpans;
bool bfs(int m){
int sum=0;
int st=0,en=0;
cl(used,0);
for(int i=0;i<m;i++){
q[en++]=node(-1,a[i],a[i]%n,sum++);
if(a[i])used[a[i]%n]=1;
}
int len=0;
while(st<en){
node s=q[st++];
if(s.mod==0&&(s.id>0||s.id==0&&s.d!=0)){
tmpans="";
tmpans+=char(s.d+‘0‘);
int t=s.pre;
while(t!=-1){//路径转化成字符串
tmpans+=q[t].d+‘0‘;
t=q[t].pre;
}
reverse(tmpans.begin(),tmpans.end());
//out(tmpans)
return true;
}
for(int i=0;i<m;i++){
node tmp=s;
if(len==0&&tmp.d==0&&tmp.mod==0)continue;
if(used[(tmp.mod*10+a[i])%n]++)continue;
len++;
q[en++]=node(s.id,a[i],(s.mod*10+a[i])%n,sum++);
}
}
return false;
}
int main(){
while(~scanf("%d",&n)&&n){
ans="zcc";
for(int i=1;i<10;i++){
a[0]=i;
if(bfs(1)){
if(ans=="zcc")ans=tmpans;
else if(tmpans.size()<ans.size()){
ans=tmpans;
}
else if(ans.size()==tmpans.size()){
ans=min(ans,tmpans);
}
}
}
if(ans=="zcc")for(int i=0;i<10;i++){
a[0]=i;
for(int j=i+1;j<10;j++){
a[1]=j;
if(bfs(2)&&tmpans!="0"){
if(ans=="zcc")ans=tmpans;
else if(tmpans.size()<ans.size()){
ans=tmpans;
}
else if(ans.size()==tmpans.size()){
ans=min(ans,tmpans);
}
}
}
}
out(ans)
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。