【题目大意】

已知Xn+1=(aXn+c) mod m,求Xn mod g。

【思路】

get到了longlong乘法的正确方法,快速乘。什么是快速乘呢?

简单来讲,快速幂就是模拟了二进制的竖式乘法。如:

10101 × 1011 = 10101*1+10101*2^1*1+10101*2^2*0+10101*2^3*1

代码如下:

long long multi(long long a,long long b,long long m) {
long long ans=; while(b) {
if(b&) (ans+=a) %= m;
(a=a*) %= m;
b/=;
} return ans;
}

接下来本题的方法就是据矩阵乘法的快速幂

(a 0

c 1)自乘n次即可

注意函数里也不要忘记了开longlong..一开始我函数里面的n写成了int,WA了一发。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll matrix[][],ans_matrix[][];
ll m,n,a,c,g,x0; ll ksc(ll a,ll b)
{
ll ans=;
while (b)
{
if (b&) ans=(ans+a)%m;
a=(a<<)%m;
b>>=;
}
return ans;
} void ksm(ll n)
{
ans_matrix[][]=ans_matrix[][]=;
ans_matrix[][]=ans_matrix[][]=;
while (n)
{
if (n&)
{
ans_matrix[][]=ksc(ans_matrix[][],matrix[][]);
ans_matrix[][]=(ksc(ans_matrix[][],matrix[][])+matrix[][])%m;
}
n>>=;
ll tmp1=ksc(matrix[][],matrix[][]);
ll tmp2=(ksc(matrix[][],matrix[][])+matrix[][])%m;
matrix[][]=tmp1,matrix[][]=tmp2;
}
} void init()
{
scanf("%lld%lld%lld%lld%lld%lld",&m,&a,&c,&x0,&n,&g);
matrix[][]=a%m,matrix[][]=,matrix[][]=c%m,matrix[][]=;
} void get_ans()
{
ll ans=(ksc(ans_matrix[][],x0)+ans_matrix[][])%m;
ans%=g;
printf("%lld",ans);
} int main()
{
//freopen("randoma.in","r",stdin);
//freopen("randoma.out","w",stdout);
init();
ksm(n);
get_ans();
return ;
}

【矩阵乘法+快速乘】BZOJ2875-[NOI2012]随机数生成器的更多相关文章

  1. BZOJ2875 [Noi2012]随机数生成器 【矩阵乘法 + 快速乘】

    题目 栋栋最近迷上了随机算法,而随机数是生成随机算法的基础.栋栋准备使用线性同余法(Linear Congruential Me thod)来生成一个随机数列,这种方法需要设置四个非负整数参数m,a, ...

  2. bzoj2875: [Noi2012]随机数生成器

    矩阵乘法. x[n] = {x[0],1} * ( {a,0} ^ n ) {b,1} 写成这样谁能看懂.... noi里的大水题.我居然 #include<cstdio> #includ ...

  3. [日常摸鱼]bzoj2875[NOI2012]随机数生成器-矩阵快速幂

    好裸的矩阵快速幂-然而我一开始居然构造不出矩阵- 平常两个的情况都是拿相邻两项放在矩阵里拿去递推的-然后我就一直构造不出来-其实把矩阵下面弄成1就好了啊orz #include<cstdio&g ...

  4. BZOJ-2875 随机数生成器 矩阵乘法快速幂+快速乘

    题目没给全,吃X了... 2875: [Noi2012]随机数生成器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 1479 Solved: 829 ...

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

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

  6. 【BZOJ2875】随机数生成器(矩阵快速幂)

    [BZOJ2875]随机数生成器(矩阵快速幂) 题面 Description 栋栋最近迷上了随机算法,而随机数是生成随机算法的基础.栋栋准备使用线性同余法(Linear Congruential Me ...

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

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

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

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

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

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

随机推荐

  1. Dilworth定理证明

    命题:偏序集能划分成的最少的全序集的个数与最大反链的元素个数相等. (离散数学结构第六版课本P245:把一个偏序集划分成具有全序的子集所需要的最少子集个数与元素在偏序下都是不可比的最大集合的基数之间有 ...

  2. [bzoj 3224]手写treap

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3224 bzoj不能用time(0),看到这个博客才知道,我也RE了好几发…… #inclu ...

  3. POJ1456:Supermarket(并查集+贪心)

    Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17634   Accepted: 7920 题目链接 ...

  4. HDU1859 最小长方形 (水

    最小长方形 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  5. ACM模板~求逆序对的个数

    #include <map> #include <set> #include <cmath> #include <ctime> #include < ...

  6. idea中mybatis-plugin破解

    Mybatis Plugin 一.Mybatis Plugin插件是什么 提供Mapper接口与配置文件中对应SQL的导航 编辑XML文件时自动补全 根据Mapper接口, 使用快捷键生成xml文件及 ...

  7. Install the AWS Command Line Interface on Linux

    Install the AWS Command Line Interface on Linux You can install the AWS Command Line Interface and i ...

  8. Linux 邮件服务器 之跟我一步一步来实现一个邮件系统【转】

    转自:http://tchuairen.blog.51cto.com/3848118/1686875/ 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法 ...

  9. Xcode升级到7之后 发现速度超级慢

    Xcode升级到7之后 发现速度超级慢 转自:http://www.jianshu.com/p/608803eb1e12 解决方法,慢google了一下是由于插件造成饿,于是乎将Alcatraz安装的 ...

  10. python数据类型-----字符串

    今天来总结下python3.4版本字符串的一些操作方法,对这些方法先作一个简单的分类,按照分类来进行总结. Sequence Typessequence类型有六种:strings, byte sequ ...