Friday, August 5, 2016

UVA 10127 - Ones solution

problem link-click here

This  is indeed an easy problem but realizing the problem statement is quite hard. but don't worry as i'm here.

in the problem it is said that you have to find the length of the multiple which is consist of only sequence of 1 ( no other digit without 1 ) for  a given number N.

for example assume that n=7 . we have to find smallest multiple of 7 and this multiple have to be consist of only the digit 1. so you just need to check whether

1,11,111,1111,11111,111111,1111111........so on which is divisible by 7 and also has to be smaller one.for 7 the smallest multiple is 111111 as,
                                       
                                                    111111 = 7 x 15873



an accepted code  is given below:


#include<bits/stdc++.h>
using namespace std;

int process(int n)
{
    vector<int> v;

    for(int i=0;i<n;i++)
    {
         long long int sum=0;

        v.push_back(1);

        for(int j=0;j<v.size();j++)
        {
            sum=(sum*10+v[j])%n;
        }

        if(sum==0)
        {
            break;
        }
    }

    return v.size();
}

int main()
{
    int n;

    while(scanf("%d",&n)==1)
    {
        cout<<process(n)<<endl;
    }

    return 0;
}

No comments:

Post a Comment