现在的位置: 首页 > 算法 > 正文

hdu 1664 Different Digits, spoj 3929 , 同余,bfs

2018年12月18日 算法 ⁄ 共 2418字 ⁄ 字号 评论关闭

Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 820    Accepted Submission(s):
202


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

题意:

给一个n,让你找出含不同数字最少的n的倍数,如果含不同数字相同,则输出最小的。


同余,bfs


定理:
对于任意的整数n,必然存在一个由不多于两个的数来组成的一个倍数。
因为a,aa,aaa……取n+1个,则必有两个模n余数相同,相减即得n的倍数m。而m只由a、0组成。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <set>
#include <queue>
#include <string>
using namespace std;

const int maxn = 100000;
int N;
int a[5], cnt;
string ans;
int ansn;
struct node{
	short d;
	int pre;
	int re;
};
node q[maxn];
int head, tail;

string tmp;

inline int cmpstr(string a, string b){
	if( a.size()<b.size() || (a.size()==b.size()&&a<b) )
		return -1;
	return 1;
}
bool hash[70000];
bool bfs(){
	head = tail = 0;
	node nt, qt;
	memset(hash, 0, sizeof hash );
	for(int i=0; i<cnt; ++i){
		if(a[i]==0) continue;
		nt.d = a[i];
		nt.pre = -1;
		nt.re = a[i] % N;
		q[tail++] = nt;
		hash[nt.re] = 1;
	}
	while(head<tail){
		nt = q[head];
		for(int i=0; i<cnt; ++i){
			qt.d = a[i]; qt.pre = head; qt.re = (nt.re*10+a[i]) % N;
			if(!hash[qt.re]){
				hash[qt.re] = 1;
				q[tail++] = qt;
			}
			if(qt.re==0){
				tmp = "";
				for(int pos=tail-1; pos!=-1; pos = q[pos].pre){
                    tmp += q[pos].d + '0';
				}
				reverse(tmp.begin(), tmp.end());
				return 1;
			}
		}
		head++;
	}
	return 0;
}

void solve(int N){
	set<int> st;
	ans = "";
	for(int x=N, y; x>0; x/=10){
		y = x % 10;
		ans += y + '0';
		st.insert(y);
	}
	ansn = st.size();
	reverse(ans.begin(), ans.end());
	cnt = 1;
	for(a[0]=1; a[0]<10; ++a[0]){
        if(bfs())
        if(ansn>cnt || (ansn==cnt&&cmpstr(tmp,ans)<0) ){
            ans = tmp;
            ansn = cnt;
        }
	}
	if(ansn==cnt){
        printf("%s\n", ans.c_str());
        return ;
	}
	cnt = 2;
	for(a[0]=0; a[0]<10; ++a[0]){
		for(a[1]=a[0]+1; a[1]<10; ++a[1]){
		   if(bfs())
           if(ansn>cnt ||(ansn==cnt&&cmpstr(tmp,ans)<0)){
                ans = tmp;
                ansn = cnt;
           }
		}
	}
	printf("%s\n", ans.c_str());
}

template<class T>
inline bool scan_d(T &ret){
    char c; ret = 0;
    while((c=getchar())<'0'||c>'9');
    while(c>='0'&&c<='9') ret = ret*10 + (c-'0'),c=getchar();
    return 0;
}
int main(){
   // freopen("in.txt","r",stdin);
	while(true){
        scan_d(N);
		if(N==0) break;
		if(N<10){
			printf("%d\n", N);
			continue;
		}
		solve(N);
	}
	return 0;
}

抱歉!评论已关闭.