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. 页面类跳转Demo

    package baidumapsdk.demo; import android.app.Activity; import android.content.BroadcastReceiver; imp ...

  2. [原创]抢先DriverStudio夺取机器控制权(上篇)

    原文链接:抢先DriverStudio夺取机器控制权 废话不谈,言归正传!大家都知道,装了DriverStudio软件(我使用的是v3.2版)的系统在启动时会显示其配置画面,(如图0所示) 图 0 这 ...

  3. MapReduce ---- TD-IDF

    1.TF-IDF TF-IDF(term frequency/inverse document frequency) 的概念被公认为信息检索中最重要的发明.描述单个term与特定document的相关 ...

  4. Cocos2d-x 学习之路------(CCCallfunc 系列)

    CCCallFunc,CCCallFuncN,CCCallFuncND,CCCallFuncO类都是调用函数来执行动作,他们的使用只是局限于他们调用的的函数的参数不同而不同 CCCallFunc的回调 ...

  5. Android NOTE

    一些小的点就记在这里吧…… MultiDex打包时zip错误 我遇到的是 Execution failed for task ':excelSior:packageAllDebugClassesFor ...

  6. Flex4开发笔记(与JAVA交互)

    (由于本人也是第一次接触flex开发,因此将开发过程中问题记录留档) 一.数据交换过程 借助BlazeDS可以实现flex与java之间的数据交互,大体流程如下: 1.导入blazeds的文件(配置w ...

  7. Oracle EBS-SQL (SYS-6):sys_在线用户职责查询.sql

    /*线用户查询-1*/ SELECT FSAV.USER_NAME,FU.DESCRIPTION,FSAV.RESPONSIBILITY_NAME,FSAV.USER_FORM_NAME,FSAV.L ...

  8. poj1552---枚举

    #include <stdio.h> #include <stdlib.h> int main() { ],th=,i,j; while(scanf("%d" ...

  9. ubuntu15.04下编译 libvirt

    很久没有编译 libvirt了. 工作需要,重新编译. [org_ref]: http://libvirt.org/compiling.html 很简单. 编译过程, 还是很多问题. 依赖包(给懒人参 ...

  10. Netfilter-packet-flow.svg

    调试网络的方法:(Debugging the kernel using Ftrace)  $ watch -n1 -d sudo cat /proc/net/snmp$ watch -n1 -d su ...