Cod sursa(job #938507)

Utilizator veleanduAlex Velea veleandu Data 12 aprilie 2013 19:25:17
Problema Ratphu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

ifstream in("ratphu.in");
ofstream out("ratphu.out");

#define FORIT( it, v ) for( typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it )

const int max_n=(1<<18);

long long Best[20][max_n];
int El[20], M[305];

int Mod, i, e, r, past;
long long n;

int main(){
	in>>n>>Mod;
	do{
		El[i++]=n%10;
		n/=10;
	}while(n);
	n=i;

	for( i=1; i<=250; ++i )
		M[i]=i%Mod;
	
	for( i=0; i<n; ++i ){
		Best[ El[i]%Mod ][ 1<<i ] = 1;
	}
	for( i=1; i<(1<<n); ++i ){
		if( i & (i-1) )
			;
		else
			continue;
		for( e=0; e<n; ++e ){
			if( i & ( 1<<e ) ){
				past = i^(1<<e);
				// am selectat elementul e :)
				// mergem inapoi de la starea "past"
				for( r=0; r<Mod; ++r )
					Best[ M[ r*10 + El[e] ] ][i] += Best[r][past];
			}
		}
	}
	out<< Best[0][(1<<n)-1];
	return 0;
}