(LightOj) 1126 – Building Twin Towers

0

The main idea is: dp[i][j] represent what is the maximum sum of two towers when the difference is j.


/***********Template Starts Here***********/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <functional>
#include <string>
#include <iostream>
#include <cctype>
#include <set>
#include <climits>
#include <iomanip>
#include <cassert>
//#include <unordered_map>

#define pb push_back
#define nl puts ("")
#define sp printf ( " " )
#define phl printf ( "hello\n" )
#define ff first
#define ss second
#define POPCOUNT __builtin_popcountll
#define RIGHTMOST __builtin_ctzll
#define LEFTMOST(x) (63-__builtin_clzll((x)))
#define MP make_pair
#define FOR(i,x,y) for(int i = (x) ; i <= (y) ; ++i)
#define ROF(i,x,y) for(int i = (y) ; i >= (x) ; --i)
#define CLR(x,y) memset(x,y,sizeof(x))
#define UNIQUE(V) (V).erase(unique((V).begin(),(V).end()),(V).end())
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define NUMDIGIT(x,y) (((int)(log10((x))/log10((y))))+1)
#define SQ(x) ((x)*(x))
#define ABS(x) ((x)<0?-(x):(x))
#define FABS(x) ((x)+eps<0?-(x):(x))
#define ALL(x) (x).begin(),(x).end()
#define LCM(x,y) (((x)/gcd((x),(y)))*(y))
#define SZ(x) ((int)(x).size())
#define NORM(x) if(x>=mod)x-=mod;

using namespace std;

typedef long long vlong;
typedef unsigned long long uvlong;
typedef pair < int, int > pii;
typedef pair < vlong, vlong > pll;
typedef vector<pii> vii;
typedef vector<int> vi;

const vlong inf = 2147383647;
const double pi = 2 * acos ( 0.0 );
const double eps = 1e-9;

#ifdef forthright48
     #include <ctime>
     clock_t tStart = clock();
     #define debug(args...) {dbg,args; cerr<<endl;}
     #define timeStamp printf("Execution Time: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC)
#else
    #define debug(args...)  // Just strip off all debug tokens
    #define timeStamp
#endif

struct debugger{
    template<typename T> debugger& operator , (const T& v){
        cerr<<v<<" ";
        return *this;
    }
}dbg;

//int knightDir[8][2] = { {-2,1},{-1,2},{1,2},{2,1},{2,-1},{-1,-2},{1,-2},{-2,-1} };
//int dir4[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};

inline vlong gcd ( vlong a, vlong b ) {
    a = ABS ( a ); b = ABS ( b );
    while ( b ) { a = a % b; swap ( a, b ); } return a;
}

vlong ext_gcd ( vlong A, vlong B, vlong *X, vlong *Y ){
    vlong x2, y2, x1, y1, x, y, r2, r1, q, r;
    x2 = 1; y2 = 0;
    x1 = 0; y1 = 1;
    for (r2 = A, r1 = B; r1 != 0; r2 = r1, r1 = r, x2 = x1, y2 = y1, x1 = x, y1 = y ) {
        q = r2 / r1;
        r = r2 % r1;
        x = x2 - (q * x1);
        y = y2 - (q * y1);
    }
    *X = x2; *Y = y2;
    return r2;
}

inline vlong modInv ( vlong a, vlong m ) {
    vlong x, y;
    ext_gcd( a, m, &x, &y );
    if ( x < 0 ) x += m; //modInv is never negative
    return x;
}

inline vlong power ( vlong a, vlong p ) {
    vlong res = 1, x = a;
    while ( p ) {
        if ( p & 1 ) res = ( res * x );
        x = ( x * x ); p >>= 1;
    }
    return res;
}

inline vlong bigmod ( vlong a, vlong p, vlong m ) {
    vlong res = 1 % m, x = a % m;
    while ( p ) {
        if ( p & 1 ) res = ( res * x ) % m;
        x = ( x * x ) % m; p >>= 1;
    }
    return res;
}

/***********Template Ends Here***********/

/*********** Hasib Templates Starts Here**********/

#include<bits/stdc++.h>
using namespace std;

#define loop(i, n) for(int i=0; i<(n); i++)
#define sf scanf
#define pf printf
#define fr first
#define sc second
#define ll long long
#define dd double
#define all(v) (v).begin(), (v).end()
#define PI acos(-1.0)
#define mem(ara, val) memset(ara, val, sizeof(ara))
#define paii pair<int, int>
#define pall pair<ll, ll>
#define read(nm) freopen(nm, "r", stdin)
#define write(nm) freopen(nm, "w", stdout)
#define vdump(x) cerr<<#x<<" = "<<x<<endl;
#define dump(args...) cerr,args; cerr<<endl

