题目描述:

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<取余操作,宽搜,巧妙>;的更多相关文章

  1. Sword 位运算取余操作

    /* 位运算取余操作 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include ...

  2. 计算机二级-C语言-程序修改题-190114记录-对整型变量进行取余操作可以取得各个位上的值。

    //给定程序中fun函数的功能是:从低位开始取出长整形变量s中奇数位上的数,依次构成一个新的数放在t中.高位仍在高位,低位仍在低位.例如:当s中的数为7654321时,t中的数为7531. //重难点 ...

  3. Java之取余操作 "%"

    取模运算与取余运算两个概念有重叠的部分但又不完全一致.主要的区别在于对负整数进行除法运算时操作不同. 对于整形数a,b来说,取模运算或者求余运算的方法都是: 1.求 整数商 c = a / b: 2. ...

  4. Python3的取余操作

    https://blog.csdn.net/u014647208/article/details/53368244 取余代码: 输入以下代码: >>>10%2 >>> ...

  5. 计算机二级-C语言-程序修改题-190123记录-对整数进行取余和除以操作。

    //函数fun功能:将长整型数中每一位上为偶数的数依次取出,构成一个新数放在t中.高位仍在高位,低位仍在低位. //重难点:思路:因为不是字符串,所以可以把问题变成整数的操作,采用取余和除的操作.对整 ...

  6. POJ1426 Find The Multiple (宽搜思想)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24768   Accepted: 102 ...

  7. python的加、减、乘、除、取整、取余计算

    加法: 输入以下代码: >>>1+1 >>>1.0+1 减法: 输入以下代码: >>>1-2 >>>1.0-2 乘法: 输入以下 ...

  8. 高效取余运算(n-1)&hash原理探讨

    Java的HashMap源码中用到的(n-1)&hash这样的运算,查找发现这是一种高效的求余数的办法,但其中的原理是什么呢为什么可以这么做呢? 先上结论:假设被除数是x,对于除数是2n的取余 ...

  9. 【错误记录】Python 负数取余问题

    print(-123%10)   # 输出 7   print(-123%-10)  # 输出 -3 这里面第二条是我们一般意义上的取余操作.这里也特别标注一下,如果涉及到负数取余要用上述解决办法. ...

随机推荐

  1. SpringMVC Logback 设置及使用

    http://b6ec263c.wiz03.com/share/s/2SX2oY0nX4f32CY5ax1bapaL1WPGHe1OeQ-J2ijprB04A67k

  2. CentOS lvm

    1.创建PVpvcreate /dev/sdb /dev/sdc或pvcreate /dev/sdb1 /dev/sdc1 2.查看PVpvdisplay 3.创建VGvgcreate vgdata ...

  3. Spark 集群环境搭建

    思路: ①先在主机s0上安装Scala和Spark,然后复制到其它两台主机s1.s2 ②分别配置三台主机环境变量,并使用source命令使之立即生效 主机映射信息如下: 192.168.32.100 ...

  4. 安装IAR ewarm

    一  安装准备 (ST方案) 1 嵌入式集成开发环境IAR ewarm 5.41 2 J-Link4.20 3 emberznet-4.3.0协议栈安装包 option1:tools - stm32软 ...

  5. webgis技术在智慧城市综合治理(9+X)网格化社会管理平台(综治平台)的应用研究

    综治中心9+X网格化社会管理平台 为落实中央关于加强创新社会治理的要求,适应国家治理体系和治理能力现代化要求,以基层党组织为核心,以整合资源.理顺关系.健全机制.发挥作用为目标,规范街道.社区综治中心 ...

  6. lua调用java过程

    在cocos2dx框架中,有继承好的luaj文件来方便我们去使用lua调用java底层代码,注意:luaj只能使用在安卓平台下,如果在平台下使用,会出错, 所以使用前需要加平台判断,方法 如下: lo ...

  7. ES6_Promise 对象 阮一锋

    Promise的含义 promise是异步编程的一种解决方法,比传统的回调函数和事件更合理更强大.他由社区最早提出和实现,ES6将其写进语言标准,统一了用法,原生提供了promise对象.所谓prom ...

  8. 【贪心 哈夫曼树】bzoj2923: [Poi1998]The lightest language

    失去了以前用STL乱搞的能力…… 题目描述 语言也是数学上经常研究的一种数据. 给出数学上关于语言的如下定义: 字母表:大小为 K 的字母表是一个由 K 不同的字符组成的集合. 单词:长度为 m 的单 ...

  9. MySQL 查询优化之 Block Nested-Loop 与 Batched Key Access Joins

    MySQL 查询优化之 Block Nested-Loop 与 Batched Key Access Joins 在MySQL中,可以使用批量密钥访问(BKA)连接算法,该算法使用对连接表的索引访问和 ...

  10. Python3 简单封装 sqlite3 - SimpleToolSql

    #coding: utf-8 #Author:boxker #Mail:icjb@foxmail.com import sqlite3 import os class simpleToolSql(): ...