Jump to content
thoga31

A unidade da soma de duas razões

Recommended Posts

mogers

Sabendo A, B e C, só há um D possível. Basta saber se esse D contém os digitos que faltam usar.

A/B + C/D = 1

C/D = 1 - A/B

C/D = (B-A)/B

D = B*C / (B-A)

migueloliveira$ g++ -Wall -O2 a.cpp -o a && time ./a

real 0m0.108s

Esta deve ser a minha versão final para o desafio.

// A/B + C/D = 1
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
#define two(n) (1<<(n))

vector<unsigned int> numerosDisponiveis[1024];
char used[10];
char numerosRestantes[1024][10000];
int numMask[10000];

void geraNumeros(int mask) {
   int i , j , k, p, num;
   for (i = 0 ; i < 10 ; i++)
used[i] = (mask & two(i)) != 0;
   for (i = 1 ; i < 10 ; i++)
if (!used[i]) {
    used[i] = 1;
    numMask[i] = two(i);
    numerosDisponiveis[mask].push_back(i);
    for (j = 0; j < 10 ; j++)
	if (!used[j]) {
	    used[j] = 1;
	    num = i*10 + j;
	    numMask[num] = two(i) | two(j);
	    numerosDisponiveis[mask].push_back(num);
	    for (k = 0; k < 10; k++)
		if (!used[k]) {
		    used[k] = 1;
		    num = i*100 +j*10 + k;
		    numMask[num] = two(i) | two(j) | two(k);
		    numerosDisponiveis[mask].push_back(num);
		    for (p = 0; p < 10; p++)
			if (!used[p]) {
			    num = i*1000 +j*100 + k*10 + p;
			    numMask[num] = two(i) | two(j) | two(k) | two(p);
			    numerosDisponiveis[mask].push_back(num);
			}
		    used[k] = 0;
		}
	    used[j] = 0;
	}
    used[i] = 0;
}
}

int main() {
   unsigned int k, i, A , B , C , D, BC, BA, mask_a, mask_ab, target = two(10)-1;
   vector<unsigned int>::iterator it;
   for (i = 0 ; i < two(10); i++) { // 2^10
geraNumeros(i);
sort(numerosDisponiveis[i].begin(), numerosDisponiveis[i].end());
   }
   for (i = 0 ; i < numerosDisponiveis[0].size(); i++) {
A = numerosDisponiveis[0][i];
numerosRestantes[numMask[A]][A] = 1;
   }
   for (i = 0 ; i < numerosDisponiveis[0].size(); i++) {
A = numerosDisponiveis[0][i];
mask_a = numMask[A];
it = upper_bound(numerosDisponiveis[mask_a].begin(), numerosDisponiveis[mask_a].end(), A);
for (; it != numerosDisponiveis[mask_a].end(); it++) {
    B = *it;
    BA = B - A;
    mask_ab = mask_a | numMask[b];
    for (k = 0; k < numerosDisponiveis[mask_ab].size() && A > numerosDisponiveis[mask_ab][k]; k++) {
	C = numerosDisponiveis[mask_ab][k];
	// A/B + C/D = 1 <=> C/D = 1 - A/B <=> C/D = (B-A)/B <=> D = B*C / (B-A)
	BC = B * C;
	if (BC % BA == 0) {
	    D = BC / BA;
	    if (D < 10000 && numerosRestantes[target ^ (mask_ab | numMask[C])][D]) {
		printf("%u/%u + %u/%u = 1\n", A, B, C, D);
	    }
	}
    }
}
   }
   return 0;
}

Edited by mogers

"What we do for ourselves dies with us. What we do for others and the world, remains and is immortal.", Albert Pine

Blog pessoal : contém alguns puzzles, algoritmos e problemas para se resolver com programação.

Share this post


Link to post
Share on other sites
thoga31

edit: ao pre-visualizar o post o codigo aparece bem indentado mas depois de guardar fica desformatado outra vez...

Usa o editor simples e não o WYSIWYG. Suponho que seja disso.


Knowledge is free!

Share this post


Link to post
Share on other sites
mogers

Eu tinha tentado ambos. Usei outro editor de texto para fazer o copy paste para o editor simples e já funcionou.


"What we do for ourselves dies with us. What we do for others and the world, remains and is immortal.", Albert Pine

Blog pessoal : contém alguns puzzles, algoritmos e problemas para se resolver com programação.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.