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居然超时了;所以我打了个表,有一个不打表的做法;

代码如下:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<queue>
 6 using namespace std;
 7
 8 queue<int> q;
 9 int n;
10 char a[205][200]={"1","10","111","100","10","1110","1001","1000","111111111","10","11","11100","1001","10010","1110","10000","11101","1111111110","11001","100","10101","110","110101","111000","100","10010","1101111111","100100","1101101","1110","111011","100000","111111","111010","10010","11111111100","111","110010","10101","1000","11111","101010","1101101","1100","1111111110","1101010","10011","1110000","1100001","100","100011","100100","100011","11011111110","110","1001000","11001","11011010","11011111","11100","100101","1110110","1111011111","1000000","10010","1111110","1101011","1110100","10000101","10010","10011","111111111000","10001","1110","11100","1100100","1001","101010","10010011","10000","1111111101","111110","101011","1010100","111010","11011010","11010111","11000","11010101","1111111110","1001","11010100","10000011","100110","110010","11100000","11100001","11000010","111111111111111111","100","101","1000110","11100001","1001000","101010","1000110","100010011","110111111100","1001010111","110","111","10010000","1011011","110010","1101010","110110100","10101111111","110111110","100111011","111000","11011","1001010","10001100111","11101100","1000","11110111110","11010011","10000000","100100001","10010","101001","11111100","11101111","11010110","11011111110","11101000","10001","100001010","110110101","100100","10011","100110","1001","1111111110000","11011010","100010","1100001","11100","110111","11100","1110001","11001000","10111110111","10010","1110110","1010100","10101101011","100100110","100011","100000","11101111","11111111010","1010111","1111100","1111110","1010110","11111011","10101000","10111101","111010","1111011111","110110100","1011001101","110101110","100100","110000","100101111","110101010","11010111","11111111100","1001111","10010","100101","110101000","1110","100000110","1001011","1001100","1010111010111","110010","11101111","111000000","11001","111000010","101010","110000100","1101000101","1111111111111111110","111000011","1000"};
11 /*
12 int cou=0;
13 void bfs()
14 {
15     while(!q.empty())
16     {
17         int t=q.front();
18         q.pop();
19             cou++;
20         if(t%n==0)
21             return;
22         q.push(t*10%n);
23         q.push((t*10+1)%n);
24     }
25 }
26 int main()
27 {
28     freopen("out","w",stdout);
29     for(int z=1;z<=200;z++)
30     {
31         n=z;
32         while(!q.empty())
33             q.pop();
34         memset(a,0,sizeof(a));
35         cou=0;
36         a[0]=1;
37        q.push(1);
38        bfs();
39        int i=0;
40        while(cou)
41        {
42            a[i++]=cou%2;
43            cou=cou/2;
44        }
45        printf("\"");
46        for(int j=i-1;j>=0;j--)
47        {
48            printf("%d",a[j]);
49        }
50        printf("\",");
51     }
52     return 0;
53 }*/
54
55 int main()
56 {
57     while(cin>>n&&n)
58     {
59         printf("%s\n",a[n-1]);
60     }
61     return 0;
62 }

 1 //Memory Time
 2 //2236K  32MS
 3
 4 #include<iostream>
 5 using namespace std;
 6
 7 int mod[524286];  //保存每次mod n的余数
 8                   //由于198的余数序列是最长的
 9                   //经过反复二分验证,436905是能存储198余数序列的最少空间
10                   //但POJ肯定又越界测试了...524286是AC的最低下限,不然铁定RE
11
12 int main(int i)
13 {
14     int n;
15     while(cin>>n)
16     {
17         if(!n)
18             break;
19
20         mod[1]=1%n;  //初始化,n倍数的最高位必是1
21
22         for(i=2;mod[i-1]!=0;i++)  //利用同余模定理,从前一步的余数mod[i/2]得到下一步的余数mod[i]
23             mod[i]=(mod[i/2]*10+i%2)%n;
24                      //mod[i/2]*10+i%2模拟了BFS的双入口搜索
25                      //当i为偶数时,+0,即取当前位数字为0  。为奇数时,则+1,即取当前位数字为1
26
27         i--;
28         int pm=0;
29         while(i)
30         {
31             mod[pm++]=i%2;   //把*10操作转化为%2操作,逆向求倍数的每一位数字
32             i/=2;
33         }
34         while(pm)
35             cout<<mod[--pm];  //倒序输出
36         cout<<endl;
37     }
38     return 0;
39 }

时间: 2024-10-10 23:25:10

kb-01-e<取余操作,宽搜,巧妙>;的相关文章

leetcode——Divide Two Integers 不用乘除取余操作求除法(AC)

