codevs1281 Xn数列
给你6个数,m, a, c, x0, n, g
Xn+1 = ( aXn + c ) mod m,求Xn
m, a, c, x0, n, g<=10^18
一行六个数 m, a, c, x0, n, g
输出一个数 Xn mod g
11 8 7 1 5 3
2
int64按位相乘可以不要用高精度。
正解:矩阵快速幂+矩阵乘法
解题报告:
对于一个递推式,求某一项的值。
这显然是可以矩阵乘法的,用一下快速幂就可以了。但是注意一点,因为对于long long下的乘法很有可能会乘炸,所以我们需要用加法模拟一下乘法(快速幂的形式),就可以啦。
代码如下:
//It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
LL MOD,A,C,n,g;
struct matrix{
LL a[][];
}ans,init; inline LL getlong()
{
LL w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} inline LL quick_cj(LL x,LL y){//为防止溢出,用快速幂的方法,用加法代替乘法
LL da=;
while(y>) {
if(y&) da+=x,da%=MOD;
x+=x; x%=MOD;
y>>=;
}
return da;
} inline matrix cj(matrix a,matrix b){
matrix c=init;
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
c.a[i][j]+=quick_cj(a.a[i][k],b.a[k][j]),c.a[i][j]%=MOD;
return c;
} inline matrix mul(LL x){
matrix base,tmp; tmp=base=init;
tmp.a[][]=; tmp.a[][]=;
base.a[][]=A; base.a[][]=;
base.a[][]=base.a[][]=;
while(x>) {
if(x&) tmp=cj(tmp,base);
base=cj(base,base);
x>>=;
}
return tmp;
} inline void work(){
MOD=getlong(); A=getlong(); C=getlong();
ans.a[][]=getlong(); ans.a[][]=C; n=getlong(); g=getlong();
for(int i=;i<;i++) for(int j=;j<;j++) init.a[i][j]=;
ans=cj(ans,mul(n));
printf("%lld",ans.a[][]%g);
} int main()
{
work();
return ;
}
codevs1281 Xn数列的更多相关文章
- CODEVS1281 Xn数列 (矩阵乘法+快速乘)
真是道坑题,数据范围如此大. 首先构造矩阵 [ f[0] , 1] * [ a,0 ] ^n= [ f[n],1 ] [ c,1 ] 注意到m, a, c, x0, n, g<=10^18,所以 ...
- C++之路进阶——codevs1281(Xn数列)
1281 Xn数列 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你6个数,m, a, c, x0, n, ...
- Codevs No.1281 Xn数列
2016-06-01 16:28:25 题目链接: Xn数列 (Codevs No.1281) 题目大意: 给定一种递推式为 Xn=(A*Xn-1+C)%M 的数列,求特定的某一项%G 解法: 矩阵乘 ...
- [WikiOI "天梯"1281] Xn数列
题目描述Description 给你6个数,m, a, c, x0, n, g Xn+1 = ( aXn + c ) mod m,求Xn m, a, c, x0, n, g<=10^18 输入描 ...
- codevs 1281 Xn数列
题目描述 Description 给你6个数,m, a, c, x0, n, g Xn+1 = ( aXn + c ) mod m,求Xn m, a, c, x0, n, g<=10^18 输入 ...
- Xn数列(codevs 1281)
题目描述 Description 给你6个数,m, a, c, x0, n, g Xn+1 = ( aXn + c ) mod m,求Xn m, a, c, x0, n, g<=10^18 输入 ...
- 【wikioi】1281 Xn数列(矩阵乘法)
http://wikioi.com/problem/1281/ 矩阵真是个神奇的东西.. 只要搞出一个矩阵乘法,那么递推式可以完美的用上快速幂,然后使复杂度降到log 真是神奇. 在本题中,应该很快能 ...
- 【CODEVS】1281 Xn数列
[算法]矩阵快速幂 [题解]T*A(n-1)=A(n)矩阵如下: a 1 * x(n-1) 0 = xn 0 0 1 c 0 c 0 防止溢出可以用类似快速幂的快速乘. ...
- Xn数列
题目描述 Description 给你6个数,m, a, c, x0, n, g Xn+1 = ( aXn + c ) mod m,求Xn m, a, c, x0, n, g<=10^18 输 ...
随机推荐
- 经常使用socket函数具体解释
经常使用socket函数具体解释 关于socket函数,每一个的意义和基本功能都知道,但每次使用都会去百度,參数究竟是什么,返回值代表什么意义.就是说用的少,也记得不够精确. 每次都查半天.常常烦恼于 ...
- 【Cocos2dX(2.x)_Lua开发之三】
[Cocos2dX(2.x)_Lua开发之三]在Lua中使用自定义精灵(Lua脚本与自创建类之间的访问)及Lua基础讲解 本站文章均为李华明Himi原创,转载务必在明显处注明:(作者新浪微博:@李华明 ...
- Log4Net.Config配置信息《转》
看了log4net的简单使用之一_log4net介绍 大家对log4net组件应该有了大概的了解,下面再近一步介绍其在项目中如何应用. 1.Logger 所有的记录器都必须实现 ILog 接口,该接口 ...
- UVA 10131题解
第一次写动态规划的代码,整了一天,终于AC. 题目: Question 1: Is Bigger Smarter? The Problem Some people think that the big ...
- 【Python+selenium Wendriver API】之下拉框定位
上代码: # coding:utf-8 from selenium import webdriver from selenium.webdriver.common.action_chains impo ...
- px与与rem vw的区别
1.px 使用具体像素点为单位,好处是比较稳定和精确,但在浏览器放大缩小会出现问题 2.rem 参考根元素的值 例如设置根元素字体大小是20像素 在h1中设置字体大小 那么H1的大小就是40px p的 ...
- windows svchost.exe 引起的出现的莫名其妙的窗口失去焦点
我不知道你们遇到没,反正我是遇到了,现在我就把解决方法给你们,当然都是从网上整理下来的 这个失去焦点可以分为两种,一种是病毒,一种是系统自带的问题 首先你得知道自己的窗口被什么给挤掉了焦点 先看看这篇 ...
- Java -- 数字
@.运用BigDecimal处理Double类型的算术运算的精度问题 原文:https://blog.csdn.net/j754379117/article/details/52238396 可使用 ...
- POJ2396 Budget 【带下界的最大流】
Budget Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5962 Accepted: 2266 Special ...
- vue面试题,知识点汇总(有答案)
一. Vue核心小知识点 1.vue中 key 值的作用 key 的特殊属性主要用在 Vue的虚拟DOM算法,在新旧nodes对比时辨识VNodes.如果不使用key,Vue会使用一种最大限度减少动态 ...