template<typename T>
ostream& operator<<(ostream& out, vector<T> v)
{
    out<<"[ ";

    loop(i, SZ(v))
    {
        if(i) out<<", ";
        out<<v[i];
    }

    out<<" ]";
    return out;
}

template<typename T1, typename T2>
ostream& operator<<(ostream &out, pair<T1, T2> p)
{
    out<<"( "<<p.fr<<", "<<p.sc<<")";
    return out;
}

template<typename T>
ostream& operator,(ostream &out, T x)
{
    out<<x<<" ";
    return out;
}


/**********Hasib Templates Ends Here**************/

/**
 *  WARTNING for me:
 *     Never use    FOR
 *     Never use    pii, pll, vi, vi
 *     Never use    ff, ss, phl, sp, nl
 */

 #define MAXX 57
 #define MAXSUM 500007

 int ara[MAXX];
 int N;

int dp[2][MAXSUM];

int solve()
{
    mem(dp, -1);

    dp[0][0] = 0;

    int now, pre;

    now = 1, pre = 0;


    loop(i, N)
    {
        loop(j, MAXSUM)
        {
            if(dp[pre][j] != -1)
            {
                dp[now][j] = max(dp[now][j], dp[pre][j]);

                int diff = abs(j - ara[i]);

                dp[now][diff] = max(dp[now][diff], max(dp[pre][diff], dp[pre][ j ] + ara[i] ));

                //pf("dp[%d] = %d\n", diff, dp[now][diff]);

                diff = j + ara[i];

                dp[now][diff] = max(dp[now][diff], max(dp[pre][diff], dp[pre][ j ] + ara[i] ));

                //pf("dp[%d] = %d\n", diff, dp[now][diff]);

            }
        }

        swap(now, pre);
    }

    return max(dp[now][0], dp[pre][0]);




}


int main ()
{
    #ifdef hasibpc
        read("input.txt");
        //write("output.txt");
    #endif // hasibpc


    int kases, kaseno = 0;

    sf("%d", &kases);

    while(kases--)
    {
        sf("%d", &N);
        loop(i, N)
        {
            sf("%d", &ara[i]);
        }


        sort(ara, ara+N); ///optimization 😀

        int ret = solve();

        if(ret != 0)
        {
            pf("Case %d: %d\n",++kaseno, ret/2);
        }
        else
        {
            pf("Case %d: impossible\n", ++kaseno);
        }


    }



    return 0;
}


(LightOj) 1014 – Ifter Party

0


Note the looping style for finding divisors. It’s the perfect way! Don’t try otherwise, or you will get Wrong divisors for 2.


/****************************************************************
   ▄█    █▄       ▄████████    ▄████████  ▄█  ▀█████████▄
  ███    ███     ███    ███   ███    ███ ███    ███    ███
  ███    ███     ███    ███   ███    █▀  ███   ███    ███
 ▄███▄▄▄▄███▄▄   ███    ███   ███        ███  ▄███▄▄▄██▀
▀▀███▀▀▀▀███▀  ▀███████████ ▀███████████ ███ ▀▀███▀▀▀██▄
  ███    ███     ███    ███          ███ ███    ███    ██▄
  ███    ███     ███    ███    ▄█    ███ ███    ███    ███
  ███    █▀      ███    █▀   ▄████████▀  █▀   ▄█████████▀
****************************************************************/



#include<bits/stdc++.h>


#define FOR(i, s, e) for(int i=s; i<e; i++)
#define loop(i, n) FOR(i, 0, n)
#define sf scanf
#define pf printf
#define pb push_back
#define MP make_pair
#define fr first
#define sc second
#define ll long long
#define dd double
#define all(v) v.begin(), v.end()
#define PI acos(-1.0)
#define mem(ara, value) memset(ara, value, sizeof(ara))
#define paii pair<int, int>
#define pall pair<ll, ll>
#define SZ(a) int(a.size())
#define read(nm) freopen(nm, "r", stdin)
#define write(nm) freopen(nm, "w", stdout)

#define take(args...) asdf,args
#define dump(x) cerr<<#x<<" = "<<x<<endl
#define debug(args...) cerr,args; cerr<<endl;
using namespace std;


template<typename T>
ostream& operator<<(ostream& output, vector<T>&v)
{
    output<<"[ ";
    if(SZ(v))
    {
        output<<v[0];
    }
    FOR(i, 1, SZ(v))
    {
        output<<", "<<v[i];
    }
    output<<" ]";
    return output;
}

