poj 1426 同余模定理加bfs

很有做的必要,题解链接http://blog.csdn.net/lyy289065406/article/details/6647917

很多人直接用long long 过了却不知道为什么,其实数据有点水,上面给出的是正确的姿势中的一种。

主要思想: 满二叉树的一个结点编号为k,则的子结点编号是2*k和2*k+1。 利用同余模定理我们知道(a*b)%n =(a%n * b%n)%n

所以有:(a*10)%n=(a%n * (10)) %n ,(a*10+1)%n=(a%n * (10)+1) %n 因此我们可以用a%n来表示a*10的值。而a%n就是上一个结点操作所得出的余数,开个

数组储存起来我们很容易可以得到:mod[i]=(mod[i/2]%n *10+i%2)%n ,当mod[i]为0时 我们就找到了i。i代表我们找到这个数进行了多少次操作。如我们找到一

个由1,0构成的数恰好整除 6 要进行 14次操作,我们可以知道最后一位数字是14%2=0 次末尾数字是 (14/2)%2=1,以此类推就可以得出这个序列。

如果还是不太明白,可以把这棵树画出来,手动模拟一下n=6的情况,会很清楚。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int mod[1000010];
int main()
{
    int n;
    while(~scanf("%d",&n)&& n!=0 )
    {
          mod[0]=-1;
          mod[1]=1%n;
          int i;
          for(i=2 ; mod[i-1] !=0 ; i++){
            mod[i]=(mod[i/2]*10 +i%2)%n;
          }
          i=i-1;
          int cnt=0;
          while(i){
            mod[cnt++]=i%2;
            i/=2;
          }
          for(int j=cnt-1;j>=0;j--){
            printf("%d",mod[j]);
          }
          printf("\n");
    }
    return 0;
}
时间: 2024-08-14 20:07:32

poj 1426 同余模定理加bfs的相关文章

poj 1426 Find The Multiple ( BFS+同余模定理)

Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18390   Accepted: 7445   Special Judge Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains

poj 1426 Find The Multiple (bfs 搜索)

Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18012   Accepted: 7297   Special Judge Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains

POJ 1426 Find The Multiple (BFS基础)

题目大意: 就是说,给你一个数字n,求出任意一个比这个n大的数字,并且这个数字是n的倍数,且只能由0和1组成. 解题思路: 刚开始考虑了大数问题,但是仔细想了下,是多虑的,因为就题目的样例来看,给你的这几个答案都很长...实际有比这个答案更小的数字. 双入口的BFS,只要一次向队列中进入两个元素就好了,q.push(x*10), q.push(x*10+1); 代码: 1 # include<cstdio> 2 # include<iostream> 3 # include<

POJ 1426 Find The Multiple &amp;amp;&amp;amp; 51nod 1109 01组成的N的倍数 (BFS + 同余模定理)

Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21436   Accepted: 8775   Special Judge Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains

DFS/BFS(同余模) POJ 1426 Find The Multiple

题目传送门 1 /* 2 题意:找出一个0和1组成的数字能整除n 3 DFS:200的范围内不会爆long long,DFS水过~ 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-2 14:21:51 8 File Name :POJ_1426.cpp 9 ******************************************

POJ 2635-The Embarrassed Cryptographer(高精度求模+同余模定理)

The Embarrassed Cryptographer Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2635 Appoint description:  System Crawler  (2015-05-28) Description The young and very promising cryptographer Odd E

[题解] [BFS] POJ 1426 - Find The Multiple

p { margin-bottom: 0.25cm; line-height: 115% } a:link { } VJudge题目:https://cn.vjudge.net/contest/279018#problem/L 即POJ 1426 - Find The Multiple:http://poj.org/problem?id=1426 题目要求: 给出一个不大于200的整数n,要求找到任意一个n的倍数(十进制),并且这个倍数只由1与0组成,最多100位. 输入输出:输入以0结束. 示

【阔别许久的博】【我要开始攻数学和几何啦】【高精度取模+同余模定理,*】POJ 2365 The Embarrassed Cryptographer

题意:给出一大数K(4 <= K <= 10^100)与一整数L(2 <= L <= 106),K为两个素数的乘积(The cryptographic keys are created from the product of two primes) 问构成K的最小素数是否绝对小于L,若是,则输出BAD p,p为最小素数,否则输出GOOD; 分析:从小到大枚举1~10^6内的素数p,while(p<L)时,判断K是否能被p整除,若能则证明构成K的最小素数绝对小于L,反之则大于L

POJ 2635 The Embarrassed Cryptographer(高精度取模 + 同余模定理)

The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12905   Accepted: 3472 Description The young and very promising cryptographer Odd Even has implemented the security module of a large system with thousands of