HDU 4294 Multiple

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

2012 ACM/ICPC Asia Regional Chengdu Online

解题:据说:

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

证明 :对于一个数字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 }

时间: 2024-10-22 03:23:15

HDU 4294 Multiple的相关文章

HDU 4294 Multiple(搜索+数学)

题意: 给定一个n,让求一个M,它是n个倍数并且在k进制之下 M的不同的数字最少. 思路: 这里用到一个结论就是任意两个数可以组成任何数的倍数.知道这个之后就可以用搜索来做了.还有一个问题就是最多找n+1个数,因为由鸽巢原理,这n+1个数当中模上n一定有一个一同的.所以他们一减就是答案.如果找到直接是它的倍数的话,就直接返回. 搜索时保存的是它的余数,如果余数为0 的时候直接返回.还有就是在搜索中并不是直接找余数相同的两个数.而是找余数为0的.当暴力不同元素个数为2的时候,这时候已经算是找余数刚

HDU 1019 Least Common Multiple (最小公倍数)

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30285    Accepted Submission(s): 11455 Problem Description The least common multiple (LCM) of a set of positive integers is

HDU 1019 Least Common Multiple 数学题解

求一组数据的最小公倍数. 先求公约数在求公倍数,利用公倍数,连续求所有数的公倍数就可以了. #include <stdio.h> int GCD(int a, int b) { return b? GCD(b, a%b) : a; } inline int LCM(int a, int b) { return a / GCD(a, b) * b; } int main() { int T, m, a, b; scanf("%d", &T); while (T--)

hdu 1019 Least Common Multiple

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 54584    Accepted Submission(s): 20824 Problem Description The least common multiple (LCM) of a set of positive integers is

ACM学习历程—HDU 3092 Least common multiple(数论 &amp;&amp; 动态规划 &amp;&amp; 大数)

hihoCoder挑战赛12 Description Partychen like to do mathematical problems. One day, when he was doing on a least common multiple(LCM) problem, he suddenly thought of a very interesting question: if given a number of S, and we divided S into some numbers

HDU 4474 Yet Another Multiple Problem

Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 4734    Accepted Submission(s): 1021 Problem Description There are tons of problems about integer multiples. Despit

杭电 HDU ACM 2028 Lowest Common Multiple Plus

Lowest Common Multiple Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 39183    Accepted Submission(s): 16144 Problem Description 求n个数的最小公倍数. Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数. Ou

【暴力+BFS】HDU 4474 Yet Another Multiple Problem

通道:http://acm.hdu.edu.cn/showproblem.php?pid=4474 题意:给出n和m个数位,求一个数X,这个数是n的最小倍数且他的每一位都不含有m个数位中的任意一个. 思路:反过来想,其实就是有非M的元素组成一个数,这个数是N的倍数.如果存在解,那么他的第一位便是非M组合中的任意一位,然后除N后,他的余数便是X-a*n,那么下一位除N的就是(X-a*n)*10+(枚举的下一个非M元素),要求最小这个数,那么BFS跑最短就可以了. 代码:https://github

HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】

Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3407    Accepted Submission(s): 825 Problem Description There are tons of problems about integer multiples. Despit