Yet Another Multiple Problem

Time Limit : 40000/20000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other)
Total Submission(s) : 2   Accepted Submission(s) : 1
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 ≤ 10[sup]4[/sup]). 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,让找出不含接下来m个数字能组成的n的倍数的最小解;参考了大神的写法;
/***************/

按照数的位数BFS,从小向大枚举就可以保证构造出来的数是递增的,如果不加判断就直接搜索的话,复杂度非常高。因此需要剪枝。

优化方法:如果一个数%N==0,那么这个数就是N的倍数。在没有找到的前提下,如果A%N==B%N,而且A<B,那么其实我们就可以取A而不取B,因为如果在A末尾增加C可以使得AC%N==0,那么BC%N也等于0,易得:如果A和B追加数之后%N==0,那么最优条件下追加的数肯定相同。

因此我们只需要维护组合出来的数%N的值即可,如果在搜索的途中出现了相同的%N值,就可以直接忽略了,因为肯定没有前面的优秀。

/***************/

代码:
 #include<stdio.h>
#include<string>
#include<queue>
#include<string.h>
#include<algorithm>
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
const int MAXN=1e4+;
int vis[MAXN],del[MAXN],pre[MAXN];
char al[MAXN];
int n;
void print_ans(){
int r=;
string ans;
while(ans.empty()||r!=){
ans+=al[r];
r=pre[r];//由于anser的下属是0,刚开始的数字的上司又是0,所以最后找到0结束循环;
}
reverse(ans.begin(),ans.end());
puts(ans.c_str());
}
bool bfs(){
queue<int>dl;
dl.push();
while(!dl.empty()){
int f1,f2;
f1=dl.front();//**
dl.pop();
for(int i=;i<=;i++){
if(del[i]||i==&&f1==)continue;
f2=(f1*+i)%n;
if(vis[f2])continue;//应该放上面
pre[f2]=f1;//**
al[f2]=i+'';
if(f2==){
print_ans();
return true;
}
vis[f2]=;
dl.push(f2);
}
}
puts("-1");
return false;
}
int main(){
int m,flot=;
while(~scanf("%d%d",&n,&m)){
mem(vis);mem(del);mem(pre);mem(al);
while(m--){
int temp;
scanf("%d",&temp);
del[temp]=true;
}
printf("Case %d: ",++flot);
bfs();
}
return ;
}

第二种方法wa:

 #include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN=1e4+;
int num[];
struct Node{
int s[];
int len;
};
int M,C,N;
int vis[MAXN];
int mod(Node a){
int x=;
for(int i=;i<a.len;i++){
x=(x*C+a.s[i])%N;
}
return x;
}
void print_ans(Node a){
for(int i=;i<a.len;i++){
printf("%d",a.s[i]);
}
puts("");
}
void bfs(){
memset(vis,,sizeof(vis));
queue<Node>dl;
Node a;
a.len=;
int md;
for(int i=;i<M;i++){
a.s[]=num[i];
md=mod(a);
if(vis[md]||num[i]==)continue;
if(md==&&num[i]){
printf("%d\n",num[i]);
return;
}
vis[md]=;
dl.push(a);
}
while(!dl.empty()){
a=dl.front();
dl.pop();
for(int i=;i<M;i++){
if(a.len==&&a.s[]==)continue;
a.s[a.len]=num[i];
a.len++;
md=mod(a);
if(vis[md]){
a.len--;
continue;
}
if(md==){ print_ans(a);
return;
}
vis[md]=;
dl.push(a);
a.len--;
}
}
puts("-1");
}
int main(){
int del[],flot=;
while(~scanf("%d%d",&N,&M)){
memset(del,,sizeof(del));
for(int i=;i<M;i++){
int temp;
scanf("%d",&temp);
del[temp]=;
}
int i,j;
for(i=,j=;i<=;i++){
if(!del[i])num[j++]=i;
}
M=j;
printf("Case %d: ",++flot);
C=;
bfs();
}
return ;
}

Yet Another Multiple Problem(bfs好题)的更多相关文章

  1. POJ1426:Find The Multiple(算是bfs水题吧,投机取巧过的)

    http://poj.org/problem?id=1426 Description Given a positive integer n, write a program to find out a ...

  2. HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )

    没什么巧办法,直接搜就行. 用余数作为每个节点的哈希值. #include <cstdio> #include <cstring> #include <cstdlib&g ...

  3. HDU 4474 Yet Another Multiple Problem BFS

    题意:求m的倍数中不包含一些数码的最小倍数数码是多少.比如15 ,不包含0  1 3,答案是45. BFS过程:用b[]记录可用的数码.设一棵树,树根为-1.树根的孩子是所有可用的数码,孩子的孩子也是 ...

  4. 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 ...

  5. HDU-4471 Yet Another Multiple Problem (BFS+路径还原)

    Problem Description There are tons of problems about integer multiples. Despite the fact that the to ...

  6. hdu4474 Yet Another Multiple Problem

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

  7. 2012Chhengdu K - Yet Another Multiple Problem

    K - Yet Another Multiple Problem Time Limit:20000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  8. POJ 3984 - 迷宫问题 - [BFS水题]

    题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...

  9. fzuoj Problem 2182 水题

    http://acm.fzu.edu.cn/problem.php?pid=2182 Problem 2182 水题 Accept: 188    Submit: 277Time Limit: 100 ...

随机推荐

  1. 一些CSS命名规则

    一些CSS命名规则 头:header 内容:content/containe 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wrapper 左右中 ...

  2. C语言如何定义结构体

    原文地址 1. struct与typedef struct区别 struct是结构体的关键字,用来声明结构体变量如 struct  student {   char  num[10];      ch ...

  3. [原创]反汇编之一:和Taskmgr过不去篇(无厘头版)

    原文链接:和Taskmgr过不去篇(无厘头版) Hook入门级文章,主要想培养一下偶写文章的感觉,老鸟无视…我想看看技术文章能不能无厘头的写,如果效果不错的话,准备更上一层-----用我的原创漫画表达 ...

  4. 密码学——网间数据加密传输全流程(SSL加密原理)

    0.导言 昨天写了一篇关于<秘钥与公钥>的文章,写的比较简单好理解,有点儿像过家家,如果详细探究起来会有不少出入,今天就来详细的说明一下数据加密的原理和过程.这个原理就是大名鼎鼎SSL的加 ...

  5. android开源框架和开源项目(转)

    特效: http://www.androidviews.net/ http://www.theultimateandroidlibrary.com/ 常用效果: 1. https://github.c ...

  6. CSSOM View Module

    就在8月份,也就是上次gf大姨妈来的时候,W3C出炉了CSSOM视图模块(CSS Object Model View)草案.CSSOM视图模块(CSSOM View Module)定义了一些 API, ...

  7. Solrj日期范围查询

    在做依据日期来检索的时候普通的格式化会出错,试了好多种仅仅有一种可行 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH ...

  8. c++打印环境变量

    直接上代码:cpp版本 #include <stdio.h> #include <stdlib.h> #include <string.h> extern char ...

  9. WPF datagrid 初学

    <Window x:Class="WpfDemo.WinDataGrid" xmlns="http://schemas.microsoft.com/winfx/20 ...

  10. html基础标签-2-textarea文本域

    textarea文本域 <!doctype html> <html lang='zh-cn'> <head> <meta charset='utf-8'> ...