Informatică, întrebare adresată de rRobert5, 8 ani în urmă

problema #2144 diofantic de pe pbinfo

asta e una din solutiile mele de 45p(am 9:') )

int diofantic(int n,int s[],int a,int b,int c){
int cnt=0,i=1,j=n,x;
while(i<=n&&j>=1){
if(a*s[i]*s[i]+b*s[j]*s[j]==c){
if(s[i]!=s[j])
cnt++;
i++;
}
if(a*s[i]*s[i]+b*s[j]*s[j] c)
j--;
}
return cnt;
}

Răspunsuri la întrebare

Răspuns de lucaciucandrei
3

int diofantic(int n, int s[], int a, int b, int c) {

   int nr = 0;

   for(int i=1; i<=n; ++i){

       long long x = 1LL * a * s[i] * s[i];

       long long y = c - x;

       if ( y % b == 0 && y >= 0 ) {

           y = y / b;

           int z = sqrt(y);

           if (z*z == y) {

               int st = 1, dr = n, ok = 0;

               while (st <= dr && !ok){

                   int m = (st + dr) / 2;

                   if (s[m] == z) ok = 1;

                   else if (z < s[m]) dr = m - 1;

                                 else st = m + 1;  

               }  

               if (ok) ++nr;  

           }

       }

   }  

   return nr;

}


iulianamarii: te roggggg
iulianamarii: urgent
Anonimul2003: bool cautare(int i,int n,int y,int s[])
{
int st = 1;
int dr = n;

while(st <= dr){
int mij = (st+dr)/2;
if(y == s[mij] && i != mij) return 1;
if( s[mij] < y) st = mij + 1;
if( s[mij] > y) dr = mij - 1;
}
return 0;

}

int diofantic(int n, int s[], int a,int b, int c)
{
int ct = 0;
long long x, y;
for(int i = 1; i <= n; i++)
{
x = a * s[i] * s[i];
y = c - x;
if(y%b == 0 && y >=0){
y/=b;
int z = sqrt(y);
if(z * z == y){

if(cautare(i,n,z,s)) ct++;
}
}

// cout< }

return ct;
}
Alte întrebări interesante