template<typename T1, typename T2>
ostream& operator<<(ostream& output, pair<T1, T2>&p)
{
    output<<"( "<<p.fr<<", "<<p.sc<<" )";
    return output;
}




template<typename T>
ostream& operator,(ostream& output, T x)
{
    output<<x<<" ";
    return output;
}



struct ASDF{
    ASDF& operator,(int &a) {
        sf("%d", &a);
        return *this;
    }
    ASDF& operator,(long int &a){
        sf("%ld", &a);
        return *this;
    }
    ASDF& operator,(long long int &a){
        sf("%lld", &a);
        return *this;
    }
    ASDF& operator,(char &c){
        sf("%c", &c);
        return *this;
    }
    ASDF& operator,(double &d){
        sf("%lf", &d);
        return *this;
    }

    template<typename T>
    ASDF& operator,(T &a){
        cin>>a;
        return *this;
    }
}asdf;



//Header ends here

ll P, L;
//vector<ll>choto, boro;

vector<ll> result;

int main()
{

    int kases, kaseno = 0;

    sf("%d", &kases);

    while(kases--)
    {
        sf("%lld %lld", &P, &L);

        ll N = P - L;

        ll root = sqrt(N);

        result.clear();

        //ll till = max(L, root);


        for(ll q = 1; q<=root; q++)
        {
            if( (N%q) == 0)
            {
                if(q > L)
                {
                    result.pb(q);
                }

                if( (q*q != N) && (N/q) > L)
                {
                    result.pb(N/q);
                }

            }
        }

        //dump(result);

        sort(all(result));


        pf("Case %d:", ++kaseno);

        if(SZ(result))
        {
            loop(i, SZ(result))
            {
                pf(" %lld", result[i]);
            }
            pf("\n");
        }
        else
        {
            pf(" impossible\n");
        }



    }



    return 0;
}


(CodeChef) MasterChef

0

A nice idea with memory optimization for Knapsack dp


// Link: http://www.codechef.com/JULY15/problems/MCHEF/


/****************************************************************
   ▄█    █▄       ▄████████    ▄████████  ▄█  ▀█████████▄
  ███    ███     ███    ███   ███    ███ ███    ███    ███
  ███    ███     ███    ███   ███    █▀  ███   ███    ███
 ▄███▄▄▄▄███▄▄   ███    ███   ███        ███  ▄███▄▄▄██▀
▀▀███▀▀▀▀███▀  ▀███████████ ▀███████████ ███ ▀▀███▀▀▀██▄
  ███    ███     ███    ███          ███ ███    ███    ██▄
  ███    ███     ███    ███    ▄█    ███ ███    ███    ███
  ███    █▀      ███    █▀   ▄████████▀  █▀   ▄█████████▀
****************************************************************/



#include<bits/stdc++.h>


#define FOR(i, s, e) for(int i=s; i<e; i++)
#define loop(i, n) FOR(i, 0, n)
#define sf scanf
#define pf printf
#define pb push_back
#define MP make_pair
#define fr first
#define sc second
#define ll long long
#define dd double
#define all(v) v.begin(), v.end()
#define PI acos(-1.0)
#define mem(ara, value) memset(ara, value, sizeof(ara))
#define paii pair<int, int>
#define pall pair<ll, ll>
#define SZ(a) int(a.size())
#define read(nm) freopen(nm, "r", stdin)
#define write(nm) freopen(nm, "w", stdout)

#define take(args...) asdf,args
#define dump(x) cerr<<#x<<" = "<<x<<endl
#define debug(args...) cerr,args; cerr<<endl;
using namespace std;


template<typename T>
ostream& operator<<(ostream& output, vector<T>&v)
{
    output<<"[ ";
    if(SZ(v))
    {
        output<<v[0];
    }
    FOR(i, 1, SZ(v))
    {
        output<<", "<<v[i];
    }
    output<<" ]";
    return output;
}

template<typename T1, typename T2>
ostream& operator<<(ostream& output, pair<T1, T2>&p)
{
    output<<"( "<<p.fr<<", "<<p.sc<<" )";
    return output;
}




template<typename T>
ostream& operator,(ostream& output, T x)
{
    output<<x<<" ";
    return output;
}



struct ASDF{
    ASDF& operator,(int &a) {
        sf("%d", &a);
        return *this;
    }
    ASDF& operator,(long int &a){
        sf("%ld", &a);
        return *this;
    }
    ASDF& operator,(long long int &a){
        sf("%lld", &a);
        return *this;
    }
    ASDF& operator,(char &c){
        sf("%c", &c);
        return *this;
    }
    ASDF& operator,(double &d){
        sf("%lf", &d);
        return *this;
    }

