zoukankan      html  css  js  c++  java
  • poj1426 Find The Multiple(dfs双入接口+模运算)

    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
    题意:给出一个n,找n的倍数且数字都为0或1.
    我刚开始也是这样想的,从1开始找,但不知道怎么找,发现是从1到10和11,10到100和101,11到110和111,,,;
    也就是从低位开始两个入口(*10+0)和(*10+1),一直dfs下去,直到找到那个数。那么又一个问题来了,长度太长无法保存,那么就用到模运算了(其实我没想到),用6举例如下:

    模运算
    (a+b)%n=(a%n+b%n)%n
    (a*b)%n=((a%n)*(b%n))%n
    if(1%6=1)
    {
        if((1*10+0)%6=4)
        {
            if((4*10+0)%6=4)//((((1*10+0)%6)*10+0)%6=4)根据模运算下面同理
            {
                if((4*10+0)%6=4)
                {
    
                }
                else if((4*10+1)%6=5)
                {
    
                }
            }
            else if((4*10+1)%6=5)
            {
                if((5*10+0)%6=2)
                {
    
                }
                else if((5*10+1)%6=3)
                {
    
                }
            }
        }
        else if((1*10+1)%6=5)
        {
            if((5*10+0)%6=2)
            {
                if((2*10+0)%6=2)
                {
    
                }
                else if((2*10+1)%6=3)
                {
    
                }
            }
            else if((5*10+1)%6=3)
            {
                if((3*10+0)%6=0)
                {
                    得到答案
                }
                else if((3*10+1)%6=1)
                {
    
                }
            }
        }
    }
    #include<stdio.h>
    #include<string>
    #include<string.h>
    #include<queue>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int a[500000];
    int b[500000];
    int main()
    {
        int n;
        while(~scanf("%d",&n)&&n)
        {
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
            a[1]=1;
            int i;
            for(i=2;a[i-1]!=0;i++)
                a[i]=(10*a[i/2]+i%2)%n;//实在想不到还有这种操作,佩服
            i--;
            int len=0;
            while(i)
            {
                b[len++]=i%2;
                i/=2;
            }
            for(int i=len-1;i>=0;i--)
            {
                printf("%d",b[i]);
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    win7与centos虚拟机的共享文件夹创建
    MySQL视图
    MySQL分区表与合并表
    PHP读写XML文件的四种方法
    备份与恢复
    MySQL日志
    MySQL锁问题
    优化数据库对象
    ActiveReport资料
    对ArrayList 进行深拷贝
  • 原文地址:https://www.cnblogs.com/zxy160/p/7215103.html
Copyright © 2011-2022 走看看