Wednesday 7 November 2018

UVA 382 - Perfection








#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,i,j,sum,x,c=0;

    while(cin>>n)
    {
        if(n==0)
        {

            break;
        }
        if(c==0)
        cout<<"PERFECTION OUTPUT"<<endl;
        x=sqrt(n);
        if(x*x==n &&n!=1)
            sum=x;
        else
        {
            sum=0;
            x=1;
        }
        if(n!=1)
        {
            for(i=2; i*i<n; i++)
            {
                if(n%i==0)
                {
                    sum+=i+(n/i);
                    //cout<<sum<<" "<<i<<" "<<n/i<<endl;
                }
            }

        sum+=1;
        }
        //cout<<sum<<" "<<n<<endl;
        if(sum==n)
            printf("%5d  PERFECT\n",n);

        else if(sum>n)
        printf("%5d  ABUNDANT\n",n);
            //cout<<n<<" ABUNDANT"<<endl;
        else
         printf("%5d  DEFICIENT\n",n);
         c++;

    }
    cout<<"END OF OUTPUT"<<endl;
    return 0;
}

Tuesday 6 November 2018

UVA 369 - Combinations






#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,m,i,c,sum,x,z,y;
    while(cin>>n>>m)
    {
        if(n==0&&m==0)
            break;
        z=n-m;
        if(m>z)
        {
            x=z;
            y=m;
        }
        else
        {
            x=m;
            y=z;
        }
        sum=1;
        c=1;
        for(i=n;i>y;i--)
        {
            //cout<<i<<" "<<sum<<" "<<c<<endl;
            sum*=i;
            sum/=c;
            if(c>=x)
                break;
            c++;
        }
        cout<<n<<" things taken "<<m<<" at a time is "<<sum<<" exactly."<<endl;
    }
    return 0;
}

Sunday 4 November 2018

UVA 272 - TEX Quotes









#include<bits/stdc++.h>
using namespace std;
int main()

{
    char s[1000000],ss[1000000];
    long long i,j,l,c,count;
    count=0;
    c=0;
    while(gets(s))
    {
        l=strlen(s);


        for(i=0; i<l; i++)
        {

            if(s[i]=='"')
            {
                c++;
                if(c%2==1)
                    cout<<"``";
                else
                    cout<<"\'\'";
            }
            else
                cout<<s[i];
        }
        cout<<endl;

    }
    return 0;
}

Saturday 3 November 2018

UVA 264 - Count on Cantor



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

vector <long long >ar,br;
int main()
{
    long long i,j,n,a=1,b=1,c,x;
    c=1;
    for(i=1; i<=1000000; i++)
    {
        if(i%2==0)
        {
            //cout<<"x="<<endl;
            a=1;
            b=i;

            for(j=1; j<=i; j++)
            {
                ar.push_back(a);
                br.push_back(b);
                b--;
                a++;

                //cout<<"A="<<ar[c]<<" "<<br[c]<<" "<<c<<endl;
                c++;

            }

        }
        else
        {
            //cout<<"y="<<endl;
            a=i;
            b=1;
            for(j=1; j<=i; j++)
            {
                ar.push_back(a);
                br.push_back(b);
                b++;
                a--;
                //cout<<"B="<<ar[c]<<" "<<br[c]<<" "<<c<<endl;
                c++;

            }
        }
        if(c>10000009)
            break;



    }
    long long count=1;
    while(cin>>x)

    {
        cout<<"TERM "<<x<<" IS "<<ar[x-1]<<"/"<<br[x-1]<<endl;
    }
    return 0;
}

Sunday 21 October 2018

Educational Codeforces Round 52 (Rated for Div. 2)/A. Vasya and Chocolate

Problem:
A. Vasya and Chocolate
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
There is a special offer in Vasya's favourite supermarket: if the customer buys a chocolate bars, he or she may take b additional bars for free. This special offer can be used any number of times.
Vasya currently has s roubles, and he wants to get as many chocolate bars for free. Each chocolate bar costs c roubles. Help Vasya to calculate the maximum possible number of chocolate bars he can get!
Input
The first line contains one integer t (1t100) — the number of testcases.
Each of the next t lines contains four integers s,a,b,c (1s,a,b,c109) — the number of roubles Vasya has, the number of chocolate bars you have to buy to use the special offer, the number of bars you get for free, and the cost of one bar, respectively.
Output
Print t lines. i-th line should contain the maximum possible number of chocolate bars Vasya can get in i-th test.
Example
input
Copy
2
10 3 1 1
1000000000 1 1000000000 1
output
Copy
13
1000000001000000000
Note
In the first test of the example Vasya can buy 9 bars, get 3 for free, buy another bar, and so he will get 13 bars.
In the second test Vasya buys 1000000000 bars and gets 1000000000000000000 for free. So he has 1000000001000000000 bars.
Solution:
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int ar[4];
    for(int i=0;i<3;i++)
        cin>>ar[i];
    sort(ar,ar+3);

    int aa=ar[0]+ar[1];
    if(aa>ar[2])
        cout<<0<<endl;
    else
        cout<<ar[2]-aa+1<<endl;
    return 0;
}

Educational Codeforces Round 52 (Rated for Div. 2)/A. Vasya and Chocolate

Problem:
A. Vasya and Chocolate
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
There is a special offer in Vasya's favourite supermarket: if the customer buys a chocolate bars, he or she may take b additional bars for free. This special offer can be used any number of times.
Vasya currently has s roubles, and he wants to get as many chocolate bars for free. Each chocolate bar costs c roubles. Help Vasya to calculate the maximum possible number of chocolate bars he can get!
Input
The first line contains one integer t (1t100) — the number of testcases.
Each of the next t lines contains four integers s,a,b,c (1s,a,b,c109) — the number of roubles Vasya has, the number of chocolate bars you have to buy to use the special offer, the number of bars you get for free, and the cost of one bar, respectively.
Output
Print t lines. i-th line should contain the maximum possible number of chocolate bars Vasya can get in i-th test.
Example
input
Copy
2
10 3 1 1
1000000000 1 1000000000 1
output
Copy
13
1000000001000000000
Note
In the first test of the example Vasya can buy 9 bars, get 3 for free, buy another bar, and so he will get 13 bars.
In the second test Vasya buys 1000000000 bars and gets 1000000000000000000 for free. So he has 1000000001000000000 bars
Solution:
#include<iostream>
using namespace std;
long long n,a,b,c,d,i;
main()
{
cin>>n;
for(i=0; i<n; i++)
{
cin>>a>>b>>c>>d;
cout<<a/d+(a/b/d)*c<<endl;
}
}