Divide two integers without using multiplication, division and mod operator. 题目只有简单的一句话,看起来可真简单啊,呵呵,假象.这个题目的难点在于对时间效率的限制和边界值的测试.第一印象肯定是循环一个个把因子从被除数中减去不久行了么,可是对于比如INT_MAX/1或者INT_MIN/1之类的执行时间长的可怕,会超出时间限制.改善时间效率的思路是参考网上别人代码,将因子不断乘以2(可以通过移位实现,同时结果也从1开始不断

Java之取余操作 &quot;%&quot;

取模运算与取余运算两个概念有重叠的部分但又不完全一致.主要的区别在于对负整数进行除法运算时操作不同. 对于整形数a,b来说,取模运算或者求余运算的方法都是: 1.求 整数商 c = a / b: 2.计算模或者余数 r = a - c* b . 取模运算和取余运算在第一步不同: 取余运算在取c值时,向0方向舍入:而取模运算在取c值时,是向负无穷方向舍入 各个环境下运算符%的含义不同,C/C++,Java为取余,python为取模 Java取余运算规则如下: a%b = a - (a/b)*b 原

leecode---07---数字整除取余操作,取余整除---翻转一个整数复件

https://leetcode.com/problems/reverse-integer/description/ 题意 将一个整数进行翻转. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 分析 给一个整数,将这个整数进行翻转,从后往前每一位先取余出来. 最后要判断是否越界处理. 代码 class Solution { public i

从C++和Python除法的区别谈谈求模(Modulus)和取余(Remainder)

今天发现一个很有意思的现象. 当做除法的时候,Python2和C++在负数的情况下会得到不同的整除结果: 当做-5 / 3的时候 C++的结果: -1 Python2的结果:-2 (请注意5 / -3的时候仍然会在C++中得到-1, Python2中得到-2) 可以看出C++在进行负数整除的时候执行的是直接舍去小数点后数字的操作,也就是返回和0比较接近的那个数字. 但在Python2中返回的则是小于等于商的最大整数,也就是返回和-∞更接近的数. 在做%操作的时候,依据的是这样的逻辑: a = b

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

加法: 输入以下代码: >>>1+1 >>>1.0+1 减法: 输入以下代码: >>>1-2 >>>1.0-2 乘法: 输入以下代码: >>>2*4 >>>2.0*4 除法: 输入以下代码: >>>2/4 >>>2.0/4 >>>2//4 >>>2.0//4 取整: 输入以下代码: >>>2//4 >&g

【图论】【宽搜】【染色】NCPC 2014 A Ades

题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1787 题目大意: N个点M条无向边(N,M<=200000),一个节点只能有一个标记.每条边有一个值{0,1或2}表示这条边连接的两个节点拥有的标记之和.问只要要多少个标记才能满足,无解impossible. 题目思路: [图论][宽搜] 因为每个点只能有或没有标记.所以可以枚举每个联通块的其中一个点有还是没有标记,用这个点去拓展这个点的联通块并01染色(这个点所能到达的所有点) 初

salesforce 零基础学习(四十三)运算取余

工作中遇到一个简单的小问题,判断两个数是否整除,如果不整除,获取相关的余数. 习惯java的我毫不犹豫的写下了代码 public Boolean isDivisibility(Integer dividend,Integer divider) { return dividend % divider == 0; } 提交代码发现竟然提交不上?? 后来查看API发现apex中没有直接的%取余运算,所以如果想要取余以及判断是否整除需要其他方式,代码如下: public without sharing

汇编总结:无符号除法,有符号除法,取余,无符号乘法,有符号乘法

本文分为3个模块. 示例---该指令的示例 解释---为指令不好理解的地方 练习---为了更熟悉该指令 1.1 有符号除法指令及取余example: 在c语言里要完成 8 / 2的汇编指令如下: 在c语言里要完成 8 % 2的汇编指令如下: 4个字节的除法及取余运算示例如下: .section .text .global _start _start:     movl $8, %eax   #被除数是%edx:%eax 是这两个寄存器拼起来的%eax存放低位%edx存储高位     movl %

【NOIP2016提高组】愤怒的小鸟(状压宽搜)

题目描述 Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可以用它向第一象限发射一只红色的小鸟,小鸟们的飞行轨迹均为形如的曲线,其中a,b是Kiana指定的参数,且必须满足a<0. 当小鸟落回地面(即x轴)时,它就会瞬间消失. 在游戏的某个关卡里,平面的第一象限中有n只绿色的小猪,其中第i只小猪所在的坐标为(xi,yi). 如果某只小鸟的飞行轨迹经过了(xi,yi),那么第i只小猪就会被消灭掉,同时小鸟将会沿着原先