zoukankan      html  css  js  c++  java
  • poj1426 Find The Multiple (DFS)

    Find The Multiple
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 41845   Accepted: 17564   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

    Source

    题意:找出一个最小的由1或者0组成的十进制数能整除n
    题解:dfs,一开始看题目说十进制数会达到100位,其实不会,在longlong的范围内,下次遇到这种可以直接打表找一下规律
    代码:
    #include<iostream>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int,int> PII;
    const ll mod=998244353;
    #define pb push_back
    #define mp make_pair
    #define all(x) (x).begin(),(x).end()
    #define fi first
    #define se second
    //head
    #define MAX 1000
    int n;
    int flag;
    void dfs(int cur,ll sum)
    {
        if(cur==19)return ;
        if(flag) return ;
       if(sum%n==0&&sum!=0)
       {
           flag=1;
           cout<<sum<<endl;
           return ;
       }
       else
        dfs(cur+1,sum*10),dfs(cur+1,sum*10+1);
    }
    int main()
    {
          cin.tie(0);
        cout.tie(0);
        ios::sync_with_stdio(0);
        while(cin>>n)
        {
            if(n==0)break;
            flag=0;
            dfs(0,1);
        }
        return 0;
    }
  • 相关阅读:
    如何给LG gram写一个Linux下的驱动?
    题解 CF492C Vanya and Exams
    题解 CF546B Soldier and Badges
    题解 P1283 【平板涂色】
    题解 P1717 【钓鱼】
    题解 UVA663 Sorting Slides(烦人的幻灯片)
    题解 P2835 【刻录光盘】
    题解 P5367 【【模板】康托展开】
    题解 P2949 【[USACO09OPEN]工作调度Work Scheduling】
    题解 P2272 【[ZJOI2007]最大半连通子图】
  • 原文地址:https://www.cnblogs.com/zhgyki/p/9503250.html
Copyright © 2011-2022 走看看