stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <cmath>

using namespace std;



int n, m=50000, mi;

double cx, cy, d, mx, p=1.0, x[55], y[55];

double f(double p, double q) {return p*p + q*q;}



int main() {

	int i;

	cin >> n;

	for(i=0; i<n; i++) {

		cin >> x[i] >> y[i];

		cx += x[i]; cy += y[i];

	}

	cx /= n; cy /= n;

	while(m--) {

		mx = f(cx - x[0], cy - y[0]);

		mi = 0;

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

			d = f(cx - x[i], cy - y[i]);

			if(d > mx) mx = d, mi = i;

		}

		cx += (x[mi] - cx) * p;

		cy += (y[mi] - cy) * p;

		p *= 0.999;

	}

	printf("%.9f", sqrt(mx));

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3205461951248398x):
#include <iostream>

#include <cstdio>

#include <cmath>

using namespace std;



int n, m=25000, mi;

double cx, cy, d, mx, p=1.0, x[55], y[55];

double f(double p, double q) {return p*p + q*q;}



int main() {

	int i;

	cin >> n;

	for(i=0; i<n; i++) {

		cin >> x[i] >> y[i];

		cx += x[i]; cy += y[i];

	}

	cx /= n; cy /= n;

	while(m--) {

		mx = f(cx - x[0], cy - y[0]);

		mi = 0;

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

			d = f(cx - x[i], cy - y[i]);

			if(d > mx) mx = d, mi = i;

		}

		cx += (x[mi] - cx) * p;

		cy += (y[mi] - cy) * p;

		p *= 0.999;

	}

	printf("%.9f", sqrt(mx));

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <cmath>

using namespace std;



int n, m=50000, mi;

double cx, cy, d, mx, p=1.0, x[55], y[55];

double f(double p, double q) {return p*p + q*q;}



int main() {

	int i;

	cin >> n;

	for(i=0; i<n; i++) {

		cin >> x[i] >> y[i];

		cx += x[i]; cy += y[i];

	}

	cx /= n; cy /= n;

	while(m--) {

		mx = f(cx - x[0], cy - y[0]);

		mi = 0;

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

			d = f(cx - x[i], cy - y[i]);

			if(d > mx) mx = d, mi = i;

		}

		cx += (x[mi] - cx) * p;

		cy += (y[mi] - cy) * p;

		p *= 0.999;

	}

	printf("%.9f", sqrt(mx));

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.4109598422337313x):
#include <iostream>

#include <cstdio>

#include <cmath>

using namespace std;



int n, m=20000, mi;

double cx, cy, d, mx, p=1.0, x[55], y[55];

double f(double p, double q) {return p*p + q*q;}



int main() {

	int i;

	cin >> n;

	for(i=0; i<n; i++) {

		cin >> x[i] >> y[i];

		cx += x[i]; cy += y[i];

	}

	cx /= n; cy /= n;

	while(m--) {

		mx = f(cx - x[0], cy - y[0]);

		mi = 0;

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

			d = f(cx - x[i], cy - y[i]);

			if(d > mx) mx = d, mi = i;

		}

		cx += (x[mi] - cx) * p;

		cy += (y[mi] - cy) * p;

		p *= 0.999;

	}

	printf("%.9f", sqrt(mx));

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pb push_back

#define fi first

#define se second



typedef pair<ll,ll> P;

using VP = vector<P>; using VVP = vector<VP>;

using VI = vector<int>; using VVI = vector<VI>; using VVVI = vector<VVI>;

const long double inf=1e9+7;

const ll INF=1LL<<61;

const ll mod=1e9+7;



double x[55],y[55];

double dif(int i,int j){

  return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));

}





int main(){

  int i,j,k;

  int n;

  cin>>n;

  for(i=0;i<n;i++){

    cin>>x[i]>>y[i];

  }

  x[n]=0,y[n]=0;

  double l=500;

  while(l>1e-68){

    double md=-1;

    int ind;

    for(i=0;i<n;i++){

      if(md<dif(i,n)){

        md=dif(i,n);

        ind=i;

      }

    }

    x[n]=x[n]+l*(x[ind]-x[n])/md;

    y[n]=y[n]+l*(y[ind]-y[n])/md;

    l*=0.995;

  }

  double md=-1;

  int ind;

  for(i=0;i<n;i++){

    if(md<dif(i,n)){

        md=dif(i,n);

        ind=i;

    }

  }



  cout<<fixed<<setprecision(15)<<md<<endl;

  

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.011071120544551x):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pb push_back

