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 ...
随机推荐
- LINQ 系列
C#图解教程 第十九章 LINQ LINQ 什么是LINQLINQ提供程序 匿名类型 方法语法和查询语法查询变量查询表达式的结构 from子句join子句什么是联结查询主体中的from…let…w ...
- poj1651 Multiplication Puzzle
比较特别的区间dp.小的区间转移大的区间时,也要枚举断点.不过和普通的区间dp比,断点有特殊意义.表示断点是区间最后取走的点.而且一个区间表示两端都不取走时中间取走的最小花费. #include &l ...
- AIM Tech Round 3 (Div. 1) (构造,树形dp,费用流,概率dp)
B. Recover the String 大意: 求构造01字符串使得子序列00,01,10,11的个数恰好为$a_{00},a_{01},a_{10},a_{11}$ 挺简单的构造, 注意到可以通 ...
- Yet Another Ball Problem CodeForces - 1118E (简单构造)
大意: 求构造n个pair, 每个pair满足 对于每k组, 让$b_i$为$[1,k]$, $g_i$循环右移就好了 int n, k, cnt; int main() { scanf(" ...
- JAVA System.arraycopy 和Arrays.copyof 效率比较
System.arraycopy()源码.可以看到是native方法: native关键字说明其修饰的方法是一个原生态方法,方法对应的实现不是在当前文件,而是在用其他语言(如C和C++)实现的文件中. ...
- Eclipse详细设置护眼背景色和字体颜色
代码区背景色: 参考地址: http://jingyan.baidu.com/article/d5a880eb6c4f7813f147ccef.html Package explorer 颜色 : ...
- vue input框数字后保留两位小数正则
<el-input type="text" v-model.trim="ruleForm2.marketPrice" maxlength="10 ...
- 桥接、nat、host-only
- en_e outtest2
e 1◆ e i: ə ɜː e I 2◆ ei ey ei i: 3◆ eer ɪə 4◆ ee i: 5◆ er ə 6◆ ere ɪə eə 7◆ ea ɪə i: ə ...
- this常见错误
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...