Pagini recente » Cod sursa (job #2874167) | Cod sursa (job #2914419) | Cod sursa (job #3157832) | Cod sursa (job #3234428) | Cod sursa (job #3207983)
#include <fstream>
#define f first
#define s second
#define ll long long
using namespace std;
ifstream fin ("rsir.in");
ofstream fout ("rsir.out");
ll xx,yy,zz,x,y,z,t0,t1,t2,t3,pas,a,b,m,i,n,nrp,nr_ciclu;
pair <ll,ll> ip,te;
int f (ll t0,ll t1)
{
return (a*t0*t0+b*t1*t1+xx*t0+yy*t1+zz)%m;
}
ll term (ll n)
{
x=t0;
y=t1;
for (i=3; i<=n; i++)
{
z=f (x,y);
x=y;
y=z;
}
return y;
}
int main()
{
fin>>t0>>t1>>a>>b>>xx>>yy>>zz>>m>>n;
t0%=m;
t1%=m;
n++;
if (n==1)
{
fout<<t0;
return 0;
}
if (n==2)
{
fout<<t1;
return 0;
}
pas=1;
t2=f (t0,t1);
t3=f (t1,t2);
te={t1,t2};
ip= {t2,t3};
while (ip!=te)
{
pas++;
x=f (te.f,te.s);
te= {te.s,x};
y=f (ip.f,ip.s);
z=f (ip.s,y);
ip= {y,z};
}
nr_ciclu=pas;
pas=0;
te= {t0,t1};
while (te!=ip)
{
pas++;
x=f (te.f,te.s);
te= {te.s,x};
y=f (ip.f,ip.s);
ip= {ip.s,z};
}
nrp=pas;
if (n<=nrp)
{
fout<<term (n);
return 0;
}
else
{
n-=nrp;
n%=nr_ciclu;
if (n==0)
n=nr_ciclu;
fout<<term (n+nrp);
}
return 0;
}