HDU 4474
来源:互联网 发布:照片白边框软件 编辑:程序博客网 时间:2024/06/04 18:55
题目描述
给定一个数n,还有若干个非法的数字,要求用合法的数字构造一个数是n的最小倍数。
方法
枚举n的倍数来判断会超时,那个合法的数可能会超过long long,具体做法是用合法的数字从小到大构造数,第一个是n的倍数的就是合法的。
对于枚举到1e5的倍数的为什么不行的问题
这里采用反证法。因为这题最大的数去到1e4,那么看上去我枚举1e5次倍数之后就会重复的样子。
假设最大的数不超过10,那么相应地枚举到10倍
样例
5 5
5 1 2 3 4
5->10->15->20->25->30->35->40->45->50//这里是枚举了10倍
55->60//但是往后枚举会出现一个6
事实上还是往后看的
import java.util.LinkedList;import java.util.Queue;import java.util.Scanner;public class Main { /** * @param 方法是用那些合法的数字来从小到大构造数,第一个是n的模数的就是答案 */ static final int MAXN = (int) (1e4 + 10); static Boolean[] invaild; static Boolean[] vis; static class Status { int u; String str;//记录答案 public Status(int u, String str) { this.u = u; this.str = str; } } static String bfs(int n) { Queue<Status> que = new LinkedList<>(); vis = new Boolean[MAXN];//因为要找倍数,那么等效于让模数等于0的状态,可以用数组来判重 que.add(new Status(0, "")); while (que.isEmpty() == false) { Status now = que.poll(); for (int i = 0; i < 10; i++) {//这样每次保证能先构造出最小的数 if (invaild[i] != null) continue; if (now.u == 0 && i == 0)//0是非法的 continue; int v = now.u * 10 + i; v %= n; String str = now.str + i; if (v == 0) {//如果模数等于0了相当于找到了 return str; } if (vis[v] != null)//java的机制,new的时候并没有给数组元素赋值,还是空的,所以不能判断false continue; vis[v] = true; que.add(new Status(v, str)); } } return "-1"; } public static void main(String[] args) { Scanner cin = new Scanner(System.in); int m, n; int icase = 0; while (cin.hasNext()) { System.out.print("Case " + ++icase + ": "); n = cin.nextInt(); m = cin.nextInt(); invaild = new Boolean[10]; for (int i = 0; i < m; i++) { int d = cin.nextInt(); invaild[d] = true; } System.out.println(bfs(n)); } cin.close(); return; }}
#include<bits/stdc++.h>using namespace std;const int MAXN = 1e4+10;bool invalid[MAXN];struct Status{ int u; string str;};string bfs(int n){ bool vis[MAXN]={0}; queue<Status> que; que.push({0,""}); while(que.size()) { Status now=que.front();que.pop(); for(int i=0;i<10;i++) { if(invalid[i]) continue; if(i==0&&now.u==0) continue; int v=now.u*10+i; string str=now.str+char('0'+i); if(v%n==0) { return str; } v%=n; if(vis[v]) continue; vis[v]=true; que.push({v,str}); } } return "-1";}int main(){ if (fopen("in.txt", "r") != NULL) { freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); } ios::sync_with_stdio(false); int n,m; int icase=0; while(cin>>n>>m) { cout<<"Case "<<++icase<<": "; memset(invalid,false,sizeof invalid); for(int i=0;i<m;i++) { int d; cin>>d; invalid[d]=true; } cout<<bfs(n)<<'\n'; } return 0;}
阅读全文
0 0
- hdu 4474
- hdu 4474
- HDU-4474
- HDU 4474
- HDU 4474
- HDU 4474 BFS
- hdu 4474 数位搜索
- hdu 4474 搜索bfs
- hdu 4474 bfs
- hdu 4474 数位BFS
- hdu 4474 dfs+减枝
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- 例题8-7 唯一的雪花(Unique snowflakes, UVa 11572)
- Code Review 代码审查 不完全整理
- hibernate的一级缓存(六)
- golang基础-kafka、zookeeper搭建、go终端发送数据给kafka
- U盘插上,系统有反应,但是却不识别,电脑能识别其他的U盘,U盘在其他电脑也是好用的经验分享(驱动问题)
- HDU 4474
- 十:.pyc是个什么?
- 新秀杯热身赛-wq的矩阵
- 十一:数据类型初识
- HDU-6008-Worried School
- BootStrap模态框闪退
- github入门
- Eventbus 1.0-3.0
- 【面试】--big Data