Different Digits
Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 464 Accepted Submission(s): 106
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
Recommend
xhd
思路:根据抽屉原理,可以先搜只有一位数字的,然后从小到大搜索两位数字的
但是我在搜索两位数字的时候,采用暴力搜索,结果毫无疑问的超时了,悲剧
了,在3170上卡了很久,而且在以30,40,70,80,20,90,结尾时时间开销有点
大
代码:
超时(暴搜)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int n;
int flag,temp,sum[100],step;
int num[100];
void BFS(int a)
{
int biji;
int map[10000];
int qmap = 0,p;
while(a > 9)
{
p = a % 10;
a = a / 10;
map[qmap ++] = p;
}
map[qmap] = a;
int mmm[10000];
memset(mmm,0,sizeof(mmm));
for(int q = 0;q <= qmap;q ++)
mmm[q] = map[q];
int shumu = 0;
for(int k = 0;k <= 9;k ++)
num[k] = k;
for(int l = 0;l <= qmap;l ++)
{
if(num[map[l]] != -1)
{
shumu ++;
num[map[l]] = -1;
}
}
if(shumu == 2)
{
for(int m = qmap;m >= 0;m --)
printf("%d",map[m]);
printf(" ");
return ;
}
while(1)
{
biji = 0;
for(int j = 0;j <= qmap;j ++)
{
map[j] = map[j] + mmm[j] + biji;
biji = map[j] / 10;
map[j] = map[j] % 10;
}
while(biji > 10)
{
qmap ++;
map[qmap] = biji % 10;
biji = biji / 10;
}
if(biji)
{
qmap ++;
map[qmap] = biji;
}
int shumu = 0;
for(int k = 0;k <= 9;k ++)
num[k] = k;
for(int l = 0;l <= qmap;l ++)
{
if(num[map[l]] != -1)
{
shumu ++;
num[map[l]] = -1;
}
}
if(shumu == 2)
{
for(int m = qmap;m >= 0;m --)
printf("%d",map[m]);
printf(" ");
return ;
}
}
}
int main()
{
//while(scanf("%d",&n),n != 0)
for(int n = 3160;n <= 3180;n ++)
{
printf("%d ",n);
flag = 0;
temp = 0;
memset(sum,0,sizeof(sum));
for(int i = 1;i <= 100000;i ++)
{
for(int j = 1;j <= 9;j ++)
{
temp = j;
sum[j] = sum[j] * 10 + j;
sum[j] = sum[j] % n;
if(sum[j] == 0 || sum[j] % n == 0)
{
flag = 1;
step = i;
break;
}
}
if(flag)
break;
}
if(flag)
{
for(int k = 1;k <= step;k ++)
printf("%d",temp);
printf(" ");
}
else
{
BFS(n);
}
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int n;
int flag,temp,sum[100],step;
int num[100];
void BFS(int a)
{
int biji;
int map[10000];
int qmap = 0,p;
while(a > 9)
{
p = a % 10;
a = a / 10;
map[qmap ++] = p;
}
map[qmap] = a;
int mmm[10000];
memset(mmm,0,sizeof(mmm));
for(int q = 0;q <= qmap;q ++)
mmm[q] = map[q];
int shumu = 0;
for(int k = 0;k <= 9;k ++)
num[k] = k;
for(int l = 0;l <= qmap;l ++)
{
if(num[map[l]] != -1)
{
shumu ++;
num[map[l]] = -1;
}
}
if(shumu == 2)
{
for(int m = qmap;m >= 0;m --)
printf("%d",map[m]);
printf(" ");
return ;
}
while(1)
{
biji = 0;
for(int j = 0;j <= qmap;j ++)
{
map[j] = map[j] + mmm[j] + biji;
biji = map[j] / 10;
map[j] = map[j] % 10;
}
while(biji > 10)
{
qmap ++;
map[qmap] = biji % 10;
biji = biji / 10;
}
if(biji)
{
qmap ++;
map[qmap] = biji;
}
int shumu = 0;
for(int k = 0;k <= 9;k ++)
num[k] = k;
for(int l = 0;l <= qmap;l ++)
{
if(num[map[l]] != -1)
{
shumu ++;
num[map[l]] = -1;
}
}
if(shumu == 2)
{
for(int m = qmap;m >= 0;m --)
printf("%d",map[m]);
printf(" ");
return ;
}
}
}
int main()
{
//while(scanf("%d",&n),n != 0)
for(int n = 3160;n <= 3180;n ++)
{
printf("%d ",n);
flag = 0;
temp = 0;
memset(sum,0,sizeof(sum));
for(int i = 1;i <= 100000;i ++)
{
for(int j = 1;j <= 9;j ++)
{
temp = j;
sum[j] = sum[j] * 10 + j;
sum[j] = sum[j] % n;
if(sum[j] == 0 || sum[j] % n == 0)
{
flag = 1;
step = i;
break;
}
}
if(flag)
break;
}
if(flag)
{
for(int k = 1;k <= step;k ++)
printf("%d",temp);
printf(" ");
}
else
{
BFS(n);
}
}
return 0;
}