zoukankan      html  css  js  c++  java
  • 【CodeForces】166'E

    这里写图片描述166’E Tetrahedron

    You are given a tetrahedron. Let’s mark its vertices with letters A, B, C and D correspondingly.
    这里写图片描述
    An ant is standing in the vertex D of the tetrahedron. The ant is quite active and he wouldn’t stay idle. At each moment of time he makes a step from one vertex to another one along some edge of the tetrahedron. The ant just can’t stand on one place.

    You do not have to do much to solve the problem: your task is to count the number of ways in which the ant can go from the initial vertex D to itself in exactly n steps. In other words, you are asked to find out the number of different cyclic paths with the length of n from vertex D to itself. As the number can be quite large, you should print it modulo 1000000007 (109 + 7).

    Input
    The first line contains the only integer n (1 ≤ n ≤ 107) — the required length of the cyclic path.

    Output
    Print the only integer — the required number of ways modulo 1000000007 (109 + 7).

    Sample test(s)
    input
    2
    output
    3
    input
    4
    output
    21
    Note
    The required paths in the first sample are:

    D - A - D
    D - B - D
    D-C-D
    改编
    这里写图片描述

    题解

    递推+矩阵乘法

    代码

    //歪鸡劈
    //don't copy
    //or you'll 滚蛋
    //¥¥¥
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    #include<queue>
    #define LL long long 
    #define maxm 100010
    using namespace std;
    LL getint()
    {
        LL w=0,q=0;char ch=getchar();
        while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
        if(ch=='-')q=1,ch=getchar();
        while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=getchar();
        return q?-w:w;
    }
    
    long long a[5][5]={{0},{0,0,1,1,1},{0,1,0,1,1},{0,1,1,0,1},{0,1,1,1,0}},b[5][5]={{0},{0,1,0,0,0},{0,0,1,0,0},{0,0,0,1,0},{0,0,0,0,1}},n,c[5][5];
    void Maxtrixquick_pow(int k)
    {
        int i,j,kk;
        while(k>0)
        {
            if(k&1)
            {
                //b*=a; b=b*a
                for(i=1;i<=4;i++)
                {
                    for(j=1;j<=4;j++)
                    {
                        c[i][j]=0;
                        for(kk=1;kk<=4;kk++)
                            c[i][j]+=b[i][kk]*a[kk][j];
                    }
                }
                for(i=1;i<=4;i++)
                    for(j=1;j<=4;j++)
                        b[i][j]=c[i][j]%1000000007;
            }
            //a*=a;
            k>>=1;
            for(i=1;i<=4;i++)
            {
                for(j=1;j<=4;j++){
                    c[i][j]=0;
                    for(kk=1;kk<=4;kk++)
                        c[i][j]+=a[i][kk]*a[kk][j];
                }
            }
            for(i=1;i<=4;i++)
                for(j=1;j<=4;j++)
                    a[i][j]=c[i][j]%1000000007;
        }
    }
    int main()
    {
        freopen("nong.in","r",stdin);
        freopen("nong.out","w",stdout);
        n=getint();
        Maxtrixquick_pow(n);
        //printf("",b[1][1]);
        cout<<b[1][1];
        return 0;
    }
    View Code

      

  • 相关阅读:
    linux 更换golang版本
    ubuntu 搭建NFS
    golang 异步并发http轮询(爬虫)
    Mysql 事务锁等待时间超时
    排序算法之鸡尾酒排序
    Sql Server一个表向另一个表添加多条数据,关联时查询出一条数据
    Easyui datagrid 开始时间不能大于结束时间
    用python爬了上千万条招聘信息后,最终分析出python要学这些才能就业...
    用python把B站小姐姐跳舞视频爬下来,并打包成可以直接运行的exe文件
    女朋友股票亏惨了,我一怒之下用Python爬取了证券最新数据...
  • 原文地址:https://www.cnblogs.com/YJinpeng/p/5907211.html
Copyright © 2011-2022 走看看