TOJ3031: Multiple bfs
3031: Multiple
Total Submit:
60
Accepted:10
Description
a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (at least one), finds the smallest strictly positive multiple of N that has no other digits besides X1,X2..XM (if such a multiple exists).
Input
The input has several data sets separated by an empty line, each data set having the following format:
On the first line - the number N
On the second line - the number M
On the following M lines - the digits X1,X2..XM.
Output
For each data set, the program should write to standard output on a single line the multiple, if such a multiple exists, and 0 otherwise.
An example of input and output:
Sample Input
22
3
7
0
1
2
1
1
Sample Output
110
0
Source
这个题目可能没那么爽啊,就是给你m个数字,然后用这些数字就构造n的倍数
首先肯定贪心取啊,然后就是大数取余,所以这个时候同余的,然后bfs就完了
#include<stdio.h>
#include<string.h>
#include<algorithm>
const int N=;
struct T
{
int res,pre,digit;
} q[N],t;
int n,m,a[];
bool vis[N];
void print (int x)
{
if(q[x].pre==-)return;
print(q[x].pre),printf("%d",q[x].digit);
}
int bfs()
{
q[]= {,-,},memset(vis,,sizeof vis);
int tot=,lst=;
while(tot<lst)
{
t=q[tot];
for(int i=,t1; i<m; i++)
{
t1=(t.res*+a[i])%n;
if(!a[i]&&t.pre==-)continue;
if(!vis[t1])vis[t1]=,q[lst++]= {t1,tot,a[i]};
if(!t1)
{
print(lst-),printf("\n");
return ;
}
}
tot++;
}
return ;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=; i<m; i++)scanf("%d",&a[i]);
if(!n)printf("0\n");
else
{
std::sort(a,a+m);
if(!bfs())printf("0\n");
}
}
}
TOJ3031: Multiple bfs的更多相关文章
- POJ 1426 Find The Multiple --- BFS || DFS
POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...
- POJ1426Find The Multiple[BFS]
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27433 Accepted: 114 ...
- ZOJ 1136 Multiple (BFS)
Multiple Time Limit: 10 Seconds Memory Limit: 32768 KB a program that, given a natural number N ...
- POJ 1465 Multiple (BFS,同余定理)
id=1465">http://poj.org/problem?id=1465 Multiple Time Limit: 1000MS Memory Limit: 32768K T ...
- poj 1426 Find The Multiple (bfs 搜索)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18012 Accepted: 729 ...
- POJ1426 Find The Multiple —— BFS
题目链接:http://poj.org/problem?id=1426 Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Tota ...
- POJ 1426 Find The Multiple BFS
没什么好说的 从1开始进行广搜,因为只能包涵0和1,所以下一次需要搜索的值为next=now*10 和 next=now*10+1,每次判断一下就可以了,但是我一直不太明白我的代码为什么C++提交会错 ...
- poj1426 - Find The Multiple [bfs 记录路径]
传送门 转:http://blog.csdn.net/wangjian8006/article/details/7460523 (比较好的记录路径方案) #include<iostream> ...
- poj 1426 Find The Multiple ( BFS+同余模定理)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18390 Accepted: 744 ...
随机推荐
- IT届常用单词读法纠正
Bootstrap ['bʊt'stræp] Java ['dʒɑːvə] Node [nod] @ [æt; ət] Common ['kɑmən] Java ...
- 测试的发现遗漏BUG的做法
首先要确认BUG的影响范围: 后续做法如下: 1.从测试角度来说,外部缺陷等同与系统崩溃,测试是必须提的2.可以询问主管或负责人是否在后一个版本中修改3.评估缺陷对于用户使用存在多大的不便4.判定缺陷 ...
- Jquery 事件 DOM操作
常规事件: 把JS的事件 on去掉即可 例如:js document.getElementById("id").onclinck=function(){} Jquery ...
- github上fork原项目,如何将本地仓库代码更新到最新版本?
场景: 在github上fork原项目,项目组成员发起pull request提交了代码,这时自己在本地仓库该如何更新到最新代码? 操作方法如下: 方法一.从github上进行操作然后更新 登录自己的 ...
- jsp另外五大内置对象之-exception
//有异常的页面 <%@ page language="java" contentType="text/html; charset=utf-8" page ...
- 洛谷 P2014 选课
题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有个学分,每门课有一 ...
- HDU 5091 Beam Cannon (扫描线思想)
题意:移动一个矩形,使矩形内包含的点尽量多. 思路:把一个点拆成两个事件,一个进(权值为1)一个出(权值为-1),将所有点按照x排序,然后扫描,对于每个x,用一个滑窗计算一下最大值,再移动扫描线.树状 ...
- Ubuntu18.04如何从英文界面更改为中文界面
本文介绍如何将Ubuntu18.04安装后的英文界面,更改为中文界面,即系统语言由英文改为简体中文.注意,与安装中文输入法不同,两者也没有冲突. 首先进入设置(Setting),选择区域和语言(Reg ...
- 安装mysql时出现问题的解决办法
问题一:在安装.重装时出现could not start the service mysql error:0 原因: 卸载mysql时并没有完全删除相关文件和服务,需要手动清除. 安装到最后一步exe ...
- 作业题:闰年 if((year%4==0&&year%100!=0)||year&400==0)
作业题:闰年 if((year%4==0&&year%100!=0)||year&400==0)