1109 01组成的N的倍数
输入1个数N。(1 <= N <= 10^6)
输出符合条件的最小的M。
4
100
和http://www.cnblogs.com/zzuli2sjy/p/5731745.html一样;
1 #include <cstdio>
2 #include <cstdlib>
3 #include <cstring>
4 #include <cmath>
5 #include <iostream>
6 #include <algorithm>
7 #include <map>
8 #include <queue>
9 #include <vector>
10 #include<set>
11 using namespace std;
12 typedef long long LL;
13 bool flag[6000000];
14 int ans[20];
15 set<int>que;
16 set<int>::iterator it;
17 typedef struct pp
18 {
19 int mod;
20 int id;
21 int pre;
22 int digit;
23 pp()
24 {
25 pre=-1;
26 }
27 } ss;
28 ss bns[1000000];
29 int ask[1000000];
30 int cp[2000000];
31 int bfs(int n,int m);
32 int main(void)
33 {
34 int i,j,k;
35 while(scanf("%d",&k)!=EOF)
36 {
37 int n;
38 int m;
39 que.clear();
40 cp[0] = 0;cp [1] = 1;
41 n = 2;
42 ans[0]=cp[0];
43 int uu=cp[0];
44 int t=1;
45 for(i=1;i<n;i++)
46 {
47 if(cp[i]!=uu)
48 {
49 ans[t++]=cp[i];
50 uu=cp[i];
51 }
52 }
53
54 if(k==0)printf("0\n");
55 else
56 {
57 int sum=0;
58 int id=bfs(t,k);
59 if(id==-1)
60 {
61 printf("0\n");
62 }
63 else
64 {
65 while(id!=-1)
66 {
67 ask[sum++]=bns[id].digit;
68 id=bns[id].pre;
69 }
70 for(i=sum-1; i>=0; i--)
71 {
72 printf("%d",ask[i]);
73 }
74 printf("\n");
75 }
76 }
77 }
78 return 0;
79 }
80 int bfs(int n,int m)
81 {
82 int i,j,k;
83 int kk=0;
84 memset(flag,0,sizeof(flag));
85 queue<ss>stc;
86 for(i=0; i<n; i++)
87 {
88 int mod=ans[i]%m;
89 if(!flag[mod]&&ans[i]!=0)
90 {
91 flag[mod]=true;
92 bns[kk].id=kk;
93 bns[kk].mod=mod;
94 bns[kk].pre=-1;
95 bns[kk].digit=ans[i];
96 stc.push(bns[kk]);
97 kk++;
98 }
99 }
100 while(!stc.empty())
101 {
102 ss tt=stc.front();
103 stc.pop();
104 for(i=0; i<n; i++)
105 {
106 int mod=(tt.mod*10+ans[i])%m;
107 if(!flag[mod])
108 {
109 bns[kk].id=kk;
110 bns[kk].pre=tt.id;
111 bns[kk].mod=mod;
112 bns[kk].digit=ans[i];
113 if(mod==0)
114 {
115 return kk;
116 }
117 stc.push(bns[kk]);
118 kk++;
119 flag[mod]=true;
120 }
121 }
122 }
123 return -1;
124 }
1109 01组成的N的倍数的更多相关文章
- 51 nod 1109 01组成的N的倍数
1109 01组成的N的倍数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 给定一个自然数N,找出一个M,使得M > 0且M是N的倍数,并且 ...
- 51nod 1109 01组成的N的倍数
用01 组成 N的最小倍数 这个BFS搜索就好. 类似这道: ZOJ Problem Set - 1530 每次 要么是0 要么是1, 记入余数,和前驱. #include<bits/stdc ...
- 51Nod——T 1109 01组成的N的倍数
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1109 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 ...
- POJ 1426 Find The Multiple && 51nod 1109 01组成的N的倍数 (BFS + 同余模定理)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21436 Accepted: 877 ...
- 胡小兔的OI日志3 完结版
胡小兔的 OI 日志 3 (2017.9.1 ~ 2017.10.11) 标签: 日记 查看最新 2017-09-02 51nod 1378 夹克老爷的愤怒 | 树形DP 夹克老爷逢三抽一之后,由于采 ...
- Problem H. The Fence 通过取余判重,求得某个区间的某些个数为某个数的倍数。
/** 题目:Problem H. The Fence 链接:https://vjudge.net/problem/Gym-101090H 题意:给定一个字符串,只有0或者1: 问:假如两个不同的1之 ...
- MPlayer
名称 mplayer − 电影播放器 mencoder − 电影编解码器 概要 mplayer [选项] [文件|URL|播放列表|−] mplayer [选项] 文件1 [指定选项] [文件 ...
- IT第四天 - 运算符、随机数、Math类
IT第四天 上午 运算符 1.%运算符的应用 2.运算符优先级:小括号 ! 算数运算符 关系运算符 && || 赋值运算符 3.三元运算符:?表示条件为true的结果,:表示条件为 ...
- 2017"百度之星"程序设计大赛 - 初赛(A) [ hdu 6108 小C的倍数问题 ] [ hdu 6109 数据分割 ] [ hdu 6110 路径交 ] [ hdu 6112 今夕何夕 ] [ hdu 6113 度度熊的01世界 ]
这套题体验极差. PROBLEM 1001 - 小C的倍数问题 题 OvO http://acm.hdu.edu.cn/showproblem.php?pid=6108 (2017"百度之星 ...
随机推荐
- mysql 不等于 符号写法
今天在写sql语句的时候,想确认下mysql的不等于运算符是用什么符号表示的 经过测试发现mysql中用<>与!=都是可以的,但sqlserver中不识别!=,所以建议用<> ...
- mongodb-to-mongodb
python3用于mongodb数据库之间倒数据,特别是分片和非分片之间. 本项目是一个集合一个集合的倒. 参考了logstash,对于只增不减而且不修改的数据的可以一直同步,阻塞同步,断点同步.改进 ...
- C#集合Dictionary中按值的排序
C#集合Dictionary中按值的降序排列 static void Main(string[] args) { Dictionary<string, int> d ...
- C#生成编号
//自动生成账单编号 public string GetNewPoID(string Prefix) { string NewPoID = Prefix + DateTime.Now.Year.ToS ...
- 生产调优4 HDFS-集群扩容及缩容(含服务器间数据均衡)
目录 HDFS-集群扩容及缩容 添加白名单 配置白名单的步骤 二次配置白名单 增加新服务器 需求 环境准备 服役新节点具体步骤 问题1 服务器间数据均衡 问题2 105是怎么关联到集群的 服务器间数据 ...
- A Child's History of England.9
But, first, as it was important to know how numerous those pestilent Danes were, and how they were f ...
- A Child's History of England.33
To strengthen his power, the King with great ceremony betrothed his eldest daughter Matilda, then a ...
- 27.0 linux VM虚拟机IP问题
我的虚拟机是每次换一个不同的网络,b不同的ip,使用桥接模式就无法连接,就需要重新还原默认设置才行: 第一步:点击虚拟机中的编辑-->虚拟网络编辑器 第二步:点击更改设置以管理员权限进入 第三步 ...
- flink01--------1.flink简介 2.flink安装 3. flink提交任务的2种方式 4. 4flink的快速入门 5.source 6 常用算子(keyBy,max/min,maxBy/minBy,connect,union,split+select)
1. flink简介 1.1 什么是flink Apache Flink是一个分布式大数据处理引擎,可以对有限数据流(如离线数据)和无限流数据及逆行有状态计算(不太懂).可以部署在各种集群环境,对各种 ...
- 通过spring-data-redis操作Redis
一.操作String类型数据 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring/ ...