• cf166e 在四面体上寻找路线数 递推,取模


       来源:codeforces                  E. Tetrahedron
     

    You are given a tetrahedron. Let's mark its vertices with letters ABC 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 Dto 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).

    Examples
    input
    Copy
    2
    output
    3
    input
    Copy
    4
    output
    21

    思路:
    递推ans[n]=ans[n-1]*2+ans[n-2]*3,但是ans会很大,需要取模
    取模后的ans可能已经不是ans了
    (ans[n-1]%mod*2+ans[n-2]*3)%mod==(ans[n-1]*2+ans[n-2]*3)%mod 是否成立?

    经过查阅

      1.(a*b) mod M=(a mod M)*(b mod M) mod M

      2.(a+b) mod M=(a mod M+b mod M) mod M;

    
    
    
     

    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        long long n,ans,a=0,b=3,c=6;
        cin>>n;
        if(n<4)
        {
            if(n==1)ans=a;
            else if(n==2)ans=b;
            else if(n==3)ans=c;
        }
        for(int i=4;i<=n;i++)
        {
            ans=(b*3+c*2)%1000000007;
            a=b;
            b=c;
            c=ans;
        }
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    关于智能本质的思考
    Effective C++ 条款39
    【视频教程】JEECG 入门视频教程大全+历史版本号代码下载
    HDU 4859(Bestcoder #1 1003)海岸线(网络流之最小割)
    最简单的基于FFMPEG的音频编码器(PCM编码为AAC)
    android经常使用的电话操作
    Vmware中为Mac Os安装vmtools
    win10.10 激活
    VM11安装Mac OS X 10.10
    win7系统升家庭版级为旗舰版的方法
  • 原文地址:https://www.cnblogs.com/carcar/p/8541203.html
Copyright © 2020-2023  润新知