    template<typename T>
    ASDF& operator,(T &a){
        cin>>a;
        return *this;
    }
}asdf;



//Header ends here


#define MAXX 100007

#define INF 16843009


int N, M;
int K;
ll ara[MAXX];

int L, R, C;

int sum[4*MAXX];

ll dp[507];


void update(int idx, int st, int ed, int i, int j, int val)
{
	if(st == i && ed == j)
	{
		sum[idx] = min(sum[idx], val);
		return;
	}


	int l = idx*2;
	int r = l + 1;
	int mid = (st + ed)/2;


	sum[l] = min(sum[l], sum[idx]);
	sum[r] = min(sum[r], sum[idx]);

	sum[idx] = INF;

	if(j <= mid)
	{
		update(l, st, mid, i, j, val);
	}
	else if(mid < i)
	{
		update(r, mid+1, ed, i, j, val);
	}
	else
	{
		update(l, st, mid, i, mid, val);
		update(r, mid+1, ed, mid+1, j, val);
	}
}



int get(int idx, int st, int ed, int pos)
{
	if(st == ed)
	{
		return sum[idx];
	}


	int l = idx*2;
	int r = l + 1;
	int mid = (st + ed)/2;

	if(sum[idx] != INF)
	{
		sum[l] = min(sum[l], sum[idx]);
		sum[r] = min(sum[r], sum[idx]);
		sum[idx] = INF;
	}


	if(pos <= mid)
	{
		return get(l, st, mid, pos);
	}
	else
	{
		return get(r, mid+1, ed, pos);
	}
}



void init()
{
	mem(sum, 1);

	mem(dp, 0);
}



int main()
{
	//init(); cout<<sum[0];
	int kases;


	take(kases);

	while(kases--)
	{
		init();
		take(N, K, M);

		ll s = 0;

		loop(i, N)
		{
			take(ara[i]);

			s += ara[i];
		}

		loop(i, M)
		{
			take(L, R, C);

			update(1, 0, N-1, L-1, R-1, C);
		}

		//cerr<<"DONE"<<endl;

		loop(i, N)
		{
			if(ara[i] < 0)
			{
				ll tmp = -ara[i];

				//dump(tmp);

				C = get(1, 0, N-1, i);

				//dump(C);

				for(int pos=K; pos>=0; pos--)
				{
					if(pos + C <= K)
					{
						dp[pos + C] = max(dp[pos + C], dp[pos] + tmp );
					}
				}
			}
		}


		cout<< s + dp[K]<<endl;
	}





}

(CodeForces) B. Modular Equations

0

If we need to find the number of divisors of a number n, we can do it in O(\sqrt n).

 

If there exist any divisor which is greater than \sqrt n, then there must be a divisor less than \sqrt n, such that this two numbers create number n by multiplication. So, we only need to loop from 1 to \sqrt n to find out all divisors.


// link: http://codeforces.com/contest/495/problem/B
/****************************************************************
   ▄█    █▄       ▄████████    ▄████████  ▄█  ▀█████████▄
  ███    ███     ███    ███   ███    ███ ███    ███    ███
  ███    ███     ███    ███   ███    █▀  ███   ███    ███
 ▄███▄▄▄▄███▄▄   ███    ███   ███        ███  ▄███▄▄▄██▀
▀▀███▀▀▀▀███▀  ▀███████████ ▀███████████ ███ ▀▀███▀▀▀██▄
  ███    ███     ███    ███          ███ ███    ███    ██▄
  ███    ███     ███    ███    ▄█    ███ ███    ███    ███
  ███    █▀      ███    █▀   ▄████████▀  █▀   ▄█████████▀
****************************************************************/



#include<bits/stdc++.h>


#define FOR(i, s, e) for(int i=s; i<e; i++)
#define loop(i, n) FOR(i, 0, n)
#define sf scanf
#define pf printf
#define pb push_back
#define MP make_pair
#define fr first
#define sc second
#define ll long long
#define dd double
#define all(v) v.begin(), v.end()
#define PI acos(-1.0)
#define mem(ara, value) memset(ara, value, sizeof(ara))
#define paii pair<int, int>
#define pall pair<ll, ll>
#define SZ(a) int(a.size())
#define read(nm) freopen(nm, "r", stdin)
#define write(nm) freopen(nm, "w", stdout)

#define dump(x) cerr<<#x<<" = "<<x<<endl
#define debug(args...) cerr,args; cerr<<endl;
using namespace std;


