Tuesday, 5 January 2016

UVa 10943 - How do you add?

//\\__ hr1212 __//\\

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;

#define si(a) scanf("%lld",&a)
#define sii(a,b) scanf("%lld %lld",&a,&b)
#define siii(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pi(a) printf("%lld\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000

ll n,m,dp[200][200];

ll solve(ll n,ll k){
    if(n==0 && k==0)
        return 1;
    if(n<0 || k<=0)
        return 0;
    if(dp[n][k]!=-1)
        return dp[n][k];
    ll z=0,i;
    f(i,0,n+1){
        z=(z+solve(n-i,k-1))%MOD;
    }
    return dp[n][k]=z;
}

int main(){
    ll r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0;
    ll ans=0;string p;

    while(1){
        sii(n,m);
        if(n==0 && m==0)
            break;
        clr(dp,-1);
        pi(solve(n,m));
    }

    return 0;
}


Another Way to code:-

//\\__ hr1212 __//\\

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;

#define si(a) scanf("%lld",&a)
#define sii(a,b) scanf("%lld %lld",&a,&b)
#define siii(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pi(a) printf("%lld\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define mod 1000000

ll n,m,dp[209][209];

int main(){
    ll r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0;
    ll ans=0;string p;
    for(i=0;i<=200;++i)
    {
        dp[i][0]=1;
    }
    f(i,1,201)
    f(j,1,201)
    dp[i][j]=(dp[i-1][j]%mod+dp[i-1][j-1]%mod)%mod;

    while(1){
        sii(n,m);
        if(n==0 && m==0)
            break;
        pi(dp[n+m-1][m-1]);
    }

    return 0;
}

No comments:

Post a Comment