zoukankan      html  css  js  c++  java
  • 计蒜客——蒜头君的兔子-矩阵乘法

    蒜头君的小伙伴在 第一年 送给他一对 一岁 的兔子,并告诉他:这种兔子 刚生下来时算 000 岁,到了 222 岁时就可以繁殖了,它在 2−102-10210 岁时,每年会生下来一对兔子,这些兔子到了 222 岁也可以繁殖,但这些兔子在 101010 岁那年 生完仔后 不久就会死亡,蒜头君想知道,第 nnn 年兔子 产仔之后(第 nnn 年 101010 岁的兔子此时已经死亡),他会有多少对兔子。结果对 100000000710000000071000000007 取模。

    输入格式

    共一行,一个正整数 nnn,表示蒜头君想知道第 nnn 年的兔子总对数。

    输出格式

    输出一个整数,表示第 nnn 年兔子总对数对 100000000710000000071000000007 取模的值。

    数据规模

    对于 303030% 的数据,满足 1≤n≤1031 le n le 10^31n103​​;

    对于 606060% 的数据,满足 1≤n≤1051 le n le 10^51n105​​;

    对于 100100100% 的数据,满足 1≤n≤1091 le n le 10^91n109​​。

    样例输入1

    10

    样例输出1

    88

    样例输入2

    88

    样例输出2

    352138150

    样例输入3

    10086

    样例输出3

    405567313
    ————————————————————————————
    这道题模拟的话其实很方便但是只能拿部分分
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define LL long long
    using namespace std;
    const int inf=0x3f3f3f3f,mod=1e9+7;
    int read(){
        int ans=0,f=1,c=getchar();
        while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
        while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();}
        return ans*f;
    } 
    int n;
    int main()
    {
        n=read();
        f[1]=1;
        for(int i=1;i<n;i++){
            int sum=0;
            for(int k=10;k;k--) f[k]=f[k-1];
            for(int k=2;k<=10;k++) (sum+=f[k])%=mod;
            f[0]=sum;
        }
        int ans=0;
        for(int i=0;i<10;i++) (ans+=f[i])%=mod;
        printf("%d
    ",ans);
        return 0;
    }
    View Code

    但是 很明显的这是一道矩阵乘法能解决的问题——这看代码就应该很好懂辣

    不过因为我贪心写的是1-n-1 所以矩乘写的时候也有一定变化 

    写正常1-n的 矩阵C长得应该不一样哦 因为我是先计算长大了一岁 再算0岁兔子的

    QAQ为什么删不掉 算了重新搞一个
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define LL long long
    using namespace std;
    const int inf=0x3f3f3f3f,mod=1e9+7;
    int read(){
        int ans=0,f=1,c=getchar();
        while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
        while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();}
        return ans*f;
    } 
    int n;
    LL b[11][11],c[11][11],d[11][11];
    void pmod(LL b[11][11],LL c[11][11]){
        for(int i=0;i<=10;i++)
            for(int j=0;j<=10;j++) d[i][j]=0;
        for(int i=0;i<=10;i++)
            for(int k=0;k<=10;k++)
                for(int j=0;j<=10;j++) (d[i][j]+=b[i][k]*c[k][j])%=mod;
        for(int i=0;i<=10;i++)
            for(int j=0;j<=10;j++) b[i][j]=d[i][j];
    }
    void prepare(){
        b[1][1]=1;
        for(int i=1;i<=9;i++) c[i][0]=1;
        for(int i=1;i<=10;i++) c[i-1][i]=1;
    }
    int main()
    {
        n=read()-1;
        prepare();
        for(;n;n>>=1,pmod(c,c)) if(n&1) pmod(b,c);
        LL ans=0;
        for(int i=0;i<10;i++) (ans+=b[1][i])%=mod;
        printf("%lld
    ",ans);
        return 0;
    }
    View Code
  • 相关阅读:
    代理模式第一篇
    命令模式第二篇
    windows service 定时
    postman之asp.net webapi
    qq第三方登录
    core2.1下的identity 汉化
    asp.net core2.0里的Nlog
    Unable to create an object of type 'ApplicationDbContext'.
    identity使用mysql
    二进制位运算解决状态值
  • 原文地址:https://www.cnblogs.com/lyzuikeai/p/7403353.html
Copyright © 2011-2022 走看看