码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4294 Multiple

时间:2015-09-01 22:48:43      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

Multiple

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1071    Accepted Submission(s): 273


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 0xAhex < 11dec.
  You may assume that 1 <= N <= 104 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).
 

 

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
 
解题:据说:

最多用两个数就可以表示任何数的倍数。

证明 :对于一个数字a,可以构造出的数字有

a,aa,aaa,aaaa,aaaaa,……

每一个数对于n都有一个余数,余数最多有n个,根据鸽巢原理,前n+1个数中,必然有两个余数相等

那么二者之差,必定为n的倍数,形式为a……a0……0。

然后暴力搜索即可

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 20010;
 4 string ans,str;
 5 int n,k,m,num[2],pre[maxn],b[maxn];
 6 bool bfs() {
 7     queue<int>q;
 8     memset(b,-1,sizeof b);
 9     for(int i = 0; i < m; ++i) 
10         if(num[i]) {
11             q.push(num[i]);
12             b[num[i]] = num[i];
13         }
14     memset(pre,-1,sizeof pre);
15     while(!q.empty()) {
16         int u = q.front();
17         q.pop();
18         if(!u) return true;
19         for(int i = 0; i < m; ++i) {
20             int t = (u*k + num[i])%n;
21             if(b[t] == -1) {
22                 b[t] = num[i];
23                 pre[t] = u;
24                 q.push(t);
25             }
26             if(!t) return true;
27         }
28     }
29     return false;
30 }
31 void Path(int u) {
32     if(pre[u] != -1) Path(pre[u]);
33     str += (char)(b[u] + 0);
34 }
35 bool cmp(string &a,string &b) {
36     if(b.size() == 0) return true;
37     if(a.size() > b.size()) return false;
38     if(a.size() < b.size()) return true;
39     return a < b;
40 }
41 int main() {
42     while(~scanf("%d%d",&n,&k)) {
43         if(n < k) {
44             printf("%d\n",n);
45             continue;
46         }
47         bool  flag = false;
48         ans = "";
49         for(int i = m = 1; i < k; ++i) {
50             num[0] = i;
51             if(bfs()) {
52                 str = "";
53                 Path(0);
54                 flag = true;
55                 if(cmp(str,ans)) ans = str;
56             }
57         }
58         if(!flag) {
59             m = 2;
60             for(int i = 1; i < k; ++i) {
61                 num[1] = i;
62                 for(int j = 0; j < i; ++j) {
63                     num[0] = j;
64                     if(bfs()) {
65                         str = "";
66                         Path(0);
67                         if(cmp(str,ans)) ans = str;
68                     }
69                 }
70             }
71         }
72         cout<<ans<<endl;
73     }
74     return 0;
75 }
View Code

 

HDU 4294 Multiple

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4777247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!