template<typename T>
ostream& operator<<(ostream& output, vector<T>&v)
{
    output<<"[ ";
    if(SZ(v))
    {
        output<<v[0];
    }
    FOR(i, 1, SZ(v))
    {
        output<<", "<<v[i];
    }
    output<<" ]";
    return output;
}

template<typename T1, typename T2>
ostream& operator<<(ostream& output, pair<T1, T2>&p)
{
    output<<"( "<<p.fr<<", "<<p.sc<<" )";
    return output;
}




template<typename T>
ostream& operator,(ostream& output, T x)
{
    output<<x<<" ";
    return output;
}





//Header ends here



int countDivisor(ll num, ll greaterThan)
{
    ll root = sqrt(num);
    //dump(root);

    int cnt = 0;

    for(ll i = 1; i < root; i++)
    {
        if((num%i) == 0)
        {
            //dump(i);
            if(i > greaterThan)
                cnt ++;

            if((num/i) > greaterThan)
                cnt++;
        }
    }

    ll componentOfRoot = num/root;

    if((num%root) == 0 && root > greaterThan)
        cnt++;

    if(root != componentOfRoot && (num%componentOfRoot) == 0 && componentOfRoot > greaterThan)
        cnt++;



    return cnt;






}

int main()
{
    ll a, b;

    cin>>a>>b;

    if(a == b)
    {
        cout<<"infinity"<<endl;
    }
    else if(a < b)
    {
        cout<<0<<endl;
    }
    else
    {
        cout<<countDivisor(a-b, b)<<endl;
    }





}

(lightOj)1007 – Mathematically Hard

0
// link: http://lightoj.com/volume_showproblem.php?problem=1007

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>

#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>


#define FOR(i, s, e) for(int i=s; i<e; i++)
#define loop(i, n) FOR(i, 0, n)
#define sf scanf
#define pf printf
#define pb push_back
#define MP make_pair
#define fr first
#define sc second
#define ll long long
#define dd double
#define all(v) v.begin(), v.end()
#define PI acos(-1.0)
#define mem(ara, value) memset(ara, value, sizeof(ara))
#define paii pair<int, int>
#define pall pair<ll, ll>
#define SZ(a) int(a.size())
#define read(nm) freopen(nm, "r", stdin)
#define write(nm) freopen(nm, "w", stdout)
#define dump(x) cout<<#x<<" = "<<x<<endl

using namespace std;

#define take(args...) asdf,args
#define debug(args...) asdfg,args; cout<<endl
struct ASDF{
    ASDF& operator,(int &a) {
        sf("%d", &a);
        return *this;
    }
    ASDF& operator,(long int &a){
        sf("%ld", &a);
        return *this;
    }
    ASDF& operator,(long long int &a){
        sf("%lld", &a);
        return *this;
    }
    ASDF& operator,(char &c){
        sf("%c", &c);
        return *this;
    }
    ASDF& operator,(double &d){
        sf("%lf", &d);
        return *this;
    }

    template<typename T>
    ASDF& operator,(T &a){
        cin>>a;
        return *this;
    }
}asdf;
struct ASDFG{
    template<typename T>
    ASDFG& operator,(vector<T> &v){
        pf("[");
        cout<<v[0];
        FOR(i, 1, SZ(v)){
            cout<<", "<<v[i];
        }
        pf("]");
        return *this;
    }

    template<typename T>
    ASDFG& operator,(T x) {
        cout<<x<<" ";
        return *this;
    }


}asdfg;



//Header ends here


#define MAXX 5000002

int phi[MAXX];
unsigned long long table[MAXX];

int main()
{
    for(int i=2; i<MAXX; i++)
    {
        if(phi[i] == 0)
        {
            phi[i] = i-1;

            for(int j=2*i; j<MAXX; j+=i)
            {
                if(phi[j] == 0)
                {
                    phi[j] = j;
                }
                phi[j] -= (phi[j]/i);
            }
        }
    }

    for(int i=2; i<MAXX; i++)
    {
        //table[i] = table[i-1] + (phi[i]*phi[i]); //overflow happes like shit

        table[i]=phi[i];
        table[i]*=phi[i];
        table[i]+=table[i-1];
    }




    int kases, kaseno = 0;
    scanf("%d", &kases);
    int a, b;


    while(kases--)
    {
        scanf("%d %d", &a, &b);
        pf("Case %d: %llu\n",++kaseno, table[b] - table[a-1] );
    }





    return 0;

}

(USACO) Humble Numbers

