Cod sursa(job #1821611)

Utilizator BrandonChris Luntraru Brandon Data 3 decembrie 2016 13:11:44
Problema Rsir Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <fstream>

#define LL long long
using namespace std;

ifstream cin ("rsir.in");
ofstream cout ("rsir.out");

LL t0, t1, a, b, x, y, z, M, n, tprev, tcurr, CycleLength = 1;

inline LL NextElement(LL tm2, LL tm1) {
  return (a * tm2 * tm2 % M + b * tm1 * tm1 % M + x * tm2 % M + y * tm1 % M + z) % M;
}

int main() {
  cin >> t0 >> t1 >> a >> b >> x >> y >> z >> M >> n;
  tcurr = t1;
  tprev = t0;
  do {
    LL aux = tcurr;
    tcurr = NextElement(tprev, tcurr);
    tprev = aux;
    ++CycleLength;
  } while (tprev != t0 or tcurr != t1);

  n %= CycleLength;
  tcurr = t1;
  tprev = t0;
  for (int i = 1; i < n; ++i) {
    LL aux = tcurr;
    tcurr = NextElement(tprev, tcurr);
    tprev = aux;
  }

  cout << tcurr << '\n';
  return 0;
}