Problem:
Score : 400 points

Problem Statement

Given are integers A, B, and N.

Find the maximum possible value of floor(Ax/B) - A × floor(x/B) for a non-negative integer x not greater than N.

Here floor(t) denotes the greatest integer not greater than the real number t.

Constraints

1 ≤ A ≤ 10^{6}

1 ≤ B ≤ 10^{12}

1 ≤ N ≤ 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B N

Output

Print the maximum possible value of floor(Ax/B) - A × floor(x/B) for a non-negative integer x not greater than N, as an integer.

Sample Input 1

5 7 4

Sample Output 1

2

When x=3, floor(Ax/B)-A×floor(x/B) = floor(15/7) - 5×floor(3/7) = 2. This is the maximum value possible.

Sample Input 2

11 10 9

Sample Output 2

9

Solution in C (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <math.h>
#include <complex.h>

void SolveCubicEquation(double complex x[3],double a,double b,double c,double d);
void SolveQuarticEquation(double complex x[4],double a,double b,double c,double d,double e);
double Cuberoot(double x);
double complex Squreroot(double complex x);

int ret[10000];
int asc(const void *a, const void *b) {
	return *(int *)a - *(int *)b;
}
void divisors(int n) {
    for(int i=0;i<10000;++i)ret[i]=1000000001;
	ret[0]=1;ret[1]=n;
	int c=2;
	for(int i = 2; i*i<=n; ++i) {
		if(n%i==0) {
			ret[c++]=i;
			if(i*i!=n)ret[c++]=n/i;
		}
	}
	qsort(ret,sizeof(ret)/sizeof(int),sizeof(int),asc);
	return;
}

int main(void){
    int n,c=0;scanf("%d",&n);
    divisors(n);
    //while(ret[c]!=1000000001)printf("%d\n",ret[c++]);
    double complex y[4];
    while(ret[c]!=1000000001){
      int s=ret[c];//printf("%d\n",s);
      SolveQuarticEquation(y,5.0,-10.0*s,10.0*s*s,-5.0*s*s*s,1.0*s*s*s*s-n/s);//x^4+2x^3+3x^2+4x+5=0を解く,結果はyに格納
      for(int i=0;i<4;++i){for(int j=-2;j<3;++j){
        int cand=creal(y[i]);//printf("%lf\n",creal(y[i]));
        int a=cand+j,b=cand+j-s;
        int t=5.0*a*a*a*a-10.0*s*a*a*a+10.0*s*s*a*a-5.0*s*s*s*a+1.0*s*s*s*s;
        if(t>0 && -0.00000001<t-n/s && t-n/s<0.000000001){
          printf("%d %d\n",a,b);
          return 0;
        }
      }}
      s=-ret[c];
      for(int i=0;i<4;++i){for(int j=-2;j<3;++j){
        int cand=creal(y[i]);//printf("%lf\n",creal(y[i]));
        int a=cand+j,b=cand+j-s;
        int t=5.0*a*a*a*a-10.0*s*a*a*a+10.0*s*s*a*a-5.0*s*s*s*a+1.0*s*s*s*s;
        if(t>0 && -0.00000001<t-n/s && t-n/s<0.000000001){
          printf("%d %d\n",a,b);
          return 0;
        }
      }}
      c++;
    }
    
    return 0;
}

void SolveCubicEquation(double complex x[3],double a,double b,double c,double d){
    if(a == 0.0){
        printf("Error:a = 0.0\n");
        printf("This equation is NOT Cubic.\n");
    }
    double A,B,C,p,q,D;
    A = b/a;
    B = c/a;
    C = d/a;
    p = B-A*A/3.0;
    q = 2.0*A*A*A/27.0-A*B/3.0+C;
    D = q*q/4.0+p*p*p/27.0;
    if(D < 0.0){//three real solutions
        double theta = atan2(sqrt(-D),-q*0.5);
        x[0] = 2.0*sqrt(-p/3.0)*cos(theta/3.0)-A/3.0;
        x[1] = 2.0*sqrt(-p/3.0)*cos((theta+2.0*M_PI)/3.0)-A/3.0;
        x[2] = 2.0*sqrt(-p/3.0)*cos((theta+4.0*M_PI)/3.0)-A/3.0;
    }else{//single real solution and two imaginary solutions(c.c)
        double u = Cuberoot(-q*0.5+sqrt(D)),v = Cuberoot(-q*0.5-sqrt(D));
        x[0] = u+v-A/3.0;
        x[1] = -0.5*(u+v)+sqrt(3.0)*0.5*1i*(u-v)-A/3.0;
        x[2] = -0.5*(u+v)-sqrt(3.0)*0.5*1i*(u-v)-A/3.0;
    }
}

void SolveQuarticEquation(double complex x[4],double a,double b,double c,double d,double e){
    if(a == 0.0){
        printf("Error:a = 0.0\n");
        printf("This equation is NOT Quartic.\n");
    }
    double A,B,C,D,p,q,r,epsilon_temp = 0.000001;
    A = b/a;
    B = c/a;
    C = d/a;
    D = e/a;
    p = -6.0*pow(A/4.0,2.0)+B;
    q = 8.0*pow(A/4.0,3.0)-2.0*B*A/4.0+C;
    r = -3.0*pow(A/4.0,4.0)+B*pow(A/4.0,2.0)-C*A/4.0+D;
    double complex t_temp[3];
    SolveCubicEquation(t_temp,1.0,-p,-4.0*r,4.0*p*r-q*q);
    double t = creal(t_temp[0]);
    double complex m = Squreroot(t-p);
    x[0] = (-m+Squreroot(-t-p+2.0*q/m))*0.5-A/4.0;
    x[1] = (-m-Squreroot(-t-p+2.0*q/m))*0.5-A/4.0;
    x[2] = (m+Squreroot(-t-p-2.0*q/m))*0.5-A/4.0;
    x[3] = (m-Squreroot(-t-p-2.0*q/m))*0.5-A/4.0;
}



double Cuberoot(double x){
    if(x > 0.0){
        return pow(x,1.0/3.0);
    }else{
        return -pow(-x,1.0/3.0);
    }
}

double complex Squreroot(double complex x){
    double complex y;
    double r = sqrt(creal(x)*creal(x)+cimag(x)*cimag(x)),theta = atan2(cimag(x),creal(x));
    if(cimag(x) == 0.0){
        if(creal(x) > 0.0){
            y = sqrt(r);
        }else{
            y = sqrt(r)*1i;
        }
    }else{
        if(theta < 0.0){
            theta += 2.0*M_PI;
        }
        double complex y = sqrt(r)*(cos(theta*0.5)+1i*sin(theta*0.5));
    }
    return y;
}