Uva 11970 - Lucky Numbers

/*

It is very interesting problem ever :). Just need a good problem analysis.
Look, We have a equation x / sqrt(n - x), we have to find out the value of x, where x is an integer and must x>0 otherwise
it will be undefined;

At first, think with Brute force...
sqrt(n - x) = 2;
=> n - x = 4;
=> - x = 4 - n;
=> x = n - 4;

if n=16, then
    x = 16 - 4 = 12;
    now check weather x / sqrt(n - x) is an integer or not !
    Note: a number may be an integer if has Modulus is 0;
    So, 12 / sqrt(16 - 12) = 12 / sqrt(4) = 12 / 2 = 6; That means 12 % 2 = 0; It is an integer.
    So, Answer is 12..

    On that Brute Force system I have carried that problem to till sqrt(n);

*/

// Header file begin
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
#include<vector>
#include<cmath>
#include<cctype>
#include<sstream>
#include<set>
#include<list>
#include<stack>
#include<utility>
#include<queue>
#include<algorithm>
#include<cstdlib>
#include<bitset>
// End
//..........
// Macro
#define sf scanf
#define pf printf
#define lp1(i,n) for(i=0;i<n;i++)
#define lp2(i,n) for(i=1;i<=n;i++)
#define mset(c,v) memset(c,v,sizeof(c))
#define cp(a) cout<<" "<<a<<" "
#define nl puts("")
#define sq(x) ((x)*(x))
#define all(x) x.begin(),x.end()
#define reall(x) x.rbegin(),x.rend()
#define s_wap(x,y) x^=y;y^=x;x^=y;
#define sz size()
#define gc getchar()
#define pb push_back
#define freader freopen("input.txt","r",stdin)
// End.........

// Size
#define mx7 20000100
#define mx6 1500000
#define mx5 100005
#define mx4 1000100
#define inf 1<<30                                           //infinity value
#define eps 1e-9
#define mx (65540)
#define mod 1000000007
#define pi acos(-1.0)

// Macros for Graph

#define white 1
#define gray 2
#define black 3
#define nil 0

using namespace std;
/***************/

// typedef

typedef long long LL;
typedef long L;
typedef unsigned long long ull;
typedef unsigned long ul;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef vector<int>vi;
typedef vector<long long> vll;
typedef vector<long>vl;
typedef vector<char>vch;
typedef vector<string>vs;
typedef map<int,int>mpii;
typedef map<int,bool>mpbi;
typedef map<long,bool>mpbl;
typedef map<long long,bool>mpbll;
typedef map<char,int>mpci;
typedef map<char,bool>mpbc;
typedef map<string,int>mpsi;
typedef map<long long,long long>mpll;

// template

template<class T> T gcd(T a, T b ) {return b!=0?gcd<T>(b,a%b):a;}
template<class T> T large(T a, T b ) {return a>b?a:b;}
template<class T> T small(T a, T b ) {return a<b?a:b;}
template<class T> T diffrnce(T a, T b) {return a-b<0?b-a:a-b;}

LL ans[mx4+9];

void lucky(LL nm)
{
    int len=0;
    ans[len++] = nm-1;
    LL i;

    for(i=2; i*i<=nm; i++)
    {
        LL tmp = i*i;
        LL x = nm - tmp;
        L tp = sqrt(tmp);
        L res = (x % tp);

        if(!res)
        {
            //pf("%lld ",x);
            ans[len++] = x;
        }
    }

    sort(ans, ans+len);

    bool f=false;

    ans[0]==0 ? i=1 : i=0;

    for(;i<len;i++)
    {
        if(!f)
        {
            pf("%lld",ans[i]);
            f=true;
        }
        else
        {
            pf(" %lld",ans[i]);
        }
    }

    nl;
}

int main()
{
    int t,tc=0;
    sf("%d",&t);

    while(t--)
    {
        LL n;
        sf("%lld",&n);
        pf("Case %d: ",++tc);
        lucky(n);
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number