UVA 294 Divisors Solution


In this problem it's easy to get TLE while using general approach. here you have to follow one rule which is

       1. The total number of divisors of a number is just doubled of divisors till the square root of that number though there's some special case which is if the square root of given number is an integer then the total number of divisors is 2*number of divisors till sqrt(number) -1 otherwise 2*number of divisors till sqrt(number).

for example let a given number is 6 then sqrt(6) = 2.4494 . here 1 and 2 (2 divisors till sqrt(6)) is the divisors till 2.4494(sqrt(6)) and the square root is not an integer(2.4494) so total number of divisors is 2*2=4.

again, let a given number is 25 then sqrt(25) = 5 . here 1 and 5 (2 divisors till sqrt(25)) is the divisors till 5(sqrt(25)) and the square root is an integer(5) so total number of divisors is 2*2-1=3.
similarly you will have to find each number's total number of divisors. then output the number which has largest divisors.

an accepted code is given below in c++:

Solution in c++:
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long  t,a,b,r,j,c,i,temp,value,mx;
    cin>>t;
    while(t--)
    {
        mx=0;
        cin>>a>>b;
        for(i=a; i<=b; i++)
        {
            c=0;
            for(j=1;j<=sqrt(i); j++)
            {
                if(i%j==0)
                {
                    c++;
                }
            }
            int temp=sqrt(i);
            if(temp==sqrt(i))
            {
                c=2*c-1;
            }
            else
            {
                c=2*c;
            }

            if(c>mx)
            {
               mx=c;
               value=i;
            }
        }
        printf("Between %lld and %lld, %lld has a maximum of %lld divisors.\n",a,b,value,mx);
    }
}

1 comment:

  1. As reported by Stanford Medical, It is in fact the one and ONLY reason this country's women get to live 10 years longer and weigh an average of 42 lbs less than we do.

    (By the way, it has totally NOTHING to do with genetics or some secret-exercise and really, EVERYTHING around "how" they eat.)

    BTW, I said "HOW", not "what"...

    TAP on this link to see if this easy test can help you find out your real weight loss possibilities

    ReplyDelete

Most View Post

Recent post

Codeforces Round 925 (Div. 3) 1931D. Divisible Pairs Solution

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

Powered by Blogger.