Timus-1009 K-based Numbers

Problem NameK-based Numbers
JudgeTimus
Problem Linkhttps://acm.timus.ru/problem.aspx?space=1&num=1009
Algorithms & DSDynamic Programming
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define PB push_back
#define VST(V) sort(V.begin(),V.end())
#define VSTrev(V) sort(V.begin(),V.end(),greater<long long int>())
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll dp[30][30];
int main()
{
    //freopen("1input.txt","r",stdin);
    fast;
    ll tcase=1;
    //cin>>tcase;
    for(ll test=1; test<=tcase; test++)
    {
        ll n,k;
        cin>>n>>k;
        for(ll i=0; i<=n; i++)
        {
            for(ll j=0; j<=k; j++)
            {
                dp[i][j]=0;
            }
        }
        for(ll i=0; i<=k; i++)dp[0][i]=1;
        dp[0][0]=0;
        for(ll i=1; i<=n; i++)
        {
            for(ll j=0; j<k; j++)
            {
                for(ll x=0; x<k; x++)
                {
                    if(j==0&&x==0)continue;
                    dp[i][j]+=dp[i-1][x];
                }
            }
        }
        ll ans=0;
        for(ll i=0; i<k; i++)
        {
            ans+=dp[n-1][i];
        }
        cout<<ans<<"\n";
    }
    return 0;
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
error: Alert: Content is protected !!