Different Digits
Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1430 Accepted Submission(s): 392
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.
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.
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.
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
15
16
101
0
Sample Output
7
555
16
1111
555
16
1111
Source
一个数论中的结论:对于任意的整数n,必然存在一个由不多于两个的数来组成的一个倍数。因为a,aa,aaa……取n+1个,则必有两个模n余数相同,相减即得n的倍数m。而m只由a、0组成。
有了上述结论+同余剪枝,这个题就能够做出来了,还有这题需要手动记录路径,不能让string 在结构体里面,不然TLE,记录路径的话,由于实在是找不到独一无二的标志,所以将每个节点作为独一无二的标志,手动模拟队列记录。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
using namespace std;
#define N 65536
typedef long long LL;
int n;
bool mod[N];
struct Node{
int val,step,mod,pre;
}q[N];
int a[];
int bfs(int k){
memset(mod,,sizeof(mod));
int head = ,tail = -;
for(int i=;i<=k;i++){
if(a[i]!=){
Node s;
s.val = a[i],s.step = ;
s.pre = -,s.mod = a[i]%n;
mod[s.mod] = true;
q[++tail] = s;
}
}
while(head<=tail){
Node now = q[head];
if(now.mod==){
return head;
}
for(int i=;i<=k;i++){
Node next;
next.mod = (now.mod*+a[i])%n;
next.pre = head;
next.val = a[i];
next.step = now.step+;
if(!mod[next.mod]){
mod[next.mod] = true;
q[++tail] = next;
}
}
head++;
}
return -;
}
string ans,res;
void getans(int k){
if(k==-) return ;
getans(q[k].pre);
ans+=(q[k].val+'');
}
int main(){
while(scanf("%d",&n)!=EOF,n){
res = "";
for(int i=;i<=;i++){
a[] = i;
int f = bfs();
if(f!=-){
ans = "";
getans(f);
if(res.compare("")==) res = ans;
else if(res.length()>ans.length()) res = ans;
}
}
if(res.compare("")!=){
cout<<res<<endl;
continue;
}
for(int i=;i<=;i++){
a[] = i;
for(int j=i+;j<=;j++){
a[] = j;
int f = bfs();
if(f!=-){
ans = "";
getans(f);
if(res.compare("")==) res = ans;
else if(res.length()>ans.length()||res.length()==ans.length()&&ans.compare(res)<) res = ans;
}
}
}
cout<<res<<endl;
}
}