• poj 1426 Find The Multiple(搜索BFS的思想+ 同余模定理+二叉树+01哈夫曼编码)


    http://poj.org/problem?id=1426

    Find The Multiple
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 14519   Accepted: 5893   Special Judge

    Description

    Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.

    Input

    The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.

    Output

    For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.

    Sample Input

    2
    6
    19
    0

    Sample Output

    10
    100100100100100100
    111111111111111111

    思路:

    这里结果只有01,所以首先想到的是用二叉树的结构,

     而又由于结果是一个01序列,那能不能把 *10得到k每一位的问题 转化为模2的操作得到k的每一位(0或1) 呢?

    答案是可以的

    首先我们利用 同余模定理 对得到余数的方式进行一个优化

    (a*b)%n = (a%n *b%n)%n

    (a+b)%n = (a%n +b%n)%n

     

    代码1:

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<queue>
     5 using namespace std;
     6 
     7 int mod[600000];
     8 int res[200];
     9 
    10 int main()
    11 {
    12     int n;
    13     while(~scanf("%d",&n)&&n)
    14     {
    15         mod[1]=1%n;  //初始化,n倍数的最高位必是1
    16         int i;
    17 
    18         for(i=2;mod[i-1];i++)
    19         {
    20             //利用同余模定理,从前一步的余数mod[i/2]得到下一步的余数mod[i]
    21             //mod[i/2]*10+i%2模拟了BFS的双入口搜索
    22             mod[i]=(mod[i/2]*10+i%2)%n;
    23         }
    24         int id=0;
    25         i--;
    26         while(i)  //像哈夫曼编码一样的,倒着求根节点的01编码
    27         {
    28             res[id++]=i%2;
    29             i/=2;
    30         }
    31         for(i=id-1;i>=0;i--)  //倒序输出
    32         {
    33             printf("%d",res[i]);
    34         }
    35         putchar(10);
    36     }
    37     return 0;
    38 }

    看来想得有点多了(下面这代码居然AC了)

    代码2:

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<queue>
     5 using namespace std;
     6 
     7 __int64 mod[600000];
     8 
     9 int main()
    10 {
    11     int n;
    12     while(~scanf("%d",&n)&&n)
    13     {
    14         int i;
    15         for(i=1;;i++)
    16         {
    17             mod[i]=mod[i/2]*10+i%2;
    18             if(mod[i]%n==0)
    19             {
    20                 break;
    21             }
    22         }
    23         printf("%I64d\n",mod[i]);
    24     }
    25     return 0;
    26 }

    想不通用BFS直接写为什么会超时,用数组模拟的可以优化这么多?????

    附上超时BFS代码(代码3)“下面代码poj超时”:

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<queue>
     5 using namespace std;
     6 
     7 __int64 bfs(int m)
     8 {
     9     queue<__int64> q;
    10     __int64 temp;
    11     q.push(1);
    12     while(!q.empty())
    13     {
    14         temp=q.front();
    15         q.pop();
    16         if(temp%m==0)
    17             return temp;
    18         q.push(temp*10+1);
    19         q.push(temp*10);
    20     }
    21     return 0;
    22 }
    23 
    24 int main()
    25 {
    26     int n;
    27     while(~scanf("%d",&n)&&n)
    28     {
    29         printf("%I64d\n",bfs(n));
    30     }
    31     return 0;
    32 }

     

     

     

  • 相关阅读:
    如何解决IOS 下固定定位fixed 失效问题?
    fixed定位按钮在底部,键盘弹起,按钮被顶上去
    js对象和jquery对象相互转换
    # js中substr、substring、indexOf、lastIndexOf的用法
    在新页面中返回到前一页浏览的历史位置
    vue-cli构建的项目中axios的请求方式及跨域处理
    搭建基于Express框架运行环境 及其启动方式
    微信小程序Page中data数据操作和函数调用
    linux的一些基本操作
    JavaScript对象
  • 原文地址:https://www.cnblogs.com/crazyapple/p/3116169.html
Copyright © 2020-2023  润新知