Doubt regarding READCOST

I have written a code with similar idea as other but why is mine exceeding TLE. Here are the two codes

Question : https://www.codechef.com/KOL18ROL/problems/READCOST

#include <bits/stdc++.h>
#define MOD 1000000007
#define arrn 100010
typedef long long int ll;
typedef unsigned long long ull;
#define forn(i, l, h) for (int i = l; i < h; i++)

using namespace std;

int main() {
  {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
  }

  while (1) {
    int n, x;
    ll m;
    cin >> n >> m >> x;
    if (m==0) {
      break;
    }
    ull out = 0;
    for (int i = 0; i < m;) {
      int k = ((i * n + x )/ m);
      ll f = ceil(((k + 1) * m - x) / (double)n) - 1;
      f = min(f, m - 1);
      out += (f - i + 1) * k;
      i = f + 1;
    }
    cout << out << '\n';
  }
  return 0;
}
#include <iostream>
#include <cmath>
#define iceil(n, x) (((n) + (x) - 1) / (x))
using namespace std;

#define int long long
main() {
// your code goes here
int n,m,x;
while(cin>>n>>m>>x && n) {
int j, i=0, sum=0, a=x/m;
while(i<=m-1){
j = iceil((a+1)*m - x, n);
if(j<=m-1){
sum += (j-i)*a;
i = j;
}else{
sum += (m-i)*a;
i = m+1;

}
a++;
}
cout << sum << '\n';
}
return 0;
}

The first coding is exceeding the time. I really appreciate any help you can provide.

//