51nod 1109 01组成的N的倍数+
2016-12-02 18:33
323 查看
1109 01组成的N的倍数
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
收藏
关注
给定一个自然数N,找出一个M,使得M > 0且M是N的倍数,并且M的10进制表示只包含0或1。求最小的M。
例如:N = 4,M = 100。
Input
Output
Input示例
Output示例
首先 ,这个题大致就是一层一层的下去 具体就是 1为开头 下面一直接0 1 画成一个满二叉树的样子
然后满二叉是肯定超时的 然后就用到了同余定理剪枝- - 秒过
需要注意的一点就是用map 会超时 - - 大致就是关联容器比较慢吧
用字符数组记录也是可以的 而且用字符数组是比较快的
但是按照效率来说的话,还是手写的队列比较快,但是因为这个题的同余定理剪枝 ,所以空间有不确定性,大约的开下 ,反正比较看运气了
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int mod=200000;
int d[200000];
string a[200000];
bool dp[1000000];
int main()
{
int n;
while(cin>>n)
{
memset(dp,true,sizeof(dp));
int i=0,j=1;
d[0]=1;
a[0]+='1';
int x;
while(1)
{
x=d[i]*10%n;
if(dp[x])
{
dp[x]=false;
d[j]=x;
a[j]=a[i]+'0';
if(d[j]==0)
{
cout<<a[j]<<endl;break;
} j=(j+1)%mod;
}
x=(d[i]*10+1)%n;
if(dp[x])
{
dp[x]=false;
d[j]=x;
a[j]=a[i]+'1';
if(d[j]==0)
{
cout<<a[j]<<endl;break;
}
j=(j+1)%mod;
}
i=(i+1)%mod;
}
// i=0;while(i<100000) {a[i].clear();i++;}
// memset(d,0,sizeof(d));
}
}
基准时间限制: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
首先 ,这个题大致就是一层一层的下去 具体就是 1为开头 下面一直接0 1 画成一个满二叉树的样子
然后满二叉是肯定超时的 然后就用到了同余定理剪枝- - 秒过
#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include <queue> using namespace std; queue<int>d; queue<string> a; int dp[1000000];//余数记录 int main() { int n; while(cin>>n) { memset(dp,0,sizeof(dp)); int i=1,j=1; d.push(1); a.push("1"); int x; while(1) { x=d.front()*10%n; if(dp[x]==0) { dp[x]=1; d.push(x); a.push(a.front()+'0'); } if(d.back()==0) { cout<<a.back()<<endl; break; } x=(d.front()*10+1)%n; if(dp[x]==0) { dp[x]=1; d.push(x); a.push(a.front()+'1'); } if(d.back()==0) { cout<<a.back()<<endl; break; } a.pop(); d.pop(); } } }
需要注意的一点就是用map 会超时 - - 大致就是关联容器比较慢吧
用字符数组记录也是可以的 而且用字符数组是比较快的
#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include <queue> #include <map> using namespace std; queue<int>d; queue<string> a; char dp[1000000];//字符数组记录 int main() { int n; while(cin>>n) { memset(dp,'0',sizeof(dp)); int i=1,j=1; d.push(1); a.push("1"); int x; while(1) { x=d.front()*10%n; if(dp[x]=='0') { dp[x]='1'; d.push(x); a.push(a.front()+'0'); } if(d.back()==0) { cout<<a.back()<<endl; break; } x=(d.front()*10+1)%n; if(dp[x]=='0') { dp[x]='1'; d.push(x); a.push(a.front()+'1'); } if(d.back()==0) { cout<<a.back()<<endl; break; } a.pop(); d.pop(); } } }
但是按照效率来说的话,还是手写的队列比较快,但是因为这个题的同余定理剪枝 ,所以空间有不确定性,大约的开下 ,反正比较看运气了
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int mod=200000;
int d[200000];
string a[200000];
bool dp[1000000];
int main()
{
int n;
while(cin>>n)
{
memset(dp,true,sizeof(dp));
int i=0,j=1;
d[0]=1;
a[0]+='1';
int x;
while(1)
{
x=d[i]*10%n;
if(dp[x])
{
dp[x]=false;
d[j]=x;
a[j]=a[i]+'0';
if(d[j]==0)
{
cout<<a[j]<<endl;break;
} j=(j+1)%mod;
}
x=(d[i]*10+1)%n;
if(dp[x])
{
dp[x]=false;
d[j]=x;
a[j]=a[i]+'1';
if(d[j]==0)
{
cout<<a[j]<<endl;break;
}
j=(j+1)%mod;
}
i=(i+1)%mod;
}
// i=0;while(i<100000) {a[i].clear();i++;}
// memset(d,0,sizeof(d));
}
}
相关文章推荐
- 一些题目
- Java 定时器ScheduleExecutorService实战
- Spring单元测试加载properties文件
- MVVM前端框架个人见解
- linux查看端口及端口详解
- SDWebImage源码解读 之 NSData+ImageContentType
- javascript数字类型number类型方法
- 【Mail】搭建邮件服务器(LAMP+Postfix+Dovcot+PostfixAdmin+Roundcubemail)
- SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
- 读写分离二
- JEECG再创新举,开辟支付宝服务窗开发新时代,业内独家培训 《支付宝服务窗实战培训》
- ansible
- 51NOD1435 位数阶乘
- nginx模块开发入门
- 在LINUX注意1和l
- 如何实现访问apache时的目录浏览功能(apache file list view)
- Struts2文件上传
- javascript Math对象方法
- CentOS系统MySQL双机热备配置
- 从面试开始_C++常见面试题