poj 3070 && nyoj 148 矩阵快速幂

题目链接

poj: http://poj.org/problem?id=3070

nyoj: http://acm.nyist.net/JudgeOnline/problem.php?pid=148

思路:

  • 矩阵快速幂
  • 直接求取

代码:

#include <iostream>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int mod = 10000;
struct mat {
int a[2][2];
mat() {
a[0][1]=a[1][0]=1;
a[1][1]=a[0][0]=0;
}
};
mat qpow(mat& x, mat& y) {
mat z;
z.a[0][1]=z.a[1][0]=0;
for(int i=0;i<=1;++i) {
for(int j=0;j<=1;++j) {
for(int k=0;k<=1;++k) {
z.a[i][j]=(z.a[i][j]%mod+(x.a[i][k]%mod*y.a[k][j])%mod)%mod;
}
}
}
return z;
}
int slove(int n) {
mat x,res;
res.a[0][1]=res.a[1][0]=0;
res.a[1][1]=res.a[0][0]=1;
x.a[0][0]=1;
while(n) {
if(n&1) res=qpow(res,x);//二进制分解
x=qpow(x,x);
n>>=1;
}
return res.a[0][1];
}
int main() {
int n;
while(~scanf("%d",&n)&&n!=-1) {
printf("%d\n",slove(n));
}
return 0;
}

poj 3070 && nyoj 148 矩阵快速幂的更多相关文章

  1. poj 3070 Fibonacci(矩阵快速幂,简单)

    题目 还是一道基础的矩阵快速幂. 具体的居者的幂公式我就不明示了. #include<stdio.h> #include<string.h> #include<algor ...

  2. POJ 3070 Fibonacci(矩阵快速幂)

    题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. #include <iostream> #include <cstring> #includ ...

  3. POJ 3070 Fibonacci 【矩阵快速幂】

    <题目链接> Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...

  4. DNA Sequence POJ - 2778 AC自动机 && 矩阵快速幂

    It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's very useful to ...

  5. poj 2778 AC自动机+矩阵快速幂

    题目链接:https://vjudge.net/problem/POJ-2778 题意:输入n和m表示n个病毒,和一个长为m的字符串,里面只可以有'A','C','G','T' 这四个字符,现在问这个 ...

  6. Scout YYF I POJ - 3744(概率dp + 矩阵快速幂)

    题意: 一条路上有n个地雷,你从1开始走,单位时间内有p的概率走一步,1-p的概率走两步,问安全通过这条路的概率 解析: 很容易想到 dp[i] = p * dp[i-1] + (1 - p) * d ...

  7. POJ 3734 Blocks (矩阵快速幂)

    题目链接 Description Panda has received an assignment of painting a line of blocks. Since Panda is such ...

  8. POJ 3734 Blocks(矩阵快速幂+矩阵递推式)

    题意:个n个方块涂色, 只能涂红黄蓝绿四种颜色,求最终红色和绿色都为偶数的方案数. 该题我们可以想到一个递推式 .   设a[i]表示到第i个方块为止红绿是偶数的方案数, b[i]为红绿恰有一个是偶数 ...

  9. POJ 2778 DNA Sequence (矩阵快速幂 + AC自动鸡)

    题目:传送门 题意: 给你m个病毒串,只由(A.G.T.C) 组成, 问你生成一个长度为 n 的 只由 A.C.T.G 构成的,不包含病毒串的序列的方案数. 解: 对 m 个病毒串,建 AC 自动机, ...

随机推荐

  1. 做直线不要使用hr

    需要横线.竖线时不要使用hr标签来做,容易出问题,可以使用高度或宽度为1px并加上背景色的div.p代替.

  2. window下eclipse4.5+hadoop2.6.1开发环境配置

    1.准备: eclipse4.5,hadoop2.6.1,hadoop-eclipse-plugin-2.6.0.jar. 2.eclipse配置 eclipse->windows->Pr ...

  3. continous integration environment (Jenkins and bitbucket configuration)

    ================================================================================ continous integrati ...

  4. Linux环境下网卡配置

    DEVICE=eth0 HWADDR=08:00:27:0D:3C:F6 TYPE=Ethernet UUID=73ff4482-1baf-4c9b-b859-720ca92a704a ONBOOT= ...

  5. King

    King Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissi ...

  6. Mysql 删除重复记录,只保留最小的一条

    delete from `jb_postcontent` where id not in(select min(id) from (select * from `jb_postcontent`) as ...

  7. selenim之ActionChains(一)

    大家好,来介绍下,今天要分享的是小编学ActionChains的经验. 先来说一下今天要用到的方法: click(element=null)                               ...

  8. Python之元类

    类型对象负责创建对象实例,控制对象行为.那么类型对象又由谁来创建呢? 元类(metaclass)——类型的类型 New-Style Class的默认类型是type >>> class ...

  9. MySQL5.6新特性

    Innodb加强项 1.Innodb全文索引 mysql> show create table film_text\G . row *************************** Tab ...

  10. riot.js教程【六】循环、HTML元素标签

    前文回顾 riot.js教程[五]标签嵌套.命名元素.事件.标签条件 riot.js教程[四]Mixins.HTML内嵌表达式 riot.js教程[三]访问DOM元素.使用jquery.mount输入 ...