Description

 给你6个数,m, a, c, x0, n, g

Xn+1 = ( aXn + c ) mod m,求Xn

m, a, c, x0, n, g<=10^18

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
 
构造矩阵
转移矩阵
|a0 0|  
|1  1|  
初始矩阵
|x0 C|
 #include<cstdio>
#define ll long long
ll z[][],a[][];
ll m,b,c,x0,n,p;
ll plus(ll x,ll y,ll p){
ll s=;
while (x>=){
if (x&==) s=(s+y)%p;
x=x>>;
y=(y+y)%p;
}
return s;
} void mult1(ll a[][],ll b[][]){
ll c[][];
for (int i=;i<;i++) for (int j=;j<;j++) c[i][j]=;
for (int i=;i<;i++)
for (int j=;j<;j++)
for (int k=;k<;k++)
c[i][j]=(c[i][j]+plus(a[i][k],b[k][j],p))%p;
for (int i=;i<;i++) for (int j=;j<;j++) a[i][j]=c[i][j];
} void mult2(ll a[][],ll b[][]){
ll c[][];
for (int i=;i<;i++) for (int j=;j<;j++) c[i][j]=;
for (int i=;i<;i++)
for (int j=;j<;j++)
for (int k=;k<;k++)
c[i][j]=(c[i][j]+plus(a[i][k],b[k][j],p))%p;
for (int i=;i<;i++) for (int j=;j<;j++) a[i][j]=c[i][j];
} void pow(ll z[][],ll n){
ll ans[][];
for (int i=;i<;i++) for (int j=;j<;j++) ans[i][j]=;
ans[][]=; ans[][]=;
while (n>){
if (n%==) mult1(ans,z);
n=n>>;
mult1(z,z);
}
for (int i=;i<;i++) for (int j=;j<;j++) z[i][j]=ans[i][j];
} int main(){
scanf("%lld%lld%lld%lld%lld%lld",&p,&b,&c,&x0,&n,&m);
z[][]=b;z[][]=;z[][]=;
a[][]=x0;a[][]=c;
pow(z,n);
mult2(a,z);
printf("%lld",a[][]%m);
}

【BZOJ 2875】 [Noi2012]随机数生成器的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. 【bzoj2875】 Noi2012—随机数生成器

    http://www.lydsy.com/JudgeOnline/problem.php?id=2875 (题目链接) 题意 求${X_{n}}$. Solution 矩乘板子,这里主要讲下会爆lon ...

随机推荐

  1. 去掉VC2010 编辑器里出现的红色波浪线

    在VC2010中浏览代码的时候就大片的红线看着不舒服 其实不关VS的事,原因在于visual assist.   在VAssistX菜单栏->Visual Assist X Options-&g ...

  2. [改善Java代码]多线程使用Vector或HashTable

    Vector是ArrayList的多线程版本,HashTable是HashMap的多线程版本,这些概念我 们都很清楚,也被前辈嘱咐过很多次,但我们经常会逃避使用Vector和HashTable,因为用 ...

  3. 【STL+模拟】UVa 506 - System Dependencies

    System Dependencies  Components of computer systems often have dependencies--other components that m ...

  4. Hashtable 和 HashMap 的比较

        Hashtable HashMap 并发操作 使用同步机制, 实际应用程序中,仅仅是Hashtable本身的同步并不能保证程序在并发操作下的正确性,需要高层次的并发保护. 下面的代码试图在ke ...

  5. Jersey(1.19.1) - Client API, Security with Http(s)URLConnection

    With Http(s)URLConnection The support for security, specifically HTTP authentication and/or cookie m ...

  6. Android Sqlite 使用 注意事项

    1.Sqlite 写操作 并不是线程安全的 1.在多进程或多线程中使用sqlite,同时操作同一个数据库的话,会导致异常抛出. 2.不同线程或实例化多个SqliteOpenhelper来操作同一个数据 ...

  7. 在win7下配置java编译环境变量

    今天刚接触java编程,环境的配置方法比较复杂.好记性不如烂笔头,发个文章记录一下吧. win7系统 Jdk版本1.6 用鼠标右击“我的电脑”->属性->高级->环境变量系统变量-& ...

  8. MVC 生成PDf表格并插入图片

    最近做的项目中有一个功能,将最终的个人信息生成PDF表格,并插入图片.对于没接触过的程序员来说回一片茫然,网上有多种生成PDf的方法,我给大家介绍一下我认为比较简单,好操作的一种. iTextShar ...

  9. tinyMCE自定义添加图片插件

    需求: 在富文本编辑器中插入图片,图片来自用户可以自己上传的图片库. 本来可以用比较恶心的方式,也就是直接用tinyMCE自带的插入图片插件来实现.恶心是因为这个图片插件需要用户填入图片的url. 想 ...

  10. reduce + Promise 顺序执行代码

    本文地址: http://www.cnblogs.com/jasonxuli/p/4398742.html 下午的太阳晒得昏昏沉沉,和上周五一样迷糊,看一段代码半天没看明白,刚才不知不觉眯了几分钟,醒 ...