Блог пользователя Afifii

Автор Afifii, история, 7 лет назад, По-английски

In a problem, when I evaluate the following expressions directly in the "main" function, the solution gets accepted.

ll sum1 = a[i] + com[n]-com[n-k] + com[i-1]-com[i-1-k];
ll sum2 = a[i] + com[n]-com[n-(k-1)] + com[i-1]-com[i-1-(k-1)];
double av1 = sum1/(double)(2*k+1);
double av2 = sum2/(double)(2*(k-1)+1);

But, when I instead use a function to evaluate the same expressions, the solution fails at test case 16.

double av1 = ValAt(i, k);
double av2 = ValAt(i, k-1);

The function:

double ValAt(int i, int k)
{
    ll sum = a[i] + com[n]-com[n-k] + com[i-1]-com[i-1-k];
    return sum/(double)(2*k+1);
}

Does any one have a rational explanation to this? Here are the two submissions: 28570229 , 28570252

  • Проголосовать: нравится
  • +9
  • Проголосовать: не нравится

»
7 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by Afifii (previous revision, new revision, compare).

»
7 лет назад, # |
Rev. 2   Проголосовать: нравится +1 Проголосовать: не нравится

I sent your solution and got AC changing double to long double, look. I think when you are assigning av to result of function it loses some precision and when you directly assign av to result it doesn't.

  • »
    »
    7 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    So, why should I loose precision when I am returning a value from a function that has the same return type? That is the strange point. Moreover, when I declared av1 and av2 as global variables and changed their values from the function, the solution also failed!

    • »
      »
      »
      7 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      At the beginning of section 8 expressions:

      The values of the floating operands and the results of floating expressions may be represented in greater precision and range than that required by the type; the types are not changed thereby.