UVa : 11401 (Triangle Counting)



// http://uva.onlinejudge.org/external/114/11401.html
// Runtime: 0.048s
// Tag: Geometry, Accumulative Sum, math, counting 

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>

#define INF 2147483647
#define pi acos(-1.0)
#define N 1000000
#define LL long long

#define For(i, a, b) for( int i = (a); i < (b); i++ )
#define Fors(i, sz) for( size_t i = 0; i < sz.size (); i++ )
#define Fore(it, x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++)
#define Set(a, s) memset(a, s, sizeof (a))
#define Dump(x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++) cout << *it << " "; cout << endl;

using namespace std;

LL oddAccuSum [N + 5];
LL evenAccuSum [N + 5];
LL output [N + 5];

void preCalc ()
{
	oddAccuSum [1] = 1;
	evenAccuSum [1] = 0;
	LL numOdd = 3;
	LL numEven = 2;

	for ( int i = 2; i < N + 5; i++ ) {
		oddAccuSum [i] = oddAccuSum [i - 1] + numOdd;
		numOdd += 2;
		evenAccuSum [i] = evenAccuSum [i - 1] + numEven;
		numEven += 2;
	}
}

void preGen ()
{
	output [3] = 0;
	output [4] = 1;

	for ( int i = 5; i < N + 5; i++ ) {
		int child = (i - 1) - (i / 2);
		output [i] = i % 2 ? evenAccuSum [child] : oddAccuSum [child];
	}

	for ( int i = 1; i < N + 5; i++ ) output [i] += output [i - 1];
}

int main ()
{

	preCalc ();
	preGen ();

	int n;

	while ( scanf ("%d", &n) && n >= 3 ) {
		printf ("%lld\n", output [n]);
	}

	return 0;
}

// @END_OF_SOURCE_CODE
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s