kb-01-e<取余操作,宽搜,巧妙>;
题目描述:
n属于1到200,找到对应的一个数只含有0和1,并且是n的倍数;
分析:
本题有几个数会是大数;所以要考虑大数;
用到余数的性质;例如n=6,1%6=1;
1*10%6=4; (1*10+1)%6=5;
4*10%6=4; (4*10+1)%6=5;
5*10%6=2; (5*10+1)%6=3;
(重复4,5)
2*10%6=2; 。。。。=3;
3*10%6=0;
这时候发现余数为0,说明这个数可以是6的倍数;倒退回去,数分别是1,10,11,100,101,110,111,。。。。1110;
可以发现余数是一样的,同余定理;
(a*b)%n = (a%n *b%n)%n
(a+b)%n = (a%n +b%n)%n
由同余模定理 (110*10+1)%6 = ((110*10)%6+1%6 )%6 = ((110%6 * 10%6)%6 +1 )%6;
用这个同余定理就可以解决大数问题了;然后就是记录路径,这里就是巧妙的地方;我还不太清除是怎么搞的,总之就是一共进行了k次操作,就相当于01全排列,首项是1,然后排到第一个符合的数的时候,这个数是第几个,它对应的二进制就是相应的串;这一题用bfs居然超时了;所以我打了个表,有一个不打表的做法;
代码如下:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std; queue<int> q;
int n;
char a[][]={"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};
/*
int cou=0;
void bfs()
{
while(!q.empty())
{
int t=q.front();
q.pop();
cou++;
if(t%n==0)
return;
q.push(t*10%n);
q.push((t*10+1)%n);
}
}
int main()
{
freopen("out","w",stdout);
for(int z=1;z<=200;z++)
{
n=z;
while(!q.empty())
q.pop();
memset(a,0,sizeof(a));
cou=0;
a[0]=1;
q.push(1);
bfs();
int i=0;
while(cou)
{
a[i++]=cou%2;
cou=cou/2;
}
printf("\"");
for(int j=i-1;j>=0;j--)
{
printf("%d",a[j]);
}
printf("\",");
}
return 0;
}*/ int main()
{
while(cin>>n&&n)
{
printf("%s\n",a[n-]);
}
return ;
}
//Memory Time
//2236K 32MS #include<iostream>
using namespace std; int mod[]; //保存每次mod n的余数
//由于198的余数序列是最长的
//经过反复二分验证,436905是能存储198余数序列的最少空间
//但POJ肯定又越界测试了...524286是AC的最低下限,不然铁定RE int main(int i)
{
int n;
while(cin>>n)
{
if(!n)
break; mod[]=%n; //初始化,n倍数的最高位必是1 for(i=;mod[i-]!=;i++) //利用同余模定理,从前一步的余数mod[i/2]得到下一步的余数mod[i]
mod[i]=(mod[i/]*+i%)%n;
//mod[i/2]*10+i%2模拟了BFS的双入口搜索
//当i为偶数时,+0,即取当前位数字为0 。为奇数时,则+1,即取当前位数字为1 i--;
int pm=;
while(i)
{
mod[pm++]=i%; //把*10操作转化为%2操作,逆向求倍数的每一位数字
i/=;
}
while(pm)
cout<<mod[--pm]; //倒序输出
cout<<endl;
}
return ;
}
kb-01-e<取余操作,宽搜,巧妙>;的更多相关文章
- Sword 位运算取余操作
/* 位运算取余操作 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include ...
- 计算机二级-C语言-程序修改题-190114记录-对整型变量进行取余操作可以取得各个位上的值。
//给定程序中fun函数的功能是:从低位开始取出长整形变量s中奇数位上的数,依次构成一个新的数放在t中.高位仍在高位,低位仍在低位.例如:当s中的数为7654321时,t中的数为7531. //重难点 ...
- Java之取余操作 "%"
取模运算与取余运算两个概念有重叠的部分但又不完全一致.主要的区别在于对负整数进行除法运算时操作不同. 对于整形数a,b来说,取模运算或者求余运算的方法都是: 1.求 整数商 c = a / b: 2. ...
- Python3的取余操作
https://blog.csdn.net/u014647208/article/details/53368244 取余代码: 输入以下代码: >>>10%2 >>> ...
- 计算机二级-C语言-程序修改题-190123记录-对整数进行取余和除以操作。
//函数fun功能:将长整型数中每一位上为偶数的数依次取出,构成一个新数放在t中.高位仍在高位,低位仍在低位. //重难点:思路:因为不是字符串,所以可以把问题变成整数的操作,采用取余和除的操作.对整 ...
- POJ1426 Find The Multiple (宽搜思想)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24768 Accepted: 102 ...
- python的加、减、乘、除、取整、取余计算
加法: 输入以下代码: >>>1+1 >>>1.0+1 减法: 输入以下代码: >>>1-2 >>>1.0-2 乘法: 输入以下 ...
- 高效取余运算(n-1)&hash原理探讨
Java的HashMap源码中用到的(n-1)&hash这样的运算,查找发现这是一种高效的求余数的办法,但其中的原理是什么呢为什么可以这么做呢? 先上结论:假设被除数是x,对于除数是2n的取余 ...
- 【错误记录】Python 负数取余问题
print(-123%10) # 输出 7 print(-123%-10) # 输出 -3 这里面第二条是我们一般意义上的取余操作.这里也特别标注一下,如果涉及到负数取余要用上述解决办法. ...
随机推荐
- Android系统Binder机制学习总结
一.Binder机制概述 在Android开发中,很多时候我们需要用到进程间通信,所谓进程间通信,实现进程间通信的机制有很多种,比如说socket.pipe等,Android中进程间通信的方式主要有三 ...
- MyLinkedList
/** * 节点类 * @author JP * */ class Node { Object value;//节点元素值 Node pre;//上一个节点 Node next;//下一个节点 pub ...
- docker单主机网络
当你安装Docker时,它会自动创建三个网络.你可以使用以下docker network ls命令列出这些网络: [root@localhost ~]# docker network ls NETWO ...
- oracle中print_table存储过程实例介绍
oracle中pro_print_table存储过程实例介绍 存储过程(Stored Procedure),就是一组用于完成特定数据库功能的SQL语句集,该SQL语句集经过编译后存储在数据库系统中.这 ...
- Visual Studio 2017 UTF-8 无 BOM 一站式解决办法
问题背景:最近捡起C++,使用VS 2017平台.因为以前的编程习惯,喜欢使用UTF-8 无 BOM 的编码格式,好让自己的代码全球通用.但是VS 2017 对这个问题不是很友善.但最终找到了解决办法 ...
- 有重复元素的排列(DFS)
题目描述: 设R={ r1, r2 , …, rn}是要进行排列的n个元素.其中元素r1, r2 , …, rn均为小写字母并且可能相同.试设计一个算法,列出R的所有不同排列. 给定n 以及待排列的n ...
- Bootstrap 模态框 select2搜索框无法输入
去掉模态框的div中的 tabindex="-1" 这个属性 <div class="modal fade" role="dialog" ...
- springboot下https证书配置
没有证书的小伙伴首先申请一个阿里云免费证书,按照我的步骤来操作 1.购买页面是这样的 按照顺序选择 神奇的一幕出现了 然后就去购买成功,我们会看到证书没有签发,我们需要去申请 填写需要绑定的域名 一般 ...
- 多本Python极速入门最佳书籍,不可错过的Python学习资料!
Python作为现在很热门的一门编程语言,介于Python的友好,许多的初学者都将其作为首选,为了帮助大家更好的学习Python,我筛选了2年内优秀的python书籍,个别经典的书籍扩展到5年内. ...
- sqlmanagementstudio2008下载地址
http://big.wy119.com/sqlmanagementstudio2008_x86_chs.zip