#include #include using namespace std; vector array(unsigned a,unsigned b,unsigned c,unsigned d) { vector result(4); result[0]=a;result[1]=b;result[2]=c;result[3]=d; return result; } struct elem { int integer; /* Number this is multiplied by */ unsigned exponent; /* 0=constant, 1=x, 2=xx, 3=xxx, ... */ public: elem() : integer(0), exponent(0) { } elem(int add, unsigned exp) : integer(add), exponent(exp) { } }; static bool TestGuess( const vector &keys, const vector &values, const vector &guess) { bool wasok = true; /* Test the guess */ for(unsigned a=0; a &mat, unsigned depf, bool sub=false) { if(!sub) for(unsigned a=0; a tmp(subdepf * subdepf); for(unsigned b=0; b=a))%depf]; int subdet = mat[a] * calcdet(tmp, subdepf, true); if(a & 1) sum -= subdet; else sum += subdet; } } if(!sub) { fprintf(stderr, " -> det: %d\n", sum); } fflush(stderr); fflush(stdout); return sum; } static void Guess( const vector &keys, const vector &values) { if(keys.size() != values.size()) { fprintf(stderr, "Error: keys.size() != values.size()\n"); return; } for(unsigned elemcount=1; elemcount<=keys.size(); ++elemcount) { vector mat (elemcount * elemcount); for(unsigned a=0; a guess(elemcount); for(unsigned a = 0; a < elemcount; ++a) { vector mat2 = mat; for(unsigned c = 0; c < elemcount; ++c) mat2[c*elemcount+a] = values[c]; guess[elemcount-1-a].integer = calcdet(mat2, elemcount, true) / det; guess[elemcount-1-a].exponent = a; } if(TestGuess(keys, values, guess)) return; } fprintf(stderr, "Not found!\n"); fflush(stderr); } int main(void) { /* Should output: x + 4 */ Guess( array(0,1,2,3), /* x*0 + y*1 = 4 x=(4*1-0*5) / (1-0)=4 */ array(4,5,6,7) ); /* x*1 + y*1 = 5 y=(5 - 4) / (1-0)=1 */ /* Should output: 2x */ Guess( array(10,11,12,13), /* x*10 + y*1 = 20 x=(20*11-22*10) / (11-10)=0 */ array(20,22,24,26) ); /* x*11 + y*1 = 22 y=(22 -20) / (11-10)=2 */ /* Should output: 3x + 1 */ Guess( array(1,2,3,4), array(4,7,10,13) ); /* Should output: xx - 1 */ Guess( array(1,2,3,4), array(0,3,8,15) ); /* Should output: xx - 2x +20 */ Guess( array( 1, 2, 3, 4), /* x*1 + y*1 + z*1 = 19 x = 1*(2-3)+1(-9blah */ array(19,20,23,28) ); /* x*4 + y*2 + z*1 = 20 */ /* x*9 + y*3 + z*1 = 23 */ return 0; }