#define fi first

#define se second

typedef pair<ll,ll> P;

using VP = vector<P>; using VVP = vector<VP>;

using VI = vector<int>; using VVI = vector<VI>; using VVVI = vector<VVI>;

const int inf=1e9+7;

const ll INF=1LL<<61;

const ll mod=1e9+7;



int n;

double x[1010],y[1010];



double ly,ry;



double solve(double px){

  ly=-1000.0;

  ry=2000.0;

  while(1){

    double sy=(2*ly+ry)/3.0;

    double ty=(ly+2*ry)/3.0;

    double ss,tt;

    ss=0.0,tt=0.0;

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

      ss=max(ss,sqrt((px-x[i])*(px-x[i])+(sy-y[i])*(sy-y[i])));

      tt=max(tt,sqrt((px-x[i])*(px-x[i])+(ty-y[i])*(ty-y[i])));

    }

    //cout<<ss<<endl;

    if(ry-ly<=1e-10) {

      //cout<<ss<<endl;

      return ss;

    }

    if(ss<=tt) ry=ty;

    else ly=sy;

  }

}



int main(){

  int i,j;

  cin>>n;

  for(i=0;i<n;i++) cin>>x[i]>>y[i];

  double lx=-1000.0,rx=2000.0;

  while(1){

    double sx=(2*lx+rx)/3.0;

    double tx=(lx+2*rx)/3.0;

    if(rx-lx<=1e-10) {

      cout<<fixed<<setprecision(15)<<solve(sx)<<endl;

      return 0;

    }

    if(solve(sx)<=solve(tx)) rx=tx;

    else lx=sx;

  }



  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

#define rep(i,n) for (int i = 0; i < (n); ++i)

using namespace std;

using ll = long long;

using P = pair<int,int>;

#define chmax(x,y) x = max(x,y);

#define chmin(x,y) x = min(x,y);

const int di[] = {-1, 0, 1, 0};

const int dj[] = {0, -1, 0, 1};

const int INF = 1001001001;







int main() {

    ll n, x, m;

    cin >> n >> x >> m;

    vector<ll> a;

    vector<int> q(100005);

    ll d = x;

    a.push_back(x);

    ll t = n;

    ll ans = 0;

    ll tot = 0;

    ll point = 0;

    bool ok = false;

    while (1) {

        t--;

        d = (d*d)%m;

        //cout << d << endl;

        if (d == 0) {

            rep(i,a.size()) ans += a[i];

            cout << ans << endl;

            return 0;

        }

        if (d == 1) {

            rep(i,a.size()) tot += a[i];

            ans = tot + n-ll(a.size());

            cout << ans << endl;

            return 0;

        }

        if (d == a.back()) {

            rep(i,a.size()) tot += a[i];

            ans = tot + (n-ll(a.size()))*d;

            cout << ans << endl;

            return 0;

        }

        if (q[d] == 1) {

            point = d;

            ok = true;

            break;

        }

        if (t == 0) break;

        a.push_back(d);

        q[d]++;

    }

    if (ok) {

        rep(i,a.size()) ans += a[i];

        n -= ll(a.size());

        ll po = 0;

        rep(i,a.size()) {

            if (a[i] == point) po = i;

        }

        ll loop = 0;

        for (int i = po; i < a.size(); i++) {

            loop += a[i];

        }

        ll ji = ll(a.size()) - po;

        ans += loop * (n/ji);

      n %= ji;

      for (int i = po; i <= po+n-1; i++) ans += a[i];

        cout << ans << endl;

        return 0;

    }

    if (t == 0) {

        rep(i,a.size()) ans += a[i];

    }

    else {

        ll p = a.size();

        //ll tot = 0;

        rep(i,a.size()) tot += a[i];

        ll y = n/p;

        //rep(i,y) ans += tot;

        ans += tot*y;

        n %= p;

        rep(i,n) ans += a[i];

    }

    cout << ans << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.257639350646066x):
#include <bits/stdc++.h>

#define rep(i,n) for (int i = 0; i < (n); ++i)

using namespace std;

using ll = long long;

using P = pair<int,int>;

#define chmax(x,y) x = max(x,y);

#define chmin(x,y) x = min(x,y);

const int di[] = {-1, 0, 1, 0};

const int dj[] = {0, -1, 0, 1};

const int INF = 1001001001;







int main() {

    ll n, x, m;

    cin >> n >> x >> m;

    vector<int> id(m,-1);

    vector<int> a;

    int len = 0;

    ll tot = 0;

    while (id[x] == -1) {

        a.push_back(x);

        id[x] = len;

        len++;

        tot += x;

        x = (x*x)%m;

    }



    int c = len-id[x];

    ll s = 0;

    for (int i = id[x]; i < len; i++) s += a[i];



    ll ans = 0;

    if (n <= len) {

        rep(i,n) ans += a[i];

    } else {

        ans += tot;

        n -= len;

        ans += s*(n/c);

        n %= c;

        rep(i,n) ans += a[id[x]+i];

    }

    cout << ans << endl;

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

#define rep(i,n) for (int i = 0; i < (n); ++i)

using namespace std;

using ll = long long;

using P = pair<int,int>;

#define chmax(x,y) x = max(x,y);

#define chmin(x,y) x = min(x,y);

const int di[] = {-1, 0, 1, 0};

const int dj[] = {0, -1, 0, 1};

const int INF = 1001001001;







int main() {

    ll n, x, m;

    cin >> n >> x >> m;

    vector<ll> a;

    vector<int> q(100005);

    ll d = x;

    a.push_back(x);

    ll t = n;

    ll ans = 0;

    ll tot = 0;

    ll point = 0;

    bool ok = false;

    while (1) {

        t--;

        d = (d*d)%m;

        //cout << d << endl;

        if (d == 0) {

            rep(i,a.size()) ans += a[i];

            cout << ans << endl;

            return 0;

        }

        if (d == 1) {

            rep(i,a.size()) tot += a[i];

            ans = tot + n-ll(a.size());

            cout << ans << endl;

            return 0;

        }

        if (d == a.back()) {

            rep(i,a.size()) tot += a[i];

            ans = tot + (n-ll(a.size()))*d;

            cout << ans << endl;

            return 0;

        }

        if (q[d] == 1) {

            point = d;

            ok = true;

            break;

        }

        if (t == 0) break;

        a.push_back(d);

        q[d]++;

    }

    if (ok) {

        rep(i,a.size()) ans += a[i];

        n -= ll(a.size());

        ll po = 0;

        rep(i,a.size()) {

            if (a[i] == point) po = i;

        }

        ll loop = 0;

        for (int i = po; i < a.size(); i++) {

            loop += a[i];

        }

        ll ji = ll(a.size()) - po;

        ans += loop * (n/ji);

      n %= ji;

      for (int i = po; i <= po+n-1; i++) ans += a[i];

        cout << ans << endl;

        return 0;

    }

    if (t == 0) {

        rep(i,a.size()) ans += a[i];

    }

    else {

        ll p = a.size();

        //ll tot = 0;

        rep(i,a.size()) tot += a[i];

        ll y = n/p;

        //rep(i,y) ans += tot;

        ans += tot*y;

        n %= p;

        rep(i,n) ans += a[i];

    }

    cout << ans << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.257639350646066x):
#include <bits/stdc++.h>

#define rep(i,n) for (int i = 0; i < (n); ++i)

using namespace std;

using ll = long long;

using P = pair<int,int>;

#define chmax(x,y) x = max(x,y);

#define chmin(x,y) x = min(x,y);

const int di[] = {-1, 0, 1, 0};

const int dj[] = {0, -1, 0, 1};

const int INF = 1001001001;







int main() {

    ll n, x, m;

    cin >> n >> x >> m;

    vector<int> id(m,-1);

    vector<int> a;

    int len = 0;

    ll tot = 0;

    while (id[x] == -1) {

        a.push_back(x);

        id[x] = len;

        len++;

        tot += x;

        x = (x*x)%m;

    }



    int c = len-id[x];

    ll s = 0;

    for (int i = id[x]; i < len; i++) s += a[i];



    ll ans = 0;

    if (n <= len) {

        rep(i,n) ans += a[i];

    }

    else {

        ans += tot;

        n -= len;

        ans += s*(n/c);

        n %= c;

        rep(i,n) ans += a[id[x]+i];

    }

    cout << ans << endl;

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

#define rep(i,n) for (int i = 0; i < (n); ++i)

using namespace std;

using ll = long long;

using P = pair<int,int>;

#define chmax(x,y) x = max(x,y);

#define chmin(x,y) x = min(x,y);

const int di[] = {-1, 0, 1, 0};

const int dj[] = {0, -1, 0, 1};

const int INF = 1001001001;







int main() {

    ll n, x, m;

    cin >> n >> x >> m;

    vector<ll> a;

    vector<int> q(100005);

    ll d = x;

    a.push_back(x);

    ll t = n;

    ll ans = 0;

    ll tot = 0;

    ll point = 0;

    bool ok = false;

    while (1) {

        t--;

        d = (d*d)%m;

        //cout << d << endl;

        if (d == 0) {

            rep(i,a.size()) ans += a[i];

            cout << ans << endl;

            return 0;

        }

        if (d == 1) {

            rep(i,a.size()) tot += a[i];

            ans = tot + n-ll(a.size());

            cout << ans << endl;

            return 0;

        }

        if (d == a.back()) {

            rep(i,a.size()) tot += a[i];

            ans = tot + (n-ll(a.size()))*d;

            cout << ans << endl;

            return 0;

        }

        if (q[d] == 1) {

            point = d;

            ok = true;

            break;

        }

        if (t == 0) break;

        a.push_back(d);

        q[d]++;

    }

    if (ok) {

        rep(i,a.size()) ans += a[i];

        n -= ll(a.size());

        ll po = 0;

        rep(i,a.size()) {

            if (a[i] == point) po = i;

        }

        ll loop = 0;

        for (int i = po; i < a.size(); i++) {

            loop += a[i];

        }

        ll ji = ll(a.size()) - po;

        ans += loop * (n/ji);

      n %= ji;

      for (int i = po; i <= po+n-1; i++) ans += a[i];

        cout << ans << endl;

        return 0;

    }

    if (t == 0) {

        rep(i,a.size()) ans += a[i];

    }

    else {

        ll p = a.size();

        //ll tot = 0;

        rep(i,a.size()) tot += a[i];

        ll y = n/p;

        //rep(i,y) ans += tot;

        ans += tot*y;

        n %= p;

        rep(i,n) ans += a[i];

    }

    cout << ans << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.2575567858195578x):
#include <bits/stdc++.h>

#define rep(i,n) for (int i = 0; i < (n); ++i)

using namespace std;

using ll = long long;

using P = pair<int,int>;

#define chmax(x,y) x = max(x,y);

#define chmin(x,y) x = min(x,y);

const int di[] = {-1, 0, 1, 0};

const int dj[] = {0, -1, 0, 1};

const int INF = 1001001001;







int main() {

    ll n, x, m;

    cin >> n >> x >> m;

    vector<int> id(m, -1);

    vector<int> a;

    int len = 0;

    ll tot = 0;

    while (id[x] == -1) {

        a.push_back(x);

        id[x] = len;

        len++;

        tot += x;

        x = (x*x)%m;

    }



    int c = len-id[x];

    ll s = 0;

    for (int i = id[x]; i < len; i++) s += a[i];



    ll ans = 0;

    if (n <= len) {

        rep(i,n) ans += a[i];

    }

    else {

        ans += tot;

        n -= len;

        ans += s*(n/c);

        n %= c;

        rep(i,n) ans += a[id[x]+i];

    }

    cout << ans << endl;

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

#define rep(i,n) for (int i = 0; i < (n); ++i)

using namespace std;

using ll = long long;

using P = pair<int,int>;

#define chmax(x,y) x = max(x,y);

#define chmin(x,y) x = min(x,y);

const int di[] = {-1, 0, 1, 0};

const int dj[] = {0, -1, 0, 1};

const int INF = 1001001001;







int main() {

    ll n, x, m;

    cin >> n >> x >> m;

    vector<ll> a;

    vector<int> q(100005);

    ll d = x;

    a.push_back(x);

    ll t = n;

    ll ans = 0;

    ll tot = 0;

    ll point = 0;

    bool ok = false;

    while (1) {

        t--;

        d = (d*d)%m;

        //cout << d << endl;

        if (d == 0) {

            rep(i,a.size()) ans += a[i];

            cout << ans << endl;

            return 0;

        }

        if (d == 1) {

            rep(i,a.size()) tot += a[i];

            ans = tot + n-ll(a.size());

            cout << ans << endl;

            return 0;

        }

        if (d == a.back()) {

            rep(i,a.size()) tot += a[i];

            ans = tot + (n-ll(a.size()))*d;

            cout << ans << endl;

            return 0;

        }

        if (q[d] == 1) {

            point = d;

            ok = true;

            break;

        }

        if (t == 0) break;

        a.push_back(d);

        q[d]++;

    }

    if (ok) {

        rep(i,a.size()) ans += a[i];

        n -= ll(a.size());

        ll po = 0;

        rep(i,a.size()) {

            if (a[i] == point) po = i;

        }

        ll loop = 0;

        for (int i = po; i < a.size(); i++) {

            loop += a[i];

        }

        ll ji = ll(a.size()) - po;

        ans += loop * (n/ji);

      n %= ji;

      for (int i = po; i <= po+n-1; i++) ans += a[i];

        cout << ans << endl;

        return 0;

    }

    if (t == 0) {

        rep(i,a.size()) ans += a[i];

    }

    else {

        ll p = a.size();

        //ll tot = 0;

        rep(i,a.size()) tot += a[i];

        ll y = n/p;

        //rep(i,y) ans += tot;

        ans += tot*y;

        n %= p;

        rep(i,n) ans += a[i];

    }

    cout << ans << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.257639350646066x):
#include <bits/stdc++.h>

#define rep(i,n) for (int i = 0; i < (n); ++i)

using namespace std;

using ll = long long;

using P = pair<int,int>;

#define chmax(x,y) x = max(x,y);

#define chmin(x,y) x = min(x,y);

const int di[] = {-1, 0, 1, 0};

const int dj[] = {0, -1, 0, 1};

const int INF = 1001001001;







int main() {

    ll n, x, m;

    cin >> n >> x >> m;

    vector<int> id(m, -1);

    vector<int> a;

    int len = 0;

    ll tot = 0;

    while (id[x] == -1) {

        a.push_back(x);

        id[x] = len;

        len++;

        tot += x;

        x = (x*x)%m;

    }



    int c = len-id[x];

    ll s = 0;

    for (int i = id[x]; i < len; i++) s += a[i];



    ll ans = 0;

    if (n <= len) {

        rep(i,n) ans += a[i];

    }

    else {

        ans += tot;

        n -= len;

        ans += s*(n/c);

        n %= c;

        rep(i,n) ans += a[id[x]+i];

    }

    cout << ans << endl;

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

typedef long long lint;

using namespace std;



lint n, x, m;

const lint E = 40;



lint f(lint z) {

    return z * z % m;

}



struct str {

    lint dest;

    lint sum;

    str() {}

    str(lint d, lint s) : dest(d), sum(s) {}

};



vector<vector<str>> doubling;



lint solve(int start, lint num) {

    lint now = start;

    bitset<E> b(num);

    lint res = 0;

    for (int i = 0; i < E; i++) {

        if (b[i]) {

            auto s = doubling[i][now];

            now = s.dest;

            res += s.sum;

        }

    }

    return res;

}



int main() {

    cin >> n >> x >> m;

    doubling.resize(E, vector<str>(m));

    for (int i = 0; i < m; i++) {

        doubling[0][i] = str(f(i), i);

    }

    for (int e = 1; e < E; e++) {

        for (int i = 0; i < m; i++) {

            auto first = doubling[e - 1][i];

            auto second = doubling[e - 1][first.dest];

            doubling[e][i] = str(second.dest, first.sum + second.sum);

        }

    }

    cout << solve(x, n) << endl;

}

// Faster C++ solution for the same problem (measured speedup: 1.8897840221326399x):
#include <bits/stdc++.h>

typedef long long lint;

using namespace std;



struct loop_info {

    lint loop_len;

    lint prefix_len;

    lint unit;

    loop_info() {}

    loop_info(lint l, lint p, lint u) : loop_len(l), prefix_len(p), unit(u) {}

};



lint n, x, m;



lint f(lint z) {

    return z * z % m;

}



loop_info get_loop() {

    vector<lint> appear_time(m, -1);

    lint now = x;

    lint time = 0;

    loop_info res;

    lint start_val;

    while (true) {

        if (appear_time[now] == -1) {

            appear_time[now] = time;

            time++;

            now = f(now);

        } else {

            res.loop_len = time - appear_time[now];

            res.prefix_len = appear_time[now];

            start_val = now;

            break;

        }

    }

    lint unit = 0;

    lint val = start_val;

    for (int i = 0; i < res.loop_len; i++) {

        unit += val;

        val = f(val);

    }

    res.unit = unit;

    return res;

}



lint simple_solve(lint k) {

    lint now = x;

    lint res = 0;

    for (int i = 0; i < k; i++) {

        res += now;

        now = f(now);

    }

    return res;

}



int main() {

    cin >> n >> x >> m;

    auto loop = get_loop();

    if (n < loop.prefix_len) {

        cout << simple_solve(n) << endl;

    } else {

        lint loop_num = (n - loop.prefix_len) / loop.loop_len;

        lint rem = n - loop_num * loop.loop_len;

        lint ans = simple_solve(rem) + loop.unit * loop_num;

        cout << ans << endl;

    }

}
// Slower C++ solution:
#include <bits/stdc++.h>

typedef long long lint;

using namespace std;



lint n, x, m;

const lint E = 40;



lint f(lint z) {

    return z * z % m;

}



struct str {

    lint dest;

    lint sum;

    str() {}

    str(lint d, lint s) : dest(d), sum(s) {}

};



vector<vector<str>> doubling;



lint solve(int start, lint num) {

    lint now = start;

    bitset<E> b(num);

    lint res = 0;

    for (int i = 0; i < E; i++) {

        if (b[i]) {

            auto s = doubling[i][now];

            now = s.dest;

            res += s.sum;

        }

    }

    return res;

}



int main() {

    cin >> n >> x >> m;

    doubling.resize(E, vector<str>(m));

    for (int i = 0; i < m; i++) {

        doubling[0][i] = str(f(i), i);

    }

    for (int e = 1; e < E; e++) {

        for (int i = 0; i < m; i++) {

            auto first = doubling[e - 1][i];

            auto second = doubling[e - 1][first.dest];

            doubling[e][i] = str(second.dest, first.sum + second.sum);

        }

    }

    cout << solve(x, n) << endl;

}

// Faster C++ solution for the same problem (measured speedup: 1.8895359055059968x):
#include <bits/stdc++.h>

typedef long long lint;

using namespace std;



struct loop_info {

    lint loop_len;

    lint prefix_len;

    lint unit;

};



lint n, x, m;



lint f(lint z) {

    return z * z % m;

}



lint simple_solve(lint start, lint k) {

    lint now = start;

    lint res = 0;

    for (int i = 0; i < k; i++) {

        res += now;

        now = f(now);

    }

    return res;

}



loop_info get_loop() {

    vector<lint> appear_time(m, -1);

    lint now = x;

    lint time = 0;

    loop_info res;

    while (true) {

        if (appear_time[now] == -1) {

            appear_time[now] = time;

            time++;

            now = f(now);

        } else {

            res.loop_len = time - appear_time[now];

            res.prefix_len = appear_time[now];

            res.unit = simple_solve(now, res.loop_len);

            return res;

        }

    }

}



int main() {

    cin >> n >> x >> m;

    auto loop = get_loop();

    if (n < loop.prefix_len) {

        cout << simple_solve(x, n) << endl;

    } else {

        lint loop_num = (n - loop.prefix_len) / loop.loop_len;

        lint rem = n - loop_num * loop.loop_len;

        lint ans = simple_solve(x, rem) + loop.unit * loop_num;

        cout << ans << endl;

    }

}