Cod sursa(job #2410350)

Utilizator YusyBossFares Yusuf YusyBoss Data 19 aprilie 2019 22:30:15
Problema Rsir Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <fstream>
#include <map>

using namespace std;

map<int, int> f[7001];
short v[7001];

int main() {
  ifstream cin ("rsir.in");
  ofstream cout ("rsir.out");
  long long t0, t1, a, b, x, y, z, n, t2, i, ok, m, n1, rep;
  cin >> t0 >> t1 >> a >> b >> x >> y >> z >> m >> n;
  a %= m;
  b %= m;
  x %= m;
  y %= m;
  z %= m;
  t0 %= m;
  t1 %= m;
  i = 0;
  ok = 0;
  f[t0][t1] = 0;
  v[0] = t0;
  v[1] = t1;
  n1 = 2;
  while (i < n - 1 && (!ok)) {
    i++;
    t2 = 1ll * ((a * t0) % m * t0) % m + 1ll * ((b * t1) % m * t1) % m + (x * t0) % m + (y * t1) % m + z;
    t2 %= m;
    if (f[t1][t2] == 0) {
      f[t1][t2] = i;
      v[n1++] = t2;
    }
    else {
      rep = i - f[t1][t2] + 1;
      n -= f[t1][t2];
      ok = 1;
    }
    t0 = t1;
    t1 = t2;
  }
  if (ok == 1)
    cout << v[f[t1][t2] + n % rep];
  else
    cout << t2;
  return 0;
}