Multiple
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 954 Accepted Submission(s): 230
Problem Description
Given a positive integer N, you’re to solve the following problem:
Find a positive multiple of N, says M, that contains minimal number of different digits in base-K notation. If there’re several solutions, you should output the numerical smallest one. By saying numerical smallest one, we compar their numerical value, so 0xA hex < 11 dec.
You may assume that 1 <= N <= 10 4 and 2 <= K <= 10.
Find a positive multiple of N, says M, that contains minimal number of different digits in base-K notation. If there’re several solutions, you should output the numerical smallest one. By saying numerical smallest one, we compar their numerical value, so 0xA hex < 11 dec.
You may assume that 1 <= N <= 10 4 and 2 <= K <= 10.
Input
There’re several (less than 50) test cases, one case per line.
For each test case, there is a line with two integers separated by a single space, N and K.
Please process until EOF (End Of File).
For each test case, there is a line with two integers separated by a single space, N and K.
Please process until EOF (End Of File).
Output
For each test case, you should print a single integer one line, representing M in base-K notation,the answer.
Sample Input
10 8 2 3 7 5
Sample Output
2222 2 111111
Source
题目大意:求N的最小整数倍在k进制下不同的数字的个数是最小的那个倍数
解题思路:对于a,aa,aaa,....,aaa..aa这n个数字的情况,如果有一个mod n为0,那么最小的数字个数为1,否则,在这n个数字中,必然存在2个数mod n是同余的,所以必然存在2个的差值aaaa000这种形式的数mod n为0,所以答案最多只会有两个数字。
对于一个数字的情况,直接暴力即可,对于两个的情况,枚举2个数字,用bfs模拟2个数字的排列,注意要以和mod n的值作为状态,这同一个mod n的值对应一个值最小的答案。
<span style="font-size:18px;">#include <iostream>
#include <cstdio>
#include <queue>
#include <string>
#include <algorithm>
using namespace std;
int n,k;
struct node{
int mod,w,x;
node(){}
node(int mod,int w,int x):mod(mod),w(w),x(x){}
}now;
string ret;
int pre[10010];
int s[10010];
bool vis[10010];
void bfs(){
for(int i=1;i<k;i++){
for(int j=0;j<i;j++){
int ans=-1;
queue<node> q;
int z=0;
memset(vis,0,sizeof vis);
q.push(node(i,1,z));
pre[0]=-1,s[0]=i,z++;
vis[i]=1;
if(j) q.push(node(j,1,z));
pre[1]=-1,s[1]=j,z++;
while(!q.empty()){
now=q.front();
q.pop();
if(now.mod==0){
ans=now.x;
break;
}
if(!vis[(now.mod*k+j)%n]){
vis[(now.mod*k+j)%n]=1;
pre[z]=now.x;
s[z]=j;
q.push(node((now.mod*k+j)%n,now.w+1,z));
z++;
}
if(!vis[(now.mod*k+i)%n]){
vis[(now.mod*k+i)%n]=1;
pre[z]=now.x;
s[z]=i;
q.push(node((now.mod*k+i)%n,now.w+1,z));
z++;
}
}
if(ans==-1) continue;
string ss="";
for(int i=ans;i!=-1;i=pre[i]){
ss.push_back(s[i]+'0');
}
int l=ss.size();
for(int i=0;i<l/2;i++) swap(ss[i],ss[l-1-i]);
if(ss.size()<ret.size()){
ret=ss;
}else if(ss.size()==ret.size()){
if(ss<ret){
ret=ss;
}
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&k))
{
ret="";
for(int i=0;i<=n;i++) ret.push_back('9');
string tmp;
int mod;
bool flag=0;
for(int i=1;i<k;i++){
tmp="";
mod=0;
for(int j=0;j<n;j++){
tmp.push_back('0'+i);
mod=(mod*k+i)%n;
if(mod==0){
if(tmp.size()<ret.size()){
ret=tmp;
flag=1;
}else if(tmp.size()==ret.size()){
if(tmp<ret) ret=tmp,flag=1;
}
break;
}
}
}
if(!flag) bfs();
printf("%s\n",ret.c_str());
}
return 0;
}
</span>