10190 – Divide, But Not Quite Conquer! UVA Solution

This is an easy problem from uva judge http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1131

Complete search, strings.

How I did it

  • Read the n and m until reach EOF.
  • If n<2 or m<2 or m>n. Print Boring.(This gives me TLE and RTE).
  • Otehrwise simulate the process.
  • If you can develop the sequence print it otherwise print boring.

Hints

  • n and m are non negative numbers. Careful with n, m = 1 or n,m =0;

Code

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

int main()
{

long n, m;
bool flag;
while(cin>>n>>m)
{
    vector<long> vec;
    flag = true;
    vec.push_back(n);
    if( m <2 || n < 2 || m>n)
       printf("Boring!\n");
    else
    {
        while(n != 1)
        {
            if(n%m == 0)
            {
                n/=m;
                vec.push_back(n);
            }
            else
            {
                flag = false;
                break;
            }
        }
            if(flag)
            {
                for(long x=0; x<vec.size(); x++)
                {
                    if(x < vec.size() - 1)
                       printf("%ld ", vec[x]);
                    else
                        printf("%ld\n", vec[x]);
                }
            }
            else
                printf("Boring!\n");
    }
}
return 0;
}

Leave a comment