devskill dcp 262 – Binary Again

import java.math.*;
import java.util.*;
import java.io.*;
import java.lang.*;

public class MyClass {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n,t;
		t = sc.nextInt();
		
		for(int jj=0;jj<t;jj++)
		{
			n = sc.nextInt();
			
			BigInteger boro = BigInteger.valueOf(1);
			BigInteger choto = BigInteger.valueOf(1);
			
			boro = boro.shiftLeft(n);
			choto = choto.shiftLeft(1);
			
			boro = boro.subtract(choto);
			
			String ans = "";
			BigInteger wow;
			choto = BigInteger.valueOf(2);
			
			for(int k=0; k<3; k++)
			{
				wow = boro.remainder(choto);
				boro = boro.divide(choto);
				ans += wow.toString();
			}
			System.out.print(n);
			System.out.printf(" ");
			for(int k=ans.length()-1;k>=0;k--)
			{
				System.out.printf("%c",ans.charAt(k));
			}
			System.out.println();
		}
		sc.close();
	}
}

Google Codejam 2017 B – Tidy Numbers

//aarifshuvo   ``CSEJU

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define SZ(x) ((int)(x).size())
#define scl(x) scanf("%lld", &x)
#define scll(x,y) scanf("%lld %lld", &x, &y)
#define all(x) (x).begin(),(x).end()
#define mem(a,d) memset(a,d,sizeof a)
#define REP(i,n) for(int i=0;i<n;i++)
#define REV(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define pri(a) cout<<a<<endl
#define prii(a,b) cout<<a<<" "<<b<<endl
using namespace std;

#define inf 12345678912

vector <int> digs;
ll n;
ll dp[19][3];

ll go(ll i, ll issmall, ll prev, ll sum)
{
    sum = sum*10 + prev;

    if(i==SZ(digs))
    {
        return sum;
    }

    if(dp[i][issmall]!=-1) return dp[i][issmall];

    ll hi = (issmall) ? 9:digs[i];

    dp[i][issmall] = 0;

    for(int j=hi; j>=0; j--)
    {
        if(prev<=j)
            dp[i][issmall] =max(dp[i][issmall], go(i+1, issmall|j<hi,j,sum));
    }
    return dp[i][issmall];
}

ll makeandgo(ll n)
{
    digs.clear();
    if(!n) digs.pb(0);

    while(n)
    {
        digs.pb(n%10);
        n/=10;
    }
    reverse(all(digs));
    
    mem(dp,-1);
    ll ans = go(0,0,0,0);
    return ans;
}


int main()
{
   // freopen("inp.txt", "r", stdin);
   // freopen("out.txt", "W", stdout);

    ll t,cs=0;
    scl(t);
    while(t--)
    {
        scl(n);

        printf("Case #%lld: %lld\n", ++cs, makeandgo(n));
    }

    return 0;
}

Google CodeJam 2017 A – Oversized Pencake Flippers

// aarifshuvo   ``CSEJU

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define SZ(x) ((int)(x).size())
#define scl(x) scanf("%lld", &x)
#define scll(x,y) scanf("%lld %lld", &x, &y)
#define all(x) (x).begin(),(x).end()
#define mem(a,d) memset(a,d,sizeof a)
#define REP(i,n) for(int i=0;i<n;i++)
#define REV(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define pri(a) cout<<a<<endl
#define prii(a,b) cout<<a<<" "<<b<<endl
using namespace std;

#define inf 12345678912

int main()
{
   // freopen("D:/gjam17/inp.txt", "r", stdin);
   // freopen("D:/gjam17/out.txt", "w", stdout);

    ll i,t,cs=0,k;
    string s,tmp;
    scl(t);
    while(t--)
    {
        cin>>s;
        scl(k);

        tmp = s;
        bool fl = 1, fl2=1;
        ll cnt=0, cnt2=0;

        i=0;
        while(i+k<=SZ(s))
        {
            if(s[i]=='-')
            {
                cnt++;
                REP(j,k)
                {
                    if(s[i+j]=='+') s[i+j]='-';
                    else s[i+j]='+';
                }
            }
            i++;
        }
        for(; i<SZ(s); i++) if(s[i]=='-') fl=0;

        reverse(all(tmp));
        i=0;

        while(i+k<=SZ(tmp))
        {
            if(tmp[i]=='-')
            {
                cnt2++;
                REP(j,k)
                {
                    if(tmp[i+j]=='+') tmp[i+j]='-';
                    else tmp[i+j]='-';
                }
            }
            i++;
        }

        for(; i<SZ(tmp); i++) if(tmp[i]=='-') fl2=0;

        printf("Case #%lld: ", ++cs);
        if(fl==0 and fl2==0) puts("IMPOSSIBLE");
        else
        {
            if(fl==1 and fl2==1)  printf("%lld\n", min(cnt,cnt2));
            else if(fl==1) printf("%lld\n", cnt);
            else printf("%lld\n", cnt2);
        }
    }
    return 0;
}

URAL 1225 – Flags

