Hacker Rank The Power Sum

Problem Link: The Power Sum

#include<bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define pfn(x , k) printf(k , x)
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
#define clr clear()

typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;

const int mod = 1000007;
const int high = 1002;

int x = 0 , y;

int getWay(LL total , LL number , LL power , LL sum)
{
    LL p = powl(number , power);

    if((sum + p) > total || p > total) return 0;

    if((sum + p) == total) return 1;

    LL x = getWay(total , number + 1 , power , sum);

    LL y = getWay(total , number + 1, power , sum + p);

    return x + y;
}

int main()
{
    fast;
    LL X , N;
    while(cin >> X >> N)
    {
        cout << getWay(X , 1, N , 0) << "\n";
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LeetCode Palindrome Number

Uva 10650 - Determinate Prime