Yet Another Multiple Problem

Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 5496    Accepted Submission(s): 1257

Problem Description

There are tons of problems about integer multiples. Despite the fact that the topic is not original, the content is highly challenging. That’s why we call it “Yet Another Multiple Problem”.
In this problem, you’re asked to solve the following question: Given a positive integer n and m decimal digits, what is the minimal positive multiple of n whose decimal notation does not contain any of the given digits?
 

Input

There are several test cases.
For each test case, there are two lines. The first line contains two integers n and m (1 ≤ n ≤ 104). The second line contains m decimal digits separated by spaces.
Input is terminated by EOF.
 

Output

For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) while Y is the minimal multiple satisfying the above-mentioned conditions or “-1” (without quotation marks) in case there does not exist such a multiple.
 

Sample Input

2345 3
7 8 9
100 1
0

Sample Output

Case 1: 2345
Case 2: -1
 
题意:
求n的最小倍数x,不包含m个特定的数字。
思路:
按数字位进行搜索,状态数最多只有10000种。
 //2016.9.5
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define N 10005 using namespace std; bool vis[N], del[];//vis表示是否访问过,del表示不能出现的数字
int n, m, pre[N];
char text[N];//最后输出的数组 bool bfs()
{
queue<int> q;
q.push();
int cur;
while(!q.empty())
{
cur = q.front();
q.pop();
for(int i = ; i < ; i++)
{
if(del[i]==true||cur==&&i==)continue;//不符合要求
int mod = (cur*+i)%n;
if(vis[mod])continue;//剪枝
text[mod] = ''+i;
vis[mod] = true;
pre[mod] = cur;//记录上一个节点
q.push(mod);
if(mod == )return true;
}
}
return false;
} void print()//打印路径
{
string ans;
int pos = ;
while(pos!= || ans.empty())
{
ans += text[pos];
pos = pre[pos];
}
reverse(ans.begin(), ans.end());//翻转,输出
puts(ans.c_str());
} int main()
{
int kase = , x;
while(scanf("%d%d", &n, &m)!=EOF)
{
memset(vis, , sizeof(vis));
memset(del, , sizeof(del));
for(int i = ; i < m; i++)
{
scanf("%d", &x);
del[x] = true;
}
printf("Case %d: ", ++kase);
if(!bfs())printf("-1\n");
else print();
} return ;
}

HDU4474的更多相关文章

  1. hdu4474 Yet Another Multiple Problem

    Yet Another Multiple Problem Description There are tons of problems about integer multiples. Despite ...

随机推荐

  1. js timer

    <html><head lang="en"> <meta charset="UTF-8"> <title>< ...

  2. 【poj解题】1028

    stack的应用 #include<iostream> #include<stack> #include<stdio.h> #include<stdlib.h ...

  3. C++------------typedef 函数指针类型定义

    摘要bycrazyhacking:        typedef 是定义了一种"函数指针"类型,可以再声明很多变量.函数指针的定义是定义了一个变量. int max(int x,i ...

  4. Windows Azure Storage Client Library 2.0 入门

    入门连接如下: http://gauravmantri.com/2012/11/17/storage-client-library-2-0-migrating-table-storage-code/

  5. linux undelete

    http://www.tldp.org/HOWTO/archived/Ext2fs-Undeletion-Dir-Struct/index.html http://www.giis.co.in/deb ...

  6. HDU 5611 Baby Ming and phone number

    #include<cstdio> #include<cstring> #include<vector> #include<cmath> #include ...

  7. FastCgi与Cgi

      转载请注明出处:http://www.cnblogs.com/stonehat/p/6286966.html  他们之间的不同在于   FastCgi的进程是常驻的,监听一个socket, 无线循 ...

  8. iOS探究UITableView的内部代码,仿UITableView自定义

    大家都知道UITableView,最经典在于循环利用,这里我自己模仿UITableView循环利用,写了一套自己的TableView实现方案,希望大家看了我的文章,循环利用思想有显著提升. 研究UIT ...

  9. mysql 不同语法

    http://blog.csdn.net/kesaihao862/article/details/6718443 REPLACE INTO id_28_repayid(stub) VALUES(1); ...

  10. IIS Default Web Site : The service did not response to the start or control request in a timely fashion

    IIS Default Web Site无法启动,提示错误:The service did not response to the start or control request in a time ...