LG2044 [NOI2012]随机数生成器
题意
栋栋最近迷上了随机算法,而随机数是生成随机算法的基础。栋栋准备使用线性同余法(Linear Congruential Method)来生成一个随机数列,这种方法需要设置四个非负整数参数m,a,c,X[0],按照下面的公式生成出一系列随机数{Xn}:
X[n+1]=(aX[n]+c) mod m
其中mod m表示前面的数除以m的余数。从这个式子可以看出,这个序列的下一个数总是由上一个数生成的。
用这种方法生成的序列具有随机序列的性质,因此这种方法被广泛地使用,包括常用的C++和Pascal的产生随机数的库函数使用的也是这种方法。
栋栋知道这样产生的序列具有良好的随机性,不过心急的他仍然想尽快知道X[n]是多少。由于栋栋需要的随机数是0,1,...,g-1之间的,他需要将X[n]除以g取余得到他想要的数,即X[n] mod g,你只需要告诉栋栋他想要的数X[n] mod g是多少就可以了。
\(n,m,a,c,X[0] \leq 10^{18},g \leq10^8\)
分析
构造法
见这篇博客
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<ctime>
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<complex>
#pragma GCC optimize ("O0")
using namespace std;
template<class T> inline T read(T&x)
{
T data=0;
int w=1;
char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(isdigit(ch))
data=10*data+ch-'0',ch=getchar();
return x=data*w;
}
typedef long long ll;
const int INF=0x7fffffff;
ll mod;
ll qmul(ll x,ll y)
{
ll res = 0;
while(y)
{
if(y&1)
(res += x) %= mod;
(x += x) %= mod, y >>= 1;
}
return res;
}
ll qpow(ll x,ll k)
{
ll res = 1;
while(k)
{
if(k&1)
res = qmul(res,x);
x = qmul(x,x), k >>= 1;
}
return res;
}
ll a,c;
ll sum(ll n)
{
if(n == 1)
return c;
ll res = sum(n / 2);
(res += qmul(qpow(a,n / 2),res) ) %= mod;
if(n&1)
(res += qmul(qpow(a,n - 1),c)) %= mod;
return res;
}
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ll X,n,g;
read(mod);read(a);read(c);read(X);read(n);read(g);
ll ans = qpow(a,n);
ans = qmul(ans,X);
(ans += sum(n)) %= mod;
printf("%lld\n",ans % g);
// fclose(stdin);
// fclose(stdout);
return 0;
}
矩阵法
\left[
\begin{matrix}
a & c\\
0 & 1\\
\end{matrix}
\right]^n
\times
\left[
\begin{matrix}
X_0\\
1
\end{matrix}
\right]
\right)_{1,1}
\mod m
\]
矩阵快速幂解决。
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<ctime>
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<complex>
#pragma GCC optimize ("O0")
using namespace std;
template<class T> inline T read(T&x)
{
T data=0;
int w=1;
char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(isdigit(ch))
data=10*data+ch-'0',ch=getchar();
return x=data*w;
}
typedef long long ll;
const int INF=0x7fffffff;
ll mod;
ll qmul(ll x,ll y)
{
ll res = 0;
while(y)
{
if(y&1)
(res += x) %= mod;
(x += x) %= mod,y >>= 1;
}
return res;
}
struct Matrix
{
ll data[2][2];
Matrix()
{
memset(data,0,sizeof data);
}
ll*operator[](const int&x)
{
return data[x];
}
Matrix operator*(const Matrix&rhs)const
{
Matrix res;
for(int i=0;i<2;++i)
for(int j=0;j<2;++j)
{
for(int k=0;k<2;++k)
{
(res[i][j] += qmul(data[i][k],rhs.data[k][j])) %= mod; // edit 1:data -> rhs
/*if(res[i][j]<0)
res[i][j] += mod;*/
}
}
return res;
}
Matrix&operator*=(const Matrix&rhs)
{
return *this=*this*rhs;
}
void out()
{
cerr<<"check"<<endl;
for(int i=0;i<2;++i)
{
for(int j=0;j<2;++j)
cerr<<data[i][j]<<" ";
cerr<<endl;
}
}
}a,b;
Matrix qpow(Matrix x,ll k)
{
Matrix res;
res[0][0]=res[1][1]=1;
while(k)
{
if(k&1)
res *= x;
x *= x, k >>= 1;
}
return res;
}
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
read(mod);
read(a[0][0]);read(a[0][1]);a[1][1]=1;
read(b[0][0]);b[1][0]=1;
ll n,g;
read(n);read(g);
a = qpow(a,n);
a *= b;
printf("%lld\n",a[0][0] % g);
// fclose(stdin);
// fclose(stdout);
return 0;
}
第二种方法比第一种方法快几毫秒。
LG2044 [NOI2012]随机数生成器的更多相关文章
- 矩阵(快速幂):COGS 963. [NOI2012] 随机数生成器
963. [NOI2012] 随机数生成器 ★★ 输入文件:randoma.in 输出文件:randoma.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 栋 ...
- BZOJ 2875: [Noi2012]随机数生成器( 矩阵快速幂 )
矩阵快速幂...+快速乘就OK了 ----------------------------------------------------------------------------------- ...
- Bzoj 2875: [Noi2012]随机数生成器(矩阵乘法)
2875: [Noi2012]随机数生成器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 2052 Solved: 1118 Description ...
- [NOI2012]随机数生成器【矩阵快速幂】
NOI2012 随机数生成器 题目描述 栋栋最近迷上了随机算法,而随机数是生成随机算法的基础.栋栋准备使用线性同余法(Linear Congruential Method)来生成一个随机数列,这种方法 ...
- BZOJ2875 & 洛谷2044:[NOI2012]随机数生成器——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=2875 https://www.luogu.org/problemnew/show/P2044 栋栋 ...
- BZOJ2875 [Noi2012]随机数生成器 【矩阵乘法 + 快速乘】
题目 栋栋最近迷上了随机算法,而随机数是生成随机算法的基础.栋栋准备使用线性同余法(Linear Congruential Me thod)来生成一个随机数列,这种方法需要设置四个非负整数参数m,a, ...
- bzoj 2875: [Noi2012]随机数生成器
#include<cstdio> #include<iostream> #include<cstring> #define ll long long using n ...
- 【BZOJ】2875: [Noi2012]随机数生成器(矩阵乘法+快速乘)
http://www.lydsy.com/JudgeOnline/problem.php?id=2875 矩阵的话很容易看出来.....我就不写了.太水了. 然后乘法longlong会溢出...那么我 ...
- 2875: [Noi2012]随机数生成器 - BZOJ
DescriptionInput 包含6个用空格分割的m,a,c,X0,n和g,其中a,c,X0是非负整数,m,n,g是正整数. Output 输出一个数,即Xn mod gSample Input ...
随机推荐
- Failed to execute operation: No such file or directory(systemctl enable iptables.service)
在保存Iptables配置时:systemctl enable iptables.service 出现错误: Failed to execute operation: No such file or ...
- javascript之非构造函数的继承
这个系列的第一部分介绍了"封装",第二部分介绍了使用构造函数实现"继承". 今天是最后一个部分,介绍不使用构造函数实现"继承". 一.什么是 ...
- 3-23Agile Web Development,3-24(chapter: 6)
第2章 Instant Gratification 复习 和 练习第一章,新建rails web页面. 重点: 知道了类,方法,实例变量 在rails是怎么用的. rails generate con ...
- 安装 tensorflow 时遇到 OSError: [Errno 1] Operation not permitted 的解决办法
Installing collected packages: numpy, scipy, six, pyyaml, Keras, opencv-python, h5py, html5lib, blea ...
- Windows 环境下安装 Oracle JDK
本页面中描述了如何在 Window 环境下安装 Oracle JDK. 我们使用的版本是 Window 10,我们需要安装的版本是 Oracle JDK 8u191. 检查当前版本 在进行新的 JDK ...
- iosFQ教程
https://www.youtube.com/watch?v=B8Vu3Xrivsc + https://sobaigu.com/how-to-use-shadowrocket-ios.html
- Oracle EBS R12 客户表结构
参考链接: Oracle EBS R12 客户表结构 Oracle EBS中的“客户”."客户地点".‘订单’之间的关系 Oracle EBS中的“客户”."客户地点&q ...
- RpcContext
RpcContext内部有一个ThreadLocal变量,它是作为ThreadLocalMap的key,表明每个线程有一个RpcContext. public class RpcContext { p ...
- zk maxClientCnxns参数
在zk模板配置文件中有: # the maximum number of client connections. # increase this if you need to handle more ...
- javascript开发HTML5游戏--斗地主(单机模式part1)
最近学习使用了一款HTML5游戏引擎(青瓷引擎),并用它尝试做了一个斗地主的游戏,简单实现了单机对战和网络对战,代码可已放到github上,在此谈谈自己如何通过引擎来开发这款游戏的. 客户端代码 ...