记录编号 144958 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 [NOI 2012]随机数生成器 最终得分 100
用户昵称 GravatarAsm.Def 是否通过 通过
代码语言 C++ 运行时间 0.006 s
提交时间 2014-12-31 20:01:28 内存使用 0.32 MiB
显示代码纯文本
/*====================Asm.Def========================*/
#include <iostream>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <vector>
#include <cmath>
#include <cstring>
#ifndef DEBUG
#define getchar getchar_unlocked
#endif
using namespace std;
template <typename T>inline void getd(T &x){
	char ch = getchar();bool minus = 0;
	while(!isdigit(ch) && ch != '-')ch = getchar();
	if(ch == '-'){minus = 1;ch = getchar();}
	x = ch - '0';
	while(isdigit(ch = getchar()))x = x * 10 + ch - '0';
	if(minus)x = -x;
}
/*======================================================*/
typedef long long LL;
LL mod;

inline LL mul(LL a, LL b){
	if(a < b)swap(a, b);
	LL ans = 0;
	while(b){
		if(b & 1)ans = (ans + a) % mod;
		b >>= 1;
		a = (a << 1) % mod;
	}
	return ans;
}

struct Mat{
	LL A, B, C, D;
	inline void multi(const Mat &x){
		static LL a, b, c, d;
		a = (mul(A, x.A) + mul(B, x.C)) % mod;
		b = (mul(A, x.B) + mul(B, x.D)) % mod;
		c = (mul(C, x.A) + mul(D, x.C)) % mod;
		d = (mul(C, x.B) + mul(D, x.D)) % mod;
		A = a, B = b, C = c, D = d;
	}
	inline void pow(LL n){
		Mat a = *this;--n;
		while(n){
			if(n & 1)multi(a);
			n >>= 1;
			a.multi(a);
		}
	}
}F;

int main(){
	#if defined DEBUG
	freopen("test", "r", stdin);
	//freopen("output.txt", "w", stdout);
	#else
		#ifndef ONLINE_JUDGE
			freopen("randoma.in", "r", stdin);
			freopen("randoma.out", "w", stdout);
		#endif
	#endif
	LL a, c, X0, n, g;
	getd(mod);getd(a);getd(c);getd(X0);getd(n);getd(g);
	if(n == 0){
		printf("%lld\n", X0);
		return 0;
	}
	F.A = a, F.C = c, F.D = 1;
	F.pow(n);
	printf("%lld\n", ((mul(X0, F.A) + F.C) % mod) % g);
	
	#if defined DEBUG
	cout << endl << (double)clock()/CLOCKS_PER_SEC << endl;
	#endif
	return 0;
}