USACO 1.2.5 – Dual Palindromes


#include <iostream>
#include <fstream>
#include <string>


using namespace std;

int isp(string s) {
	int i = -1, j = s.length();
	while (i < j && s[++i] == s[--j]);
	return i >= j;
}

string itoa(int val, int base) {
	string buf;
	do {
		buf += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[ val % base];
		val /= base;
	} while ( val );
	return string(buf.rbegin(), buf.rend());
}

int main() {
	ofstream fout("dualpal.out");
	ifstream fin("dualpal.in");

	int N, S;
	fin >> N >> S;
	for (int i = S+1; N; i++)
		for(int b=2, dual=0, p=0; b<11; b++)
			if ( (dual+=isp(itoa(i,  b))) && dual==2 && !(p++) && (N-- +1))
				fout << i << endl;
	return 0;
}

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

%d bloggers like this: