ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1071. Nikifor 2

optimize for constant factors too
Posted by Marcin Mika 14 Feb 2003 06:32
#include <stdio.h>
#include <vector>

using namespace std;

int nice( int x, int y, int b ){
int k;

while ( y ){
 k=y%b;
  while ( x && (x%b) != k ) x/=b;
  if ( !x ) return 0;
 x/=b;
 y/=b;
}

return 1;
}

int main( void ){
//FILE *in=fopen( "input.txt", "r" );
int i,j,k,b;
int n,x,y,lim;

scanf( "%d %d", &x, &y );
lim=x+1;

for ( b=2; b<=lim; b++ )
 if ( nice(x,y,b) ) break;

if ( b<=lim ){
printf( "%d\n", b );
}

else{
printf( "No solution\n" );
}

return 0;
}