Codevs No.1281 Xn数列
2016-06-01 16:28:25
题目链接: Xn数列 (Codevs No.1281)
题目大意:
给定一种递推式为 Xn=(A*Xn-1+C)%M 的数列,求特定的某一项%G
解法:
矩阵乘法
不会的去看看高中矩阵的那本选修,起码知道都是啥意思,好理解得多
矩阵构造: 向量构造:
A C X0
0 1 1
需要注意的地方:
1.超大整数乘法,写个快速乘,防止爆longlong
2.函数的代值类型千万别错了啊,一个longlong打成int爆了一个多小时
//Xn数列 (Codevs No.1281)
//矩阵乘法
#include<stdio.h>
#include<algorithm>
using namespace std;
long long a[][];
long long b[][];
long long c[][];
long long M,A,C,X,N,G;
long long ans;
long long mX(long long x, long long y)
{
long long s=,k=;
while(y>)
{
if(y&)s=(s+x)%M;
x=(x<<)%M;
y=y>>;
}
return s;
}
void Multi1()
{
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
c[i][j]=;
for(int k=;k<=;k++)
{
c[i][j]=(c[i][j]%M+mX(a[i][k],b[k][j])%M)%M;
}
}
}
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
a[i][j]=c[i][j];
}
}
return ;
}
void Multi2()
{
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
c[i][j]=;
for(int k=;k<=;k++)
{
c[i][j]=(c[i][j]%M+mX(b[i][k],b[k][j])%M)%M;
}
}
}
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
b[i][j]=c[i][j];
}
}
return ;
}
void Bpow(long long x)
{
while(x)
{
if(x&)Multi1();
Multi2();
x>>=;
}
return ;
}
int main()
{
scanf("%lld %lld %lld %lld %lld %lld",&M,&A,&C,&X,&N,&G);
a[][]=b[][]=;
a[][]=b[][]=A%M;
a[][]=b[][]=C%M;
Bpow(N-);
ans=(mX(a[][],X)+a[][])%M;
printf("%lld",ans%G);
}
Codevs No.1281 Xn数列的更多相关文章
- 【CODEVS】1281 Xn数列
[算法]矩阵快速幂 [题解]T*A(n-1)=A(n)矩阵如下: a 1 * x(n-1) 0 = xn 0 0 1 c 0 c 0 防止溢出可以用类似快速幂的快速乘. ...
- [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 输入 ...
- codevs 1281 Xn数列 (矩阵乘法)
/* 再来个题练练手 scanf longlong 有bug....... */ #include<cstdio> #include<iostream> #include< ...
- 【wikioi】1281 Xn数列(矩阵乘法)
http://wikioi.com/problem/1281/ 矩阵真是个神奇的东西.. 只要搞出一个矩阵乘法,那么递推式可以完美的用上快速幂,然后使复杂度降到log 真是神奇. 在本题中,应该很快能 ...
- C++之路进阶——codevs1281(Xn数列)
1281 Xn数列 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你6个数,m, a, c, x0, n, ...
- 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 输入 ...
- codevs1281 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数列
题目描述 Description 给你6个数,m, a, c, x0, n, g Xn+1 = ( aXn + c ) mod m,求Xn m, a, c, x0, n, g<=10^18 输 ...
随机推荐
- 40. Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- redhat 新装后不能联网
1.ifconfig查看是否有ip地址,如果没有就配置,命令如下: [root@redhat ~]# system-config-network 设置DHCP 为 [*] [ok]后,重新ifconf ...
- sonar runner 2.4
https://www.versioneye.com/java/org.codehaus.sonar.runner:sonar-runner-dist/2.4
- Hadoop HDFS文件常用操作及注意事项(更新)
1.Copy a file from the local file system to HDFS The srcFile variable needs to contain the full name ...
- SimpleDateFormat日期格式化
public class T { /** * @param args */ public static void main(String[] args) { // TODO Auto-generate ...
- cocos2dx开发笔记
1.帧动画:SpriteTest=>SpriteAnimationSplit 2.sourceinsight显示代码行 option->document option->editin ...
- 25-语言入门-25-n-1位数
题目地址: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=96 描述已知w是一个大于10但不大于1000000的无符号整数,若w是n(n ...
- C#中默认的修饰符
参考自Default visibility for C# classes and members (fields, methods, etc)? Classes and structs that ar ...
- LinuxShell算术运算
Bash shell 的算术运算有四种方式:1:使用 expr 外部程式 加法 r=`expr 4 + 5`echo $r注意! '4' '+' '5' 这三者之间要有空白r=`expr 4 * 5` ...
- Catalan数推导(转载)
Raney引理: 设整数序列A = {Ai, i=1, 2, …, N},且部分和Sk=A1+…+Ak,序列中所有的数字的和SN=1,在A的N个循环表示中,有且仅有一个序列B,满足B的任意部分和Si均 ...