zoukankan      html  css  js  c++  java
  • [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
    ",ans % g);
    //  fclose(stdin);
    //  fclose(stdout);
        return 0;
    }
    

    矩阵法

    [left( left[ egin{matrix} a & c\ 0 & 1\ end{matrix} ight]^n imes left[ egin{matrix} X_0\ 1 end{matrix} ight] ight)_{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
    ",a[0][0] % g);
    //  fclose(stdin);
    //  fclose(stdout);
        return 0;
    }
    

    第二种方法比第一种方法快几毫秒。

    静渊以有谋,疏通而知事。
  • 相关阅读:
    laravel使用redis报错
    PHP新特性W3Cschool
    【python】跳过验证直接登陆-cookie已经知道需要部分直接注入
    【python】显示等待
    【python】pymysql库的简单使用
    【python】UI自动化优化浏览器重复打开和跑脚本时电脑无法做其他工作的问题
    【python】seleniumwire和selenium的区别
    【python】UI自动化-新版
    【python】UI自动化获取输入框的值
    【python】UI自动化多窗口处理
  • 原文地址:https://www.cnblogs.com/autoint/p/9748362.html
Copyright © 2011-2022 走看看