Codeforces 489C. Given Length and Sum of Digits... Solution

Solution in C++:

///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE,23rd batch

///Islamic University,Bangladesh 

#include<iostream>

using namespace std;

int main()

{

    int m,s,i,j;

    cin>>m>>s;

    if(s==0)

    {

        if(m==1)

        {

            cout<<"0 0"<<endl;

             return 0;

        }

        else

        {

            cout<<"-1 -1"<<endl;

            return 0;

        }

    }

    string mx,mn;

    for(i=0; i<m; i++)

    {

        int v=min(9,s);

        mx.push_back('0'+v);

        s=s-v;

    }

    if(s>0)

    {

        cout<<"-1 -1"<<endl;

        return 0;

    }

    for(i=m-1; i>=0; i--)

    {

        mn.push_back(mx[i]);

    }

    j=0;

    while(mn[j]=='0')

        j++;

        mn[0]++;

        mn[j]--;

    cout<<mn<<" "<<mx<<endl;

}


No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h ...

Powered by Blogger.