题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1126

存在参数a,b为负数的情况。这时候要这么处理:

根据mod值(7)加至刚好大于0。

否则有些样例是过不去的。

 #include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; const int maxn = ;
typedef struct Matrix {
int m[maxn][maxn];
int r;
int c;
Matrix(){
r = c = ;
memset(m, , sizeof(m));
}
} Matrix; Matrix mul(Matrix m1, Matrix m2, int mod) {
Matrix ans = Matrix();
ans.r = m1.r;
ans.c = m2.c;
for(int i = ; i <= m1.r; i++) {
for(int j = ; j <= m2.r; j++) {
for(int k = ; k <= m2.c; k++) {
if(m2.m[j][k] == ) continue;
ans.m[i][k] = ((ans.m[i][k] + m1.m[i][j] * m2.m[j][k] % mod) % mod) % mod;
}
}
}
return ans;
} Matrix quickmul(Matrix m, int n, int mod) {
Matrix ans = Matrix();
for(int i = ; i <= m.r; i++) {
ans.m[i][i] = ;
}
ans.r = m.r;
ans.c = m.c;
while(n) {
if(n & ) {
ans = mul(m, ans, mod);
}
m = mul(m, m, mod);
n >>= ;
}
return ans;
} int a, b, n, m; int main() {
// freopen("in", "r", stdin);
m = ;
while(~scanf("%d %d %d", &a, &b, &n)) {
Matrix p;
while(a < ) a += m;
while(b < ) b += m;
p.r = , p.c = ;
p.m[][] = ;
p.m[][] = ;
if(n <= ) {
printf("%d\n", p.m[-n+][] % m);
continue;
}
Matrix s;
s.r = s.c = ;
s.m[][] = a, s.m[][] = b;
s.m[][] = , s.m[][] = ;
s = quickmul(s, n-, m);
int ans = ;
for(int i = ; i <= p.r; i++) {
ans = (ans + (p.m[i][] * s.m[][i]) % m) % m;
}
printf("%d\n", ans % m);
}
return ;
}

[51NOD1126]求递推序列的第n项(矩阵快速幂)的更多相关文章

  1. 51nod1126 求递推序列的第N项

    求递推序列的第N项 有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的 ...

  2. 51nod1126 求递推序列的第N项【递推】

    有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...

  3. [51nod 1126] 求递推序列的第N项 - 矩阵乘法

    #include <bits/stdc++.h> using namespace std; #define int long long const int mod = 7; struct ...

  4. 51nod 1126 求递推序列的第N项

    1126 求递推序列的第N项  基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 有一个序列是这样定义的:f(1) = 1, f(2) = 1, f( ...

  5. 51nod 1126 求递推序列的第N项 思路:递推模拟,求循环节。详细注释

    题目: 看起来比较难,范围10^9 O(n)都过不了,但是仅仅是看起来.(虽然我WA了7次 TLE了3次,被自己蠢哭) 我们观察到 0 <= f[i] <= 6 就简单了,就像小学初中学的 ...

  6. 51nod 1126 - 求递推序列的第N项 - [找规律]

    题目链接:https://cn.vjudge.net/problem/51Nod-1126 有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + ...

  7. 515Nod 1126 求递推序列的第n项【矩阵快速幂】

    有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...

  8. 51Nod 1126 求递推序列的第N项(矩阵快速幂)

    #include <iostream> #include <algorithm> #include <cmath> #define MOD 7 #define N ...

  9. 51nod 1126 求递推序列的第N项 && hdu - 1005 Number Sequence (求周期)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1126 http://acm.hdu.edu.cn/showproblem ...

随机推荐

  1. 深入理解ThreadLocal(一)

    Android里,在不同的线程(假设子线程已经创建了Looper)中创建Handler时,并不需要显式指定Looper,系统能自动找到该线程自己的Looper.不同线程的Looper相互独立,之所以能 ...

  2. final ,override关键字

    final 有时我们会定义这样一种类,我们不希望其他类继承它,或者不想考虑它是否适合作为一个基类.为了实现这一目的,c++ 11新标准提供了一种防止继承发生的方法,即在类名后跟一个关键字final: ...

  3. 微软职位内部推荐-Software Development Engineer II

    微软近期Open的职位: Job Title:Software Development EngineerII Division: Server & Tools Business - Comme ...

  4. VBS基础篇 - 常量

    常量:指的是在程序运行过程中其值保持不变的量,它用来保存固定不变的数值,字符串等常数 . 常量的定义:在vbscript中使用使用 Const 指令可以创建名称具有一定含义的字符串型或数值型常量,并给 ...

  5. 解决Ubuntu开机自动挂载硬盘回收站不可用等权限问题

    1.修改fstab sudo gedit /etc/fstab 2.添加如下代码 #Entry for /dev/sdb7 : UUID=78A675EB46D703C4 /media/anseey/ ...

  6. Code First 中的 TPH TPT TPC

    public class Blog { public int Id { get; set; } public DateTime Creationdate { get; set; } public st ...

  7. [nowCoder] 局部最小值位置

    定义局部最小的概念.arr长度为1时,arr[0]是局部最小.arr的长度为N(N>1)时,如果arr[0]<arr[1],那么arr[0]是局部最小:如果arr[N-1]<arr[ ...

  8. XML 实体扩展攻击

    XMl Entity Expansion(攻击)某种程度上类似于 XML Entity Expansion,但是它主要试图通过消耗目标程序的服务器环境来进行DOS攻击的.这种攻击基于XML Entit ...

  9. POJ 2568/ZOJ 1965 Decode the Tree

    题意:在树中,每次删去节点值最小的叶子结点. 每删去一个点,就给出与这相连的点的值,直到最后只剩下一个根结点,给这N-1个数,重新建立这个树. 思路: 给出的节点号按次序存入到数组a中,将未给出的数存 ...

  10. mybatis 插入日期类型精确到秒的有关问题

    mybatis 插入日期类型精确到秒的问题 Mybatis 插入 数据库是为了防止插入空时报错, Mybatis 提供了一套机制,只要给定插入的字段的类型,如果为空,则它会自动处理为相应类型的默认值: ...