0
// link: http://cerberus.delos.com:790/usacoprob2?a=VKy6gbSa7gv&S=humble
/*
ID: himuhas1
TASK: humble
LANG: C++
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>

#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>


#define FOR(i, s, e) for(int i=s; i<e; i++)
#define loop(i, n) FOR(i, 0, n)
#define sf scanf
#define pf printf
#define pb push_back
#define MP make_pair
#define fr first
#define sc second
#define ll long long
#define dd double
#define all(v) v.begin(), v.end()
#define PI acos(-1.0)
#define mem(ara, value) memset(ara, value, sizeof(ara))
#define paii pair<int, int>
#define pall pair<ll, ll>
#define SZ(a) int(a.size())
#define read(nm) freopen(nm, "r", stdin)
#define write(nm) freopen(nm, "w", stdout)
#define dump(x) cout<<#x<<" = "<<x<<endl

using namespace std;

#define take(args...) asdf,args
#define debug(args...) asdfg,args; cout<<endl
struct ASDF{
    ASDF& operator,(int &a) {
        sf("%d", &a);
        return *this;
    }
    ASDF& operator,(long int &a){
        sf("%ld", &a);
        return *this;
    }
    ASDF& operator,(long long int &a){
        sf("%lld", &a);
        return *this;
    }
    ASDF& operator,(char &c){
        sf("%c", &c);
        return *this;
    }
    ASDF& operator,(double &d){
        sf("%lf", &d);
        return *this;
    }

    template<typename T>
    ASDF& operator,(T &a){
        cin>>a;
        return *this;
    }
}asdf;
struct ASDFG{
    template<typename T>
    ASDFG& operator,(vector<T> &v){
        pf("[");
        cout<<v[0];
        FOR(i, 1, SZ(v)){
            cout<<", "<<v[i];
        }
        pf("]");
        return *this;
    }

    template<typename T>
    ASDFG& operator,(T x) {
        cout<<x<<" ";
        return *this;
    }


}asdfg;



//Header ends here

#define MAX_PRIMES 102
#define MAX_N 100002
#define INF 4611686018427387904

int primes[MAX_PRIMES];
int hprime[MAX_PRIMES];
long long humble[MAX_N];


int main()
{
    #ifndef hasibpc
        read("humble.in");
        write("humble.out");
    #endif
    int cntPrime, N;
    take(cntPrime, N);
    long long temp;
    loop(i, cntPrime)
    {
        take(primes[i]);
    }
    mem(hprime, 0);

    humble[0] = 1;

    loop(i, N)
    {
        long long next = INF;

        loop(p, cntPrime)
        {
            while(primes[p]*humble[ hprime[p] ] <= humble[i])
            {
                hprime[p]++;
            }
            temp = primes[p]*humble[ hprime[p] ];
            if(temp < next)
            {
                next = temp;
            }
        }
        humble[i+1] = next;
    }

    cout<<humble[N]<<endl;


    return 0;
}


(USACO) Stamps

0

/*
ID: himuhas1
TASK: stamps
LANG: C++
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>

#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<sstream>
#include<set>


#define FOR(i, s, e) for(int i=s; i<e; i++)
#define loop(i, n) FOR(i, 0, n)
#define ll long long
#define pb push_back
#define MP make_pair
#define mem(a, v) memset(a,  v, sizeof(a))
#define fr first
#define sc second
#define all(v) v.begin(), v.end()
#define SZ(a) (int(a.size()))
#define INF (1<<29)
#define paii pair<int, int>
#define sf scanf
#define pf printf


using namespace std;


#define MAXX 2000002

#define dump(x) cout<<#x<<" = "<<x<<endl

int main()
{
    freopen("stamps.in", "r", stdin);
    freopen("stamps.out", "w", stdout);


    int n, k, values[52];

    sf("%d %d", &k, &n);

    loop(i, n)
    {
        sf("%d", &values[i]);
    }

    int dp[MAXX];
    mem(dp, 1);
    dp[0] = 0;
    int mxSum = 0;
    int tmp;

    loop(i, n)
    {
        for(int j=0; j<=mxSum; j++)
        {
            if(dp[j] != 16843009 && dp[j]+1 <=k)
            {
                tmp = j + values[i];
                dp[tmp] = min(dp[tmp], dp[j] + 1);
                mxSum = max(mxSum, tmp);
            }
        }
    }

    int res;
    for(res=0; res<=mxSum; res++)
    {
        if(dp[res] == 16843009 )
        {
            break;
        }
    }
    res--;

    cout<<res<<endl;




}


(USACO) Contact

0
/*
ID: himuhas1
TASK: contact
LANG: C++
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>

#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>


#define FOR(i, s, e) for(int i=s; i<e; i++)
#define loop(i, n) FOR(i, 0, n)
#define sf scanf
#define pf printf
#define pb push_back
#define MP make_pair
#define fr first
#define sc second
#define ll long long
#define dd double
#define all(v) v.begin(), v.end()
#define PI acos(-1.0)
#define mem(ara, value) memset(ara, value, sizeof(ara))
#define paii pair<int, int>
#define pall pair<ll, ll>
#define SZ(a) int(a.size())
#define read(nm) freopen(nm, "r", stdin)
#define write(nm) freopen(nm, "w", stdout)
#define dump(x) cout<<#x<<" = "<<x<<endl

using namespace std;

#define take(args...) asdf,args
#define debug(args...) asdfg,args; cout<<endl
struct ASDF{
    ASDF& operator,(int &a) {
        sf("%d", &a);
        return *this;
    }
    ASDF& operator,(long int &a){
        sf("%ld", &a);
        return *this;
    }
    ASDF& operator,(long long int &a){
        sf("%lld", &a);
        return *this;
    }
    ASDF& operator,(char &c){
        sf("%c", &c);
        return *this;
    }
    ASDF& operator,(double &d){
        sf("%lf", &d);
        return *this;
    }

    template<typename T>
    ASDF& operator,(T &a){
        cin>>a;
        return *this;
    }
}asdf;
struct ASDFG{
    template<typename T>
    ASDFG& operator,(vector<T> &v){
        pf("[");
        cout<<v[0];
        FOR(i, 1, SZ(v)){
            cout<<", "<<v[i];
        }
        pf("]");
        return *this;
    }

    template<typename T>
    ASDFG& operator,(T x) {
        cout<<x<<" ";
        return *this;
    }


}asdfg;



//Header ends here
#define MAXX 8192+2

void printBinary(int num)
{
    int len;
    for(int i=13; i>-1; i--)
    {
        if(num & (1<<i))
        {
            len = i;
            break;
        }
    }
    for(int i=len-1; i>-1; i--)
        if(num & (1<<i))
            pf("1");
        else
            pf("0");
}

int main()
{


    #ifndef henapc
        read("contact.in");
        write("contact.out");
    #endif
    int cnt[MAXX];
    mem(cnt, 0);
    int A, B, N;
    string str;
    string inp;
    take(A, B, N);
    char c;
    cin.ignore();
    while(true)
    {
        c = getchar();
        if(c == EOF) break;
        if(c != '\n')
            str.pb(c);
    }
    //debug("str", str, "str");
    B++;

    FOR(len, A, B)
    {
        int mask = 0;

        loop(i, len)
        {
            mask = mask*2 + (str[i] - '0');
        }
        mask = mask | (1<<len);

        //if(len == 2) dump(mask);


        cnt[mask]++;

        FOR(i, len, SZ(str))
        {
            //dump(i);
            mask = mask & ~(1<<len);
            mask = mask*2 + str[i]-'0';
            mask = mask & ~(1<<len);
            mask = mask | (1<<len);
            //if(len == 2) dump(mask);
            cnt[mask]++;
        }
        //dump(cnt[4][2]);
    }



    map<int, vector<int> >all;

    paii temp;

    loop(i, MAXX)
    {

            if(cnt[i])
            {
                all[cnt[i]].pb(i);
            }

        //debug("came here");
    }


    int counting = 0;
    for(map<int, vector<int> >::iterator it = all.end(); it != all.begin() && counting < N;)
    {
        it--;
        counting++;
        pf("%d\n", it->fr);

        vector<int> &v = it->sc;

        printBinary(v[0]);

        FOR(i, 1, SZ(v))
        {
            if(i%6 == 0) pf("\n");
            else pf(" ");
            printBinary(v[i]);
        }
        pf("\n");
    }





    return 0;
}

(USACO) Score Inflation

0
/*
ID: himuhas1
TASK: inflate
LANG: C++
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>

#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>


#define FOR(i, s, e) for(int i=s; i<e; i++)
#define loop(i, n) FOR(i, 0, n)
#define sf scanf
#define pf printf
#define pb push_back
#define MP make_pair
#define fr first
#define sc second
#define ll long long
#define dd double
#define all(v) v.begin(), v.end()
#define PI acos(-1.0)
#define mem(ara, value) memset(ara, value, sizeof(ara))
#define paii pair<int, int>
#define pall pair<ll, ll>
#define SZ(a) int(a.size())
#define read(nm) freopen(nm, "r", stdin)
#define write(nm) freopen(nm, "w", stdout)
#define dump(x) cout<<#x<<" = "<<x<<endl

using namespace std;

#define take(args...) asdf,args
#define debug(args...) asdfg,args; cout<<endl
struct ASDF{
    ASDF& operator,(int &a) {
        sf("%d", &a);
        return *this;
    }
    ASDF& operator,(long int &a){
        sf("%ld", &a);
        return *this;
    }
    ASDF& operator,(long long int &a){
        sf("%lld", &a);
        return *this;
    }
    ASDF& operator,(char &c){
        sf("%c", &c);
        return *this;
    }
    ASDF& operator,(double &d){
        sf("%lf", &d);
        return *this;
    }

    template<typename T>
    ASDF& operator,(T &a){
        cin>>a;
        return *this;
    }
}asdf;
struct ASDFG{
    template<typename T>
    ASDFG& operator,(vector<T> &v){
        pf("[");
        cout<<v[0];
        FOR(i, 1, SZ(v)){
            cout<<", "<<v[i];
        }
        pf("]");
        return *this;
    }

    template<typename T>
    ASDFG& operator,(T x) {
        cout<<x<<" ";
        return *this;
    }


}asdfg;



//Header ends here
#define MAXX 10002
ll best[MAXX];

int main()
{
#ifndef henapc
    read("inflate.in");
    write("inflate.out");
#endif // henapc

    int given, cntGroups;
    int points, minitus;
    take(given, cntGroups);
    given++;

    loop(i, cntGroups)
    {
        take(points, minitus);
        FOR(i, minitus, given)
        {
            best[i] = max(best[i], best[i-minitus]+points);
        }
    }
    cout<<best[given-1]<<endl;


    return 0;
}

(USACO) Longest Prefix

0
// link: http://cerberus.delos.com:790/usacoprob2?a=QX6ufTaKJ4D&S=prefix

/*
ID: himuhas1
TASK: prefix
LANG: C++
*/

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<stdio.h>

