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;
}

No comments:

Post a Comment