Gym101845 A.Apple Trees

http://codeforces.com/group/NVaJtLaLjS/contest/241861/problem/A
In the beginning God created the heaven and the earth, the next five days God created the light, the water, the sky, the land, the trees and the living creatures, and on the sixth day he formed a man from the dust of the ground, called Adam. Afterwards, God took one of the man’s ribs and made a woman from the rib, called Eve. Adam and Eve lived in peace and harmony in the land, until God told them they should not take any apple from the only apple tree in Earth (at that moment, in year 0).

How they disobeyed God is a story from another day, now we are interested in how apple trees population increased over all those years. Apple trees are very interesting because their cycle of life is the following: first an apple fall into the ground and immediately a new apple tree blooms, from this moment we consider this as an apple tree, exactly ten years later the tree is full-grown and it starts to generate apples, from here every ten years the tree generates f(x) apples, where and x is the age of the tree, note that x will be always be a multiple of 10. Every apple generated from a tree will fall into the ground and generate a new tree, finally every apple tree dies exactly at the age of 45.

Now we want to know how many apple trees will be living on Earth after n years of the creation. At year 0 the apple tree created by God was not full-grown (this happened 10 years later).

Input
The input consists of only one integer n (0 ≤ n ≤ 1015) - the number of years after the creation of the Earth including Adam, Eve and the first apple tree.

Output
Print a single integer - the number of apple trees on Earth at the end of the n - th year after the creation of Earth, as this number can be very big print it modulo 109 + 7

Examples
inputCopy
9
outputCopy
1
inputCopy
10
outputCopy
17
inputCopy
44
outputCopy
77328
inputCopy
45
outputCopy
77327
Note
In the first case, at 9th year there was on Earth only the apple tree that God first created.

In second case we have that in the 10th year the first apple tree was full-grown and 16 apples that belonged to that tree made new 16 trees, therefore there were 17 trees.

In fourth case, we have that at year 45 the first apple tree died, so it doesn’t count anymore.

题意:
1.0年时有一颗树0岁
2.树在10,20,30,40岁时各生16,9,4,1棵0岁的小树
3.树在45岁时死亡
求n年时有多少颗活着的树。
思路:
兔子繁殖问题,不考虑死亡:https://blog.csdn.net/Wen_Yongqi/article/details/83757775
如果考虑死亡,就要表示出来每个年龄当前的个数。
对于这道题,设f(n,i):n年时年龄为i的个数。
f(n,0)=16f(n-1,9)+9f(n-1,19)+4f(n-1,29)+1f(n-1,39)
f(n,i)=f(n-1,i-1)
开一个45*45的矩阵,用矩阵快速幂就行了。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007

ll n;
struct mat{
    ll a[45][45];
};

mat mul(mat x,mat y,int s1,int s2,int s3)
{
    mat res={0};
    for(int i=0;i<s1;i++)
        for(int j=0;j<s3;j++)
            for(int k=0;k<s2;k++)
                res.a[i][j]=(res.a[i][j]+x.a[i][k]*y.a[k][j])%mod;
    return res;
}

ll pow(ll n)
{
    mat res={0},c={0};  
    for(int i=0;i<45;i++)res.a[i][i]=1;
    c.a[0][9]=16;c.a[0][19]=9;c.a[0][29]=4;c.a[0][39]=1;
    for(int i=1;i<45;i++)c.a[i][i-1]=1;
    while(n)
    {
        if(n&1)res=mul(res,c,45,45,45);
        c=mul(c,c,45,45,45);
        n>>=1;
    }
    
    ll ans=0;
    mat init={0};
    init.a[0][0]=1;
    mat ret=mul(res,init,45,45,1);
    for(int i=0;i<45;i++)ans=(ans+ret.a[i][0])%mod;
    return ans;
}

int main()
{
    cin>>n;
    cout<<pow(n)<<endl;
    return 0;
}

实际上,这道题矩阵开一个5*5的就够了,因为这题的特殊性在于,对于任意一个时刻,如果0~9内存在年龄为i的,那么所有的年龄中,只可能有i,10+i,20+i,30+i,40+i这5个年龄存在树。这是为什么呢?我们从一开始推,第0年只有1个0岁,第1 ~ 9年只有1个i岁,第10年时候有10岁和0岁,之后类似,所有树恰好相差0岁或者10岁,20岁,30岁,40岁。
那么我们以10年为一个单位,f(0):0岁的个数,f(1):10岁的个数,…f(40):40岁的个数,
进行m=(n-n%10)/10次幂,然后根据n%10是否<=4来决定是否把40岁的算进答案里。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007

ll n;
struct mat{
    ll a[5][5];
};

mat mul(mat x,mat y)
{
    mat res={0};
    for(int i=0;i<5;i++)
        for(int j=0;j<5;j++)
            for(int k=0;k<5;k++)
                res.a[i][j]=(res.a[i][j]+x.a[i][k]*y.a[k][j])%mod;
    return res;
}

ll pow(ll n)
{
    mat res={0},c={0};  
    for(int i=0;i<5;i++)res.a[i][i]=1;
    c.a[0][0]=16;c.a[0][1]=9;c.a[0][2]=4;c.a[0][3]=1;
    for(int i=1;i<5;i++)c.a[i][i-1]=1;
    ll m=(n-n%10)/10;
    while(m)
    {
        if(m&1)res=mul(res,c);
        c=mul(c,c);
        m>>=1;
    }
    ll ans=0;
    ll A=res.a[0][0],B=res.a[1][0],C=res.a[2][0],D=res.a[3][0],E=res.a[4][0];
    ans=(A+B+C+D)%mod;
    if(n%10<=4)ans=(ans+E)%mod;
    return ans;
}

int main()
{
    cin>>n;
    cout<<pow(n)<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自2020年以来,自动驾驶汽车(Autonomous Vehicle)在这一领域取得了显著进展。 自动驾驶汽车通过使用先进的传感技术,如雷达、激光雷达和摄像头,以及深度学习和人工智能算法,实现了车辆的自主驾驶。它们能够感知周围环境,了解道路状况,并做出相应的决策和行驶动作,从而实现无需人类操控的车辆行驶。 自动驾驶汽车在2020年迅速崭露头角。它们的技术日益成熟,不断的实验和测试表明其在安全性和性能方面已经取得了显著的突破。虽然仍然存在一些挑战,比如在复杂城市环境中导航和处理紧急情况,但这些问题正经过不断的研究和改进来得以解决。 在未来,自动驾驶汽车有望在各个领域发挥重要作用。首先,它们将可以提高道路交通的安全性。由于自动驾驶车辆不受人类司机的疲劳、分心和驾驶误差的限制,它们的驾驶能力更为稳定和准确。其次,自动驾驶汽车还能够提高交通效率。通过与其他车辆实时通信和协同,它们可以避免交通堵塞和减少事故发生,从而减少交通拥堵和行车时间成本。 此外,自动驾驶汽车也将为交通出行带来便利和舒适性。乘客可以更轻松地进行其他活动,如工作、休息或娱乐,而不必担心驾驶问题。老年人和残障人士也将能够自由独立地出行,提高他们的生活质量。 综上所述,作为2020年的重要趋势,自动驾驶汽车具有广阔的应用前景。通过不断的创新和发展,它们将在道路交通安全、交通效率和出行体验方面取得进一步的提升。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值