poj3070 单位矩阵(转移矩阵构造)+矩阵快速幂

太妙了。。通过矩阵乘法来加速递推

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define mod 10000
int n;
void mul(int f[],int a[2][2]){//一维数组和矩阵相乘
    int c[2]={};
    for(int i=0;i<2;i++)
        for(int j=0;j<2;j++)
            c[j]=(c[j]+(long long)f[i]*a[i][j])%mod;
    memcpy(f,c,sizeof c);
}
void mulself(int a[2][2]){//矩阵自乘
    int c[2][2]={};
    for(int i=0;i<2;i++)
        for(int j=0;j<2;j++)
            for(int k=0;k<2;k++)
                c[i][j]=(c[i][j]+(long long)a[i][k]*a[k][j])%mod;
    memcpy(a,c,sizeof c);
}

int main(){
    while(cin>>n && n>=0){
        int f[2]={0,1},a[2][2]={{0,1},{1,1}};
        while(n){
            if(n%2)mul(f,a);
            mulself(a);
            n>>=1;
        }
        cout<<f[0]<<endl;
    }
}
/*
1 1 2 3 5 8 13 21 34
*/

原文地址:https://www.cnblogs.com/zsben991126/p/10525509.html

时间: 2024-08-01 14:10:29

poj3070 单位矩阵(转移矩阵构造)+矩阵快速幂的相关文章

hdu 4565 So Easy! (共轭构造+矩阵快速幂)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4565 题目大意: 给出a,b,n,m,求出的值, 解题思路: 因为题目中出现了开根号,和向上取整后求余,所以用矩阵快速幂加速求解过程的时候,会产生误差,就很自然地想到了凑数,因为(a-1)^2<b<a^2,得出0<a-sqrt(b)<1,则无论n取多大,(a-sqrt(b))^n都是小于1的,(a-sqrt(b))^n 与 (a+sqrt(b))^n共轭,两者展开后会相互抵销,所以(

poj 3070 Fibonacci 【矩阵快速幂 求第N个斐波那契数%1000】

Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11123   Accepted: 7913 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn ? 1 + Fn ? 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequenc

省选算法学习-矩阵与矩阵快速幂

0x00 引入 矩阵,顾名思义,就是由数构成的矩形阵列 比如这样的:$\begin{array}{l}\begin{bmatrix}2&3&4\0&7&13\c&\alpha&\sqrt5\end{bmatrix}\\end{array}$ 就是一个3*3的矩阵 矩阵在信息学乃至数学里面的用处都非常广泛,下面就来介绍下它的一些基本知识,以及常用的地方.本文同时还会介绍矩阵快速幂以及快速矩阵乘法. 0x01 何为矩阵 矩阵的定义 其实就是上面那样的啦.....

HUST 1569(Burnside定理+容斥+数位dp+矩阵快速幂)

传送门:Gift 题意:由n(n<=1e9)个珍珠构成的项链,珍珠包含幸运数字(有且仅由4或7组成),取区间[L,R]内的数字,相邻的数字不能相同,且旋转得到的相同的数列为一种,为最终能构成多少种项链. 分析:这是我做过的最为综合的一道题目(太渣了),首先数位dp筛选出区间[L,R]内的幸运数字总数,dp[pos]表示非限制条件下还有pos位含有的幸运数字个数,然后记忆化搜索一下,随便乱搞的(直接dfs不知会不会超时,本人做法900+ms险过,应该直接dfs会超时),再不考虑旋转相同的情况,可以

hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10); 所求的是f(x)取m的模,而x,m,a[0]至a[9]都是输入项 初拿到这道题,最开始想的一般是暴力枚举,通过for循环求出f(x)然后再取模,但是有两个问题,首先f(x)可能特别大,其

【构造共轭函数+矩阵快速幂】HDU 4565 So Easy! (2013 长沙赛区邀请赛)

链接 :click here~~ 题意: A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate Sn. You, a top coder, say: So easy! [解题思路] 给一张神图,推理写的灰常明白了,关键是构造共轭函数,这一点实在是要有数学知识的理论基础,推出了递推式,接下

[POJ 3735] Training little cats (构造矩阵、矩阵快速幂)

Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9613   Accepted: 2296 Description Facer's pet cat just gave birth to a brood of little cats. Having considered the health of those lovely cats, Facer decides to make th

poj 3735 Training little cats(矩阵构造,快速幂)

Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10350   Accepted: 2471 Description Facer's pet cat just gave birth to a brood of little cats. Having considered the health of those lovely cats, Facer decides to make t

POJ3070 Fibonacci(矩阵快速幂)

用矩阵快速幂求fibonacci序列的第n项. /* *********************************************** Author :devil Created Time :2016/1/19 20:11:43 ************************************************ */ #include <iostream> #include <algorithm> #include <cstring> #in

FZU 1911 Construct a Matrix ( 矩阵快速幂 + 构造 )

FZU 1911 Construct a Matrix (  矩阵快速幂 + 构造 ) 题意: 需要构造一个矩阵满足如下要求: 1.矩阵是一个S(N)*S(N)的方阵 2.S(N)代表斐波那契数列的前N项和模上M 3.矩阵只能由1, 0, -1组成 4.矩阵每行每列的和不能相等 Here, the Fibonacci numbers are the numbers in the following sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 1