Cod sursa(job #2310612)

Utilizator AlexandruLuchianov1Alex Luchianov AlexandruLuchianov1 Data 1 ianuarie 2019 17:55:59
Problema Rsir Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <iostream>
#include <fstream>
#include <bitset>

using namespace std;

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

#define ll long long
#define MIN(a , b) (((a) < (b)) ? (a) : (b))
#define MAX(a , b) (((a) < (b)) ? (b) : (a))

int const nmax = 7000;

bitset <10 + nmax * nmax> frec;


int main()
{
  int t0 , t1 , a , b , x , y , z , modulo ;
  ll n;
  in >> t0 >> t1 >> a >> b >> x >> y >> z >> modulo >> n;
  t0 %= modulo;
  t1 %= modulo;
  int initt0 = t0, initt1 = t1;

  int lim = 1 , val = 0;

  while(true){
    //cout << t0 << " " << t1 << '\n';

    if(frec[t0 * modulo + t1] == 1) {
      val = t0 * modulo + t1;
      break;
    }
    frec[t0 * modulo + t1] = 1;

    t0 = a * t0 % modulo * t0 + b * t1 % modulo * t1 + x * t0 + y * t1 + z;
    t0 %= modulo;

    swap(t0 , t1);
    lim++;
  }

  t0 = initt0;
  t1 = initt1;

  int start = 1;

  while(true){
    if(t0 * modulo + t1 == val)
      break;

    t0 = a * t0 % modulo * t0 + b * t1 % modulo * t1 + x * t0 + y * t1 + z;
    t0 %= modulo;

    swap(t0 , t1);
    start++;
  }

  //cout << start << " " << lim << '\n';

  if(start <= n)
    n = (n - start) % (lim - start) + start;

  t0 = initt0;
  t1 = initt1;
  for(int i = 2 ; i <= n ; i++){
    t0 = a * t0 % modulo * t0 + b * t1 % modulo * t1 + x * t0 + y * t1 + z;
    t0 %= modulo;

    swap(t0 , t1);
  }
  out << t1;

  return 0;
}