Artifact Content
Not logged in

Artifact 9d8dcb43599a380c10dc71da02fc52a4708f07c9


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;

class Multifactorial
{
public:
	string calcMultiFact(int n, int k) 
	{
		LL ans = 1;
		for(LL v=n; v>0; v-=k)
		{
			if( ans*v<ans || ans*v/v!=ans || ans*v>1000000000000000000LL )
				return "overflow";
			ans *= v;
		}
		stringstream sout;
		sout << ans;
		return sout.str();
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }

template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const string &Expected, const string &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	int n = 14; 
	int k = 3; 
	string RetVal = "12320"; 
	return verify_case(RetVal, Multifactorial().calcMultiFact(n, k)); }
int Test_(Case_<1>) {
	int n = 5; 
	int k = 4; 
	string RetVal = "5"; 
	return verify_case(RetVal, Multifactorial().calcMultiFact(n, k)); }
int Test_(Case_<2>) {
	int n = 1000; 
	int k = 2; 
	string RetVal = "overflow"; 
	return verify_case(RetVal, Multifactorial().calcMultiFact(n, k)); }
int Test_(Case_<3>) {
	int n = 2000000000; 
	int k = 1900000000; 
	string RetVal = "200000000000000000"; 
	return verify_case(RetVal, Multifactorial().calcMultiFact(n, k)); }
int Test_(Case_<4>) {
	int n = 1000; 
	int k = 256; 
	string RetVal = "84232704000"; 
	return verify_case(RetVal, Multifactorial().calcMultiFact(n, k)); }
int Test_(Case_<5>) {
	int n = 2000000000; 
	int k = 1; 
	string RetVal = "overflow"; 
	return verify_case(RetVal, Multifactorial().calcMultiFact(n, k)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE