一道坑题,用来更新矩阵模板,WIKI OI 1281
2014-09-30 21:44
260 查看
1281 Xn数列
时间限制: 1 s空间限制: 128000 KB
题目等级 : 大师 Master
题目描述 Description
给你6个数,m, a, c, x0, n, g
Xn+1 = ( aXn + c ) mod m,求Xn
m, a, c, x0, n, g<=10^18
输入描述 Input Description
一行六个数 m, a, c, x0, n, g
输出描述 Output Description
输出一个数 Xn mod g
样例输入 Sample Input
11 8 7 1 5 3
样例输出 Sample Output
2
数据范围及提示 Data Size & Hint
int64按位相乘可以不要用高精度。
中文题,不解释,主要是数据很吓人,一开始完全没注意溢出的问题,直接跪在了第二个点?然后注意了一些取模细节,跪在了第十个点= = 最后发现乘法都会溢出,改成了俄罗斯农民乘法,以为A了结果发现跪在了第五个点= =,后面才注意到最后乘常数又是直接乘的,所以有数据可以把这种方法搞掉,最后带×的都用俄罗斯农民乘法算,就A了= =。。
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; long long mod,a,c,x0,n,g; struct juzhen{ long long m[2][2]; }; long long modMul(long long a, long long b) { long long res = 0LL; for (; b; b >>= 1) { if (b & 1) res = (res + a) % mod; a = (a + a) % mod; } return res; } juzhen mut(juzhen a,juzhen b) { juzhen c; for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { c.m[i][j]=0; for(int k=0;k<2;k++) { c.m[i][j] += modMul(a.m[i][k], b.m[k][j]); c.m[i][j] %= mod; } } } return c; } juzhen power(juzhen a,long long p) { juzhen ans; memset(ans.m,0,sizeof(ans.m)); for(int i=0;i<2;i++) { ans.m[i][i]=1; } while(p) { if(p&1) ans=mut(ans,a); a=mut(a,a); p>>=1; } return ans; } int main() { cin>>mod>>a>>c>>x0>>n>>g; juzhen ans; a %= mod; c %= mod; x0 %= mod; ans.m[0][0] = a; ans.m[0][1] = ans.m[1][1] = 1; ans.m[1][0] = 0; ans = power(ans,n); long long res = (modMul(ans.m[0][0], x0) + modMul(ans.m[0][1], c) ) % mod; cout << res % g << endl; return 0; }
相关文章推荐
- Poj 3233 矩阵快速幂,暑假训练专题中的某一道题目,矩阵快速幂的模板
- 【图灵杯 F】一道简单的递推题(矩阵快速幂,乘法模板)
- 海乐网DEDECMS企业模板不断更新中
- 矩阵快速幂模板(可以在二重循环或者三重循环的地方取模)
- 矩阵操作模板
- HDU 1754 B I Hate It 线段树 单点更新 区间最大值 模板
- P1939 【模板】矩阵加速(数列)
- Django1.10配置Jinja2模板(20161130更新)
- 更新完畢[轉]31個iPhone软件开发实例教程, 此系列教程幾乎全部使用Utility模板
- 一道母函数的模板题 (hdu 2082)
- thymeleaf模板部分知识(持续更新)
- 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bccced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中
- 【每天一道剑指offer】2:矩阵中的路径
- [模板]矩阵加速
- 线段数 (区间更新 区间查询 单点更新 单点查询) 模板
- 矩阵快速幂模板
- [置顶] Nobleman__ ACM 比赛模板 (C++ && Java)个人总结 (不断更新) (自用)
- js 简易模板引擎 , 持续更新。。。
- [置顶] NOIP复赛模板及技巧积累(不定期更新)
- 自己制作的autoitX的vbs脚本,用来自动配置金山毒霸2011局域网更新