HDU 4291 A Short problem 又是一道神奇的矩阵

首先要知道一个姿势,对于Fib数列这类的东西,只要取余就一定会出现循环节。所以上来就直接暴力打表找规律就好了。

MOD = 1000000007 发现循环节是 222222224。

MOD = 2222222227 发现循环节是 183120

然后这个问题就解决了。

不要问我为啥会出现循环节,我也不会证明。。。

----------------------------------分割线----------------------------------

我好像会证明了,试着证一发。

设有一个递推公式 f(n) = (f(n-1)+f(n-2))%MOD。

那么f(n) 的取值范围为[0,MOD-1],一共MOD个数,即一共MOD×MOD种组合,且一旦递推公式确定,这MOD个数的排列方式就确定了,所以一定存在循环节。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map>

#pragma comment(linker, "/STACK:1024000000")
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f

using namespace std;

const int MAXN = 5;

struct MAT
{
    int row,col;
    LL mat[MAXN][MAXN];

    void Init(int R,int C,int val)
    {
        row = R,col = C;

        for(int i = 1;i <= row; ++i)
            for(int j = 1;j <= col; ++j)
                    mat[i][j] = (i == j ? val : 0);
    }

    MAT Multi(MAT c,LL MOD)
    {
        MAT tmp;
        tmp.Init(this->row,c.col,0);

        int i,j,k;
       for(k = 1;k <= this->col; ++k)
            for(i = 1;i <= tmp.row; ++i)
                for(j = 1;j <= tmp.col; ++j)
                    (tmp.mat[i][j] += (this->mat[i][k]*c.mat[k][j])%MOD)%=MOD;

        return tmp;
    }

    MAT Quick(LL n,LL MOD)
    {
        MAT res,tmp = *this;

        res.Init(row,col,1);

        while(n)
        {
            if(n&1)
                res = res.Multi(tmp,MOD);
            tmp = tmp.Multi(tmp,MOD);
            n >>= 1;
        }

        return res;
    }

    void Output()
    {
        cout<<"         ****************        "<<endl;
        int i,j;
        for(i = 1;i <= row; ++i)
        {
            for(j = 1;j <= col; ++j)
                    printf("%3d ",mat[i][j]);
            puts("");
        }
        cout<<"         &&&&&&&&&&&&&       "<<endl;
    }

};

int main()
{
    const int M0 = 183120;
    const int M1 = 222222224;
    const int M2 = 1000000007;

    LL n;

    MAT A,B,tmp;

    A.Init(2,2,0);
    A.mat[1][1] = 0;
    A.mat[1][2] = 1;
    A.mat[2][1] = 1;
    A.mat[2][2] = 3;

    B.Init(2,1,0);
    B.mat[1][1] = 0;
    B.mat[2][1] = 1;

    while(cin>>n)
    {
        n = A.Quick(n,M0).Multi(B,M0).mat[1][1];

        n = A.Quick(n,M1).Multi(B,M1).mat[1][1];

        n = A.Quick(n,M2).Multi(B,M2).mat[1][1];

        cout<<n<<endl;

    }
    return 0;
}
时间: 2024-12-23 04:26:07

HDU 4291 A Short problem 又是一道神奇的矩阵的相关文章

HDU 4291 A Short problem

A Short problem Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 429164-bit integer IO format: %I64d      Java class name: Main According to a research, VIM users tend to have shorter fingers, compared with Em

HDU 4291 A Short problem(矩阵+循环节)

A Short problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2711    Accepted Submission(s): 951 Problem Description According to a research, VIM users tend to have shorter fingers, compared

hdu 4291 A Short problem(矩阵+取模循环节)

A Short problem                                                          Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1785    Accepted Submission(s): 651 Problem Description According to a r

HDU 4291 A Short problem(矩阵快速幂+循环节)

题目链接": http://acm.hdu.edu.cn/showproblem.php?pid=4291 题意: g(0)=0,g(1)=1; g(n) = 3g(n - 1) + g(n - 2): 求g(g(g(n))) mod 109 + 7 分析: 首先我们得认识到,如果一层一层算是必定会超时的. 其次,取模运算是有循环节的. step1我们找出g(x)%1000000007的循环节 mod1 step2 设g(g(n)) = g(x) x=g(n) 对mod1 取模得到mod2. 剩

HDU 4291 A Short problem (2012成都网络赛,矩阵快速幂+循环节)

链接: click here~~ 题意: According to a research, VIM users tend to have shorter fingers, compared with Emacs users. Hence they prefer problems short, too. Here is a short one: Given n (1 <= n <= 1018), You should solve for g(g(g(n))) mod 109 + 7 where

HDU 4291 A Short problem 短问题 (递推,微变型)

题意:给出递推式 g(n) = 3g(n - 1) + g(n - 2),且g(1) = 1,g(0) = 0.求g( g( g(n))) mod 109 + 7. 思路:要求的g( g( g(n)))一共里外3层.看到时间限制1s,数据最大10^18,必定不能老实递推,要么有循环,要么用通项公式.这里用通项公式太麻烦了,数字不好算,而g(n)%109 + 7是有规律的, 在n=222222224之后会出现循环,也就是n=0和n=222222224的g(n)是一样的,这是最外层.那么也就是说在g

HDU——4291A Short problem(矩阵快速幂+循环节)

A Short problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2461    Accepted Submission(s): 864 Problem Description According to a research, VIM users tend to have shorter fingers, compared

hdu 4291 矩阵幂 循环节

http://acm.hdu.edu.cn/showproblem.php?pid=4291 凡是取模的都有循环节-----常数有,矩阵也有,而且矩阵的更神奇: g(g(g(n))) mod 109 + 7  最外层MOD=1e9+7  可以算出g(g(n))的循环节222222224,进而算出g(n)的循环节183120LL,然后由内而外计算即可 注释掉的是求循环节的代码 //#pragma comment(linker, "/STACK:102400000,102400000")

【dfs】hdu 1016 Prime Ring Problem

[dfs]hdu 1016 Prime Ring Problem 题目链接 刚开始接触搜索,先来一道基本题目练练手. 注意对树的深度进行dfs dfs过程中注意回退!!! 素数提前打表判断快一些 参考代码 /*Author:Hacker_vision*/ #include<bits/stdc++.h> #define clr(k,v) memset(k,v,sizeof(k)) using namespace std; const int _max=1e3+10;//素数打表 int n,pr