Friday, 20 February 2015

Codeforces Round #292 (Div. 1) Drazil and Factorial

Problem: Drazil and Factorial

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

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

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010


int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int tc,kk=1, n, fact[12];
    cin>>n;
    fact[0]=fact[1]=1;
    for(int i=2; i<10; i++)
        fact[i]=fact[i-1]*i;
   
    string s;
    cin>>s;
    int num=1;
    vector<int>v;
    for(int i=0; i<n; i++)
    {
        num=(fact[ s[i]-'0' ] );
        
        while(num%7==0)
        {
            num/=fact[7];
            v.push_back(7);
        }
        while(num%5==0)
        {
            num/=fact[5];
            v.push_back(5);
        }
        while(num%3==0)
        {
            num/=fact[3];        
            v.push_back(3);
        }
        while(num%2==0)
        {
            num/=fact[2];           
            v.push_back(2);
        }
    }

    sort(v.begin(), v.end());
    for(int i=v.size()-1;i>=0;i--)
        cout<<v[i];
    return 0;
}

Codeforces Round #276 (Div. 1) Kindergarten

Problem: Kindergarten

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

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

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 1000010

long long arr[MX];

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc,kk=1, n;
    long long inc=0, dec=0,tmp=0;

    cin>>n;

    for(int i=0;i<n;i++) cin>>arr[i];

    for(int i=1;i<n;i++)
    {
        if(arr[i]>arr[i-1])
        {
            tmp=inc;
            inc=max(dec, inc+arr[i]-arr[i-1]);
            dec=max(tmp, dec);
        }
        else
        {
            tmp=dec;
            dec=max(inc, dec+arr[i-1]-arr[i]);
            inc=max(inc, tmp);
        }
    }
    cout<<max(inc, dec);
    return 0;
}

Codeforces Round #172 (Div. 2) Maximum Xor Secondary

Problem: Maximum Xor Secondary

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

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

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

long long arr[MX];
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc,kk=1, n;
    cin>>n;
    for(int i=0;i<n;i++) cin>>arr[i];
    //forward increasing.
    stack<long long>fi;
    fi.push(arr[0]);
    fi.push(arr[1]);
    long long mx=arr[0]^arr[1];
    for(int i=2;i<n;i++)
    {
        while(!fi.empty() && fi.top()<arr[i])
            fi.pop();
        fi.push(arr[i]);
        long long last=fi.top();
        fi.pop();
        if(!fi.empty())
        {
            long long prev=fi.top();
            mx=max(mx, prev^last);
        }
        fi.push(last);
    }
    //backward increasing
    stack<long long>bi;
    bi.push(arr[n-1]);
    bi.push(arr[n-2]);
    mx=max(mx, arr[n-1]^arr[n-2]);
    for(int i=n-3;i>=0;i--)
    {
        while(!bi.empty() && bi.top()<arr[i])
            bi.pop();
        bi.push(arr[i]);
        long long last=bi.top();
        bi.pop();
        if(!bi.empty())
        {
            long long prev=bi.top();
            mx=max(mx, prev^last);
        }
        bi.push(last);
    }

    cout<<mx;
    return 0;
}

Codeforces Round #277 (Div. 2) Valid Sets

Problem: Valid Sets

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

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

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

vector<int>adj[2005];
long long arr[2005], dp[2005];
int n, d, now;
bool vis[2005];

long long func(int u)
{
    vis[u]=1;
    long long &ret=dp[u];
    if(ret!=-1) return ret;
    ret=1;
    for(int i=0;i<adj[u].size();i++)
    {
        int v=adj[u][i];
        if(!vis[v])
            if(arr[now]<=arr[v] && arr[now]+d>=arr[v])
                if(!(arr[now]==arr[v] && now<v))
                    ret=(ret*(func(v)+1))%MOD;
    }
return ret;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc,kk=1, x, y;
    cin>>d>>n;
    for(int i=1;i<=n;i++) cin>>arr[i];
    for(int i=1;i<n;i++)
    {
        cin>>x>>y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    long long ans=0;
    for(int i=1;i<=n;i++)
    {
        CLR(vis);
        SET(dp);
        now=i;
        ans=(ans+func(i))%MOD;
    }
    cout<<ans;
    return 0;
}

Monday, 16 February 2015

UVA 12075 Counting Triangles, UVALive 3295, Regionals 2005 >> Asia - Dhaka

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

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

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

long long sameLine[1005][1005];

long long nc3(long long n)
{
    if(n<3) return 0;
    return (n*(n-1)*(n-2))/6;
}

void precal()
{
    //distinct
    for(int i=2;i<1003;i++)
        for(int j=2;j<1003;j++)
            sameLine[i][j]=sameLine[i-1][j]+sameLine[i][j-1]-sameLine[i-1][j-1]+__gcd(i, j)-1;
    //sum-up
    for(int i=2;i<1003;i++)
        for(int j=2;j<1003;j++)
            sameLine[i][j]+=sameLine[i-1][j]+sameLine[i][j-1]-sameLine[i-1][j-1];
return;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    precal();
    int kk=1, tc;
    long long n, m;
    string s;
    while(cin>>n>>m && n)
    {
        n++;
        m++;
        long long ans = nc3(n*m);
        //row-col
        ans-=nc3(n)*m;
        ans-=nc3(m)*n;
        //diagonal
        ans-= (sameLine[n-1][m-1]*2);
        cout<<"Case "<<kk++<<": "<< ans <<"\n";
    }

    return 0;
}