#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<utility>

#define FOR(i, s, e) for(int i=s; i<e; i++)
#define loop(i, n) FOR(i, 0, n)
#define gi(a) sf("%d", &a)
#define gi2(a, b) sf("%d%d", &a, &b)
#define gi3(a, b, c) sf("%d%d%d", &a, &b, &c)
#define gi4(a, b, c, d) sf("%d%d%d%d", &a, &b, &c, &d)
#define all(v) v.begin(), v.end()
#define pb push_back
#define sf scanf
#define pf printf
#define mem(a, v) memset(a, v, sizeof(a))
#define dd double
#define ll long long
#define paii pair<int, int>
#define pall pair<ll, ll>
#define SZ(a) int(a.size())

using namespace std;


#define NO_CHILD -2
#define NO_MAP -1

#define MAXX 200002

struct Trie{
    map<int, map<int, int> > childNode;
    vector<bool>allNodes;
    int id;
    int *p;
    int zero;


    Trie()
    {
        allNodes.pb(0);
        zero = 0;
    }


    void add(const string &val)
    {
        p = &zero;
        int ch;
        loop(i, SZ(val))
        {
            ch = val[i];
            p = &childNode[*p][ch];
            if(*p == 0)
            {
                *p = SZ(allNodes);
                allNodes.pb(0);
            }
        }

        allNodes[*p] = 1;
    }

