码迷,mamicode.com
首页 > 其他好文 > 详细

51Nod 1109 01组成的N的倍数

时间:2017-09-28 20:00:35      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:http   push   family   lis   pop   namespace   clu   one   span   

 

准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
技术分享 收藏
技术分享 关注
给定一个自然数N,找出一个M,使得M > 0且M是N的倍数,并且M的10进制表示只包含0或1。求最小的M。
 
例如:N = 4,M = 100。
Input
输入1个数N。(1 <= N <= 10^6)
Output
输出符合条件的最小的M。
Input示例
4
Output示例
100
 
 
找一个数,应该就是搜索 
一开始 我写了priority_queue 但是他不断地乘 会爆long long (我并没有取模)
 
一个数能整除n 等价于这个数%n==0 我们可以记录一下余数 若余数相同我们可以只入队一次
记录m的话 只能有字符串了。。
 
技术分享
 1 #include <queue>
 2 #include <cctype> 
 3 #include <cstdio>
 4 #include <iostream>
 5 
 6 using namespace std;
 7 
 8 typedef long long LL;
 9 const int MAXN=1000010;
10 
11 int n,t;
12 
13 struct node {
14     string s;
15     int x;
16 };
17 node a;
18 
19 std::queue<node> q;
20 
21 bool vis[MAXN];
22 
23 void BFS() {
24     q.push(a);
25     while(!q.empty()) {
26         a=q.front();
27         q.pop();
28         if(a.x==0) {
29             cout<<a.s<<endl;
30             return;
31         }
32         node L=a;
33         L.s+="0";
34         L.x=L.x*10%n;
35         if(!vis[L.x]) q.push(L),vis[L.x]=true;
36         L=a;
37         L.s+="1";
38         L.x=(L.x*10+1)%n;
39         if(!vis[L.x]) q.push(L),vis[L.x]=true;
40     }
41 }
42 
43 int hh() {
44     scanf("%d",&n);
45     a.s="1";a.x=1;
46     vis[1]=true;
47     BFS();
48     return 0;
49 }
50 
51 int sb=hh();
52 int main(int argc,char**argv) {;}
代码

 

 

51Nod 1109 01组成的N的倍数

标签:http   push   family   lis   pop   namespace   clu   one   span   

原文地址:http://www.cnblogs.com/whistle13326/p/7608122.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!