My Weblog

Blog about programming and math

Project Euler Problem 303

For Project Euler problem 303 ,a simple brute force is enough to kill but DP can be used improve the run time which many solvers did. This problem has nice pattern about 9,99,999 , 9999 which makes brute force to killed by memory [Mine memory was 768MB ] limit exceed. For 9 the number is 12222 ,99——1122222222 , 999 ———111222222222222 and 9999 ———–11112222222222222222. Approximately 5 minutes on my laptop.

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include<gmpxx.h>
typedef long long _int;
using namespace std;
struct state
	{
		_int n;
		/*bool operator<(state s) const 
		{
			return this->n > s.n;
		}*/
	};
int main()
	{
		
		_int sum=0;
		for(int i=1;i<=10000;i++)
		{
		
			if(i==9) { sum+=_int(1358LL);continue;}
			if(i==99){sum+=_int(11335578LL);continue;}
			if(i==999){ sum+=_int(111333555778LL);continue;}
			if(i==9999){sum+=_int(1111333355557778LL);continue;}	
			queue<state> Q;
			Q.push((state){1});
			Q.push((state){2});
			bool t=true;
			while(t)
			{
				state tmp=Q.front();Q.pop();
				if(tmp.n%i==0) {/*cout<<i<<" "<<tmp.n<<" "<<tmp.n/i<<endl;*/t=false;sum+=(tmp.n/i);}
				for(int i=0;i<=2;i++) Q.push((state){10*tmp.n+i });

			}
		}
		cout<<endl<<sum<<endl;
	}
Advertisements

October 30, 2010 - Posted by | Programming

No comments yet.

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

%d bloggers like this: