题意

栋栋最近迷上了随机算法,而随机数是生成随机算法的基础。栋栋准备使用线性同余法(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(
\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]随机数生成器的更多相关文章

  1. 矩阵(快速幂):COGS 963. [NOI2012] 随机数生成器

    963. [NOI2012] 随机数生成器 ★★   输入文件:randoma.in   输出文件:randoma.out   简单对比 时间限制:1 s   内存限制:128 MB [问题描述] 栋 ...

  2. BZOJ 2875: [Noi2012]随机数生成器( 矩阵快速幂 )

    矩阵快速幂...+快速乘就OK了 ----------------------------------------------------------------------------------- ...

  3. Bzoj 2875: [Noi2012]随机数生成器(矩阵乘法)

    2875: [Noi2012]随机数生成器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 2052 Solved: 1118 Description ...

  4. [NOI2012]随机数生成器【矩阵快速幂】

    NOI2012 随机数生成器 题目描述 栋栋最近迷上了随机算法,而随机数是生成随机算法的基础.栋栋准备使用线性同余法(Linear Congruential Method)来生成一个随机数列,这种方法 ...

  5. BZOJ2875 & 洛谷2044:[NOI2012]随机数生成器——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=2875 https://www.luogu.org/problemnew/show/P2044 栋栋 ...

  6. BZOJ2875 [Noi2012]随机数生成器 【矩阵乘法 + 快速乘】

    题目 栋栋最近迷上了随机算法,而随机数是生成随机算法的基础.栋栋准备使用线性同余法(Linear Congruential Me thod)来生成一个随机数列,这种方法需要设置四个非负整数参数m,a, ...

  7. bzoj 2875: [Noi2012]随机数生成器

    #include<cstdio> #include<iostream> #include<cstring> #define ll long long using n ...

  8. 【BZOJ】2875: [Noi2012]随机数生成器(矩阵乘法+快速乘)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2875 矩阵的话很容易看出来.....我就不写了.太水了. 然后乘法longlong会溢出...那么我 ...

  9. 2875: [Noi2012]随机数生成器 - BZOJ

    DescriptionInput 包含6个用空格分割的m,a,c,X0,n和g,其中a,c,X0是非负整数,m,n,g是正整数. Output 输出一个数,即Xn mod gSample Input ...

随机推荐

  1. ZendFramework中实现自动加载models

    最近自学Zendframework中,写Controller的时候总要require model下的类文件,然后才能实例化,感觉非常不爽 Google了许久,找到个明白人写的方法不错,主要就是修改ap ...

  2. 合并两个dt

    C#代码中实现两个表(DataTable)的关联查询(JOIN)   之前通常都是使用SQL直接从数据库中取出表1和表2关联查询后的数据,只需要用一个JOIN就可以了,非常方便.近日遇到一种情况,两个 ...

  3. rabbitmq 消息的状态转换

    tutorial:http://www.rabbitmq.com/tutorials/tutorial-two-java.html 这里解释接收消息端关于 acknowledge和prefetch的设 ...

  4. English trip -- VC(情景课)4 D

    What do you see? I can see three men in the pictrue. one older man is a doctor, two younger men are ...

  5. python3 操作windows的粘贴板(读取和传值)

    #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = "loki" import win32con import wi ...

  6. Mass Change Queries CodeForces - 911G (线段树合并)

    链接 大意: 给定序列, 每次操作将区间[l,r]中的x全改为y, 最后输出序列 权值范围比较小, 对每个权值开一颗线段树, 每次将x合并到y上即可 #include <iostream> ...

  7. 『Pickle』数据结构持久化模块_常用方法记录

    可以把数据结构保存进文件并直接读出, 不论读取或者是保存,我们都需要借助open()函数,并且是二进制方式(‘wb’,‘rb’) json模块可以把字典结构改写为string然后保存,并可以反向读取字 ...

  8. python-day21--sys模块

    sys模块是与python解释器交互的一个接口 1.sys.argv           命令行参数List,第一个元素是程序本身路径     # 传参 应用场景:权限控制 2.sys.path    ...

  9. hdu-1404-博弈+打表

    Digital Deletions Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  10. 最小生成树 - 普里姆 - 边稠密 - O(N ^ 2)

    #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #define N 1005 #def ...