    void iniCharByCharSearch()
    {
        id = 0;
    }

    int charByCharSearch(char ch)
    {
        if(childNode[id].find(ch) == childNode[id].end())
        {
            return NO_CHILD;
        }
        else
        {
            id = childNode[id][ch];
            return allNodes[id];
        }
    }
};






int main()
{
    #ifndef hasibpc
        freopen("prefix.in", "r", stdin);
        freopen("prefix.out", "w", stdout);
    #endif // hasibpc

    Trie tr;

    string name;
    string str;
    while(1)
    {
        cin>>name;
        if(name[0] == '.') break;

        tr.add(name);
    }

    while(cin>>name)
    {
        str += name;
    }

    bool dp[MAXX];
    mem(dp, 0);
    int ret;
    int result = 0;
    //dp[0] = 1;
    for(int i=-1; i<SZ(str); i++)
    {
        if(i==-1 || dp[i])
        {
            result = max(result, i+1);
            tr.iniCharByCharSearch();
            FOR(j, i+1, SZ(str))
            {
                ret = tr.charByCharSearch(str[j]);

                //pf("%d %d ==> %d\n", i, j, ret);

                if(ret == 1)
                {
                    //cout<<"j = "<< j<<endl;

                    dp[j] = 1;
                    //result = max(result, j+1);
                }


                if(ret == NO_CHILD)
                    break;

            }
        }
    }

    cout<<result<<endl;




    return 0;
}