Cod sursa(job #1821689)

Utilizator BrandonChris Luntraru Brandon Data 3 decembrie 2016 14:38:44
Problema Rsir Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <fstream>

#define LL long long
using namespace std;

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

const int MaxM = 7005;

LL t0, t1, a, b, x, y, z, M, n, tprev, tcurr, tprev2, tcurr2, CycleLength = 1;
LL A[MaxM], B[MaxM];

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

int main() {
  cin >> t0 >> t1 >> a >> b >> x >> y >> z >> M >> n;

  for (int i = 1; i <= M; ++i) {
    A[i] = a * i * i + x * i;
    B[i] = b * i * i + y * i;
  }

  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 (tprev2 != t0 or tcurr2 != t1);

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