//aarifshuvo   ``CSEJU

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define SZ(x) ((int)(x).size())
#define scl(x) scanf("%lld", &x)
#define scll(x,y) scanf("%lld %lld", &x, &y)
#define all(x) (x).begin(),(x).end()
#define mem(a,d) memset(a,d,sizeof a)
#define REP(i,n) for(int i=0;i<n;i++)
#define REV(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define pri(a) cout<<a<<endl
#define prii(a,b) cout<<a<<" "<<b<<endl
using namespace std;

ll dp[50][4][4];
ll n;

ll go(ll i, ll cur, ll prev)
{
    if(i==n-1)
    {
        if(cur==2) return 0;
        else        return 1;
    }
    ll &ret = dp[i][cur][prev];
    if(~ret)        return ret;

    ret = 0;

    if(cur==1)
    {
        ret += ( go(i+1,3,cur) + go(i+1,2,cur) );
    }
    else if(cur==3)
    {
        ret += ( go(i+1,1,cur) + go(i+1,2,cur) );
    }
    else if(cur==2)
    {
        if(prev==1) ret += go(i+1,3,cur);
        else        ret += go(i+1,1,cur);
    }
    return ret;
}

// 1 white 2 blue 3 red

int main()
{
    scl(n);
    mem(dp,-1);
    ll ans = go(0,1,0) + go(0,3,0);
    printf("%lld\n", ans);
    return 0;
}

UVa 11742 – Social Constraints

// aarifshuvo   ``CSEJU

#include <bits/stdc++.h>
#define ll long long
#define SZ(x) ((int)(x).size())
#define scl(x) scanf("%lld", &x)
#define scll(x,y) scanf("%lld %lld", &x, &y)
#define ALL(x) (x).begin(),(x).end()
#define REP(i,n) for(int i=0;i<n;i++)
#define REV(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define pri(a) cout<<a<<endl
#define prii(a,b) cout<<a<<" "<<b<<endl
using namespace std;

int main()
{
    ll n,m;
    ll arr[10],a[22],b[22],c[22];


    while(scll(n,m)==2 && n+m)
    {
        REP(i,m) scll(a[i],b[i]), scl(c[i]);

        REP(i,n) arr[i]=i;

        ll ans = 0,fl=1;

        do
        {
            REP(i,m)
            {
                ll df = abs(arr[a[i]]-arr[b[i]]);

                if(c[i]>0 and df<=c[i]) fl=1;
                else if(c[i]<0 and df>=(-c[i])) fl=1;
                else
                {
                    fl=0;
                    break;
                }
            }
            if(fl) ans++;
        }while(next_permutation(arr,arr+n));

        printf("%lld\n", ans);
    }
    return 0;
}

UVa 11567 Moliu Number Generator

// aarifshuvo   ``CSEJU

#include <bits/stdc++.h>
#define ll long long
#define SZ(x) ((int)(x).size())
#define scl(x) scanf("%lld", &x)
#define scll(x,y) scanf("%lld %lld", &x, &y)
#define ALL(x) (x).begin(),(x).end()
#define REP(i,n) for(int i=0;i<n;i++)
#define REV(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define pri(a) cout<<a<<endl
#define prii(a,b) cout<<a<<" "<<b<<endl
using namespace std;

ll cnt, ans;

void cal(ll n, ll cnt)
{
    if(n==0)
    {
        ans=min(ans,cnt);
        return;
    }

    if(n%2==0) cal(n/2,cnt+1);
    else
    {
        cal(n-1, cnt+1);
        
        if(n!=1) cal(n+1,cnt+1);
    }
}

int main()
{
    ll a;

    while(scl(a)==1)
    {
        cnt = 0;
        ans = 99999999

        cal(a, cnt);

        printf("%lld\n", ans);
    }
}

UVa 357 Let Me Count The Ways

// aarifshuvo   ``CSEJU

#include <bits/stdc++.h>
#define ll long long
#define SZ(x) ((int)(x).size())
#define scl(x) scanf("%lld", &x)
#define scll(x,y) scanf("%lld %lld", &x, &y)
#define ALL(x) (x).begin(),(x).end()
#define REP(i,n) for(int i=0;i<n;i++)
#define REV(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define pri(a) cout<<a<<endl
#define prii(a,b) cout<<a<<" "<<b<<endl
using namespace std;

ll n, mem[6][31000];
ll coins[] = {1, 5, 10, 25, 50};

ll call(ll i, ll feas)
{
    if(feas==0) return 1;
    if(i==5) return 0;

    if(mem[i][feas]!=-1) return mem[i][feas];

    mem[i][feas] = 0;

    for(int j=0; feas-coins[i]*j>=0; j++)
    {
        mem[i][feas] += call(i+1, feas-coins[i]*j);
    }
    return mem[i][feas];
}

int main()
{
    memset(mem,-1,sizeof mem);
    while(~scl(n))
    {
        ll res = call(0,n);
        if(res==1)
            printf("There is only 1 way to produce %lld cents change.\n", n);
        else
            printf("There are %lld ways to produce %lld cents change.\n", res, n);
    }
    return 0;
}