zoukankan      html  css  js  c++  java
  • bzoj2875: [Noi2012]随机数生成器

    矩阵快速幂+快速乘即可。总调不出来,然后发现输出时a写成b了sadsadsad。

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define REP(i,s,t) for(int i=s;i<=t;i++)
    #define clr(x,c) memset(x,c,sizeof(x))
    #define ll long long
    ll read(){
    	ll x=0;char c=getchar();
    	while(!isdigit(c)) c=getchar();
    	while(isdigit(c)) x=x*10+c-'0',c=getchar();
    	return x;
    }
    
    ll mod,A,c,xx,n,g;
    ll qmul(ll x,ll y){
    	ll ans=0;
    	while(y){
    		if(y&1) ans=(ans+x)%mod;
    		y>>=1;x<<=1;x%=mod;
    	}
    	return ans;
    }
    
    struct matrix{
    	ll a[3][3];
    	matrix(){
    		clr(a,0);
    	}
    	matrix operator*(const matrix&o)const{
    	    matrix tmp;
    	    REP(i,1,2) REP(j,1,2) REP(k,1,2) tmp.a[i][j]=(qmul(a[i][k],o.a[k][j])+tmp.a[i][j])%mod;
    	    return tmp;
    	}
    }b,a;
    
    int main(){
    	mod=read(),A=read(),c=read(),xx=read(),n=read(),g=read();
    	b.a[1][1]=A;b.a[1][2]=0;b.a[2][1]=c;b.a[2][2]=1;
    	REP(i,1,2) a.a[i][i]=1;
    	while(n){
    		if(n&1) a=a*b;
    		b=b*b;n>>=1;
    	}
    	printf("%lld
    ",((qmul(xx,a.a[1][1])+a.a[2][1])%mod)%g);
    	return 0;
    }
    

      

                              [BZOJ2875][NOI2012]随机数生成器

    Description

    栋栋最近迷上了随机算法,而随机数生成是随机算法的基础。栋栋准备使用线性同余法(Linear Congruential Method)来生成一个随机数列,这种方法需要设置四个非负整数参数m, a, c, X0,按照下面的公式生成出一系列随机数<Xn>: 
           Xn+1  =  (aXn  +  c) mod m 
          mod m 表示前面的数除以m的余数。从这个式子可以看出,这个序列的下一个数总是由上一个数生成的。 
          用种方法生成的序列具有随机序列的性质,因此这种方法被广泛地使用,包括常用的C++和Pascal 的产生随机数的库函数使用的也是这种方法。 
          知道这样产生的序列具有良好的随机性,不过心急的他仍然想尽快知道Xn 是多少。由于栋栋需要的随机数是0, 1,…, g − 1 之间的,他需要将Xn除以g。取余得到他想要的数,即Xn mod g,你只需要告诉栋栋他想要的数Xn mod g 是多少就可以了。

    Input

    包含6个用空格分割的m,a,c,X0,n和g,其中a,c,X0是非负整数,m,n,g是正整数。

    Output

    输出一个数,即Xn mod g

    Sample Input

    11 8 7 1 5 3

    Sample Output

    2

    HINT

    1<=n,m,a,c,X0<=10^18,1<=g<=10^8

  • 相关阅读:
    day01-h1字体大小和文本居中
    js正则表达式中的
    js滚动分页原理
    在web.xml中设置全局编码
    C# 导出word 表格代码
    C# 创建单例
    Winform 异步调用2 时间
    Winform 异步调用
    c#中跨线程调用windows窗体控件
    C# 中的委托和事件
  • 原文地址:https://www.cnblogs.com/fighting-to-the-end/p/5699341.html
Copyright © 2011-2022 走看看