2875: [Noi2012]隨機數產生器

來源:互聯網
上載者:User

題目連結

題目大意:Xn+1 = (aXn + c) mod m ,求Xn mod g

題解:模板題,隨手構造一發矩陣

加了特技快速乘

注意矩乘的時候mod m,最後再mod g

我的收穫:233

#include <bits/stdc++.h>using namespace std;#define ll long long#define Rep(i,x) for(int i=0;i<x;i++)#define N 2ll a,m,n,g,c,x0;ll mul(ll x,ll y){    ll tmp=(x*y-(ll)((long double)x/m*y+1e-8)*m);    return tmp<0?tmp+m:tmp;}struct Matrix{    int x,y;ll v[N][N];    ll *operator [] (int x){return v[x];}    Matrix(int _x,int _y,int w=0){        x=_x,y=_y;        Rep(i,N) Rep(j,N) v[i][j]=(i==j)?w:0;    }};Matrix operator * (Matrix a,Matrix b){    Matrix c(a.x,b.y);    Rep(i,a.x) Rep(j,b.y) Rep(k,a.y) c[i][j]=(c[i][j]+mul(a[i][k],b[k][j]))%m;    return c;}Matrix operator ^ (Matrix a,ll b){    Matrix c(a.x,a.y,1);    for(;b;b>>=1,a=a*a)    if(b&1) c=c*a;    return c;}Matrix F(1,N),T(N,N);void work(){    T=T^n;F=F*T;    printf("%lld\n",F[0][0]%g);}void init(){    scanf("%lld%lld%lld%lld%lld%lld",&m,&a,&c,&x0,&n,&g);    F[0][0]=x0;F[0][1]=1;    T[0][0]=a;T[1][0]=c;T[0][1]=0;T[1][1]=1;}int main(){    init();    work();    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.