http://www.lydsy.com/JudgeOnline/problem.php?id=2875

矩阵的话很容易看出来。。。。。我就不写了。太水了。

然后乘法longlong会溢出。。。那么我们用快速乘。。。就是将快速幂的乘法变成加法。。。这种很简单吧。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef unsigned long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } ll m, a, c, x0, n, g;
typedef ll mtx[2][2]; mtx t;
ll mul(ll a, ll b) {
ll ret=0;
while(b) {
if(b&1) ret=(ret+a)%m;
a=(a+a)%m;
b>>=1;
}
return ret;
}
void mtxmul(mtx a, mtx b, mtx c, int la, int lb, int lc) {
rep(i, la) rep(j, lc) {
t[i][j]=0;
rep(k, lb) t[i][j]=(t[i][j]+mul(a[i][k], b[k][j]))%m;
}
rep(i, la) rep(j, lc) c[i][j]=t[i][j];
} mtx ma, mb, mc;
int main() {
cin >> m >> a >> c >>x0 >> n >> g;
ma[0][0]=x0; ma[0][1]=1;
mb[0][0]=a; mb[0][1]=0;
mb[1][0]=c; mb[1][1]=1;
mc[0][0]=mc[1][1]=1;
while(n) {
if(n&1) mtxmul(mc, mb, mc, 2, 2, 2);
n>>=1;
mtxmul(mb, mb, mb, 2, 2, 2);
}
mtxmul(ma, mc, ma, 2, 2, 2);
printf("%llu\n", ma[0][0]%g);
return 0;
}

  


Description

 

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

 

Source

【BZOJ】2875: [Noi2012]随机数生成器(矩阵乘法+快速乘)的更多相关文章

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

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

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

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

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

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

  4. [vijos1725&bzoj2875]随机数生成器<矩阵乘法&快速幂&快速乘>

    题目链接:https://vijos.org/p/1725 http://www.lydsy.com/JudgeOnline/problem.php?id=2875 这题是前几年的noi的题,时间比较 ...

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

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

  6. [NOI2012]随机数生成器 矩阵乘法

    Code: #include<cstdio> #include<algorithm> #include<iostream> #include<cstring& ...

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

    题面: 传送门 思路: 看一眼这个公式: $x\left[n+1\right]=\left(a\ast x\left[n\right]+c\right) mod m$ 递推,数据范围$n\leq 10 ...

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

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

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

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

随机推荐

  1. MD(d)、MT(d)编译选项的区别

    1.编译选项的位置 以VS2005为例,这样子打开: 1)         打开项目的Property Pages对话框 2)         点击左侧C/C++节 3)         点击Code ...

  2. HDU 4435 charge-station () bfs图论问题

    E - charge-station Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  3. MySQL5.5 RPM安装的默认安装路径

    MySQL5.5 RPM安装的默认安装路径 2011-06-20 10:34:32|  分类: MySQL|举报|字号 订阅   下载LOFTER客户端     由于一客户要求安装mysql- 5.5 ...

  4. php增加对mysqli的支持

    php增加对mysqli的支持   我在fedora下使用yum安装的php和mysql,但是发现php不支持myslqi,只能编译一个mysqli的扩展给php用了. 方法如下: 1.下载php 2 ...

  5. Dan计划:重新定义人生的10000个小时

    一. 1985年,芝加哥大学的Benjamin Bloom教授,出版了一本重要著作<如何培养天才>(Developing Talent in Young People). 他研究的是,如何 ...

  6. 【leetcode】Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  7. (转)Sublime Text 2 2.0.2 序列号

    ----- BEGIN LICENSE -----Andrew WeberSingle User LicenseEA7E-855605813A03DD 5E4AD9E6 6C0EEB94 BC9979 ...

  8. windows下批量删除文件

    FORFILES /P d:\www /D -7 /S /M ex*.log /C "cmd /c del @path" 删除d:\www目录下7天前ex*.log的所有文件 例子 ...

  9. 关于Xcode6 Segue 的疑问,没有解决!

    xcode6 的segue 变化了,如图 关于前3个选项,始终没有太明白,我试验结果如下,简单地把几个viewController连接起来时,无论用show,还是showdetail,还是Presen ...

  10. UML 序列图一点理解

    激活状态,就是长方形该画到哪里呢?我的理解是,一个长方形代表调用函数的执行过程,比如下图 这个长方形就表示viewDidAppear()函数没有执行完,在这个长方形上发出或者收到的消息都是在 view ...