Cod sursa(job #1821670)

Utilizator BrandonChris Luntraru Brandon Data 3 decembrie 2016 14:18:16
Problema Rsir Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 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, tprev2, tcurr2, 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;
  t0 %= M;
  t1 %= M;
  tprev = t0;
  tcurr = t1;
  for (CycleLength = 0; CycleLength < n and CycleLength < M * M; ++CycleLength) {
    LL aux = tcurr;
    tcurr = NextElement(tprev, tcurr);
    tprev = aux;
  }

  if (CycleLength == n) {
    cout << tprev << '\n';
    return 0;
  }

  n -= CycleLength;
  tprev2 = tprev;
  tcurr2 = tcurr;
  do {
    LL aux = tcurr2;
    tcurr2 = NextElement(tprev2, tcurr2);
    tprev2 = aux;
    ++CycleLength;
  } while (tprev != t0 or tcurr != t1);

  n %= CycleLength;
  for (int i = 1; i < n; ++i) {
    LL aux = tcurr;
    tcurr = NextElement(tprev, tcurr);
    tprev = aux;
  }
  cout << tcurr << '\n';
  return 0;
}