Thursday, June 16, 2016

1014 - Ifter Party

Problem link : http://lightoj.com/volume_showproblem.php?problem=1014

Solution idea : very simple. Just need to find all the divsors of (p-l) by iterating a for loop from 1 to square root of (p-l) and we will select the number if the number is >l and has not been selected before.

Solution :

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

vector<int>ans;

void nod(long long int  n,long long int  l)
{
    int sq=sqrt(n);

    if(n==0)
    {
        ans.push_back(1);
        return;
    }
    for(int i=1; i<=sq; i++)
    {

        long long int a;
        a=n/i;
        if(n%i==0)
        {
            if(i>l) ans.push_back(i);
            if(a>l and a!=i)ans.push_back(a);
        }
    }
}

int main()
{
    int t;
    cin>>t;
    long long int p,l;

    for(int ca=1; ca<=t; ca++)
    {
        cin>>p>>l;
        nod(p-l,l);
        sort(ans.begin(),ans.end());
        int q=ans.size();
        if(q==0)
        {
            cout<<"Case "<<ca<<": "<<"impossible"<<endl;
        }
        else
        {
            cout<<"Case "<<ca<<":";
            for(int i=0; i<q; i++) cout<<" "<<ans[i];
            cout<<endl;
        }
        ans.clear();
    }

}

No comments:

Post a Comment