CF1369D-TediousLee【找规律】

题意:

给定有 \(n\) 层的树,按规律生成的该树,在其中寻找爪子,若找到的爪子里的节点若全是绿色,将其染成黄色,统计染成黄色的节点数量。

分析:

找规律:
如图(来源于:https://blog.csdn.net/mrcrack/article/details/106938912

所以:

\[f[n]=f[n-1]+2*f[n-2]+(i\%3==0?1:0) \]

自己找的时候,过分注重于在一个图中推公式,没有去考虑图形前后的联系。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=2e6+6;
ll f[N];
void init()
{
    f[1]=0;
    f[2]=0;
    f[3]=1;
    f[4]=1;
    for(int i=5;i<=2e6;i++)
    {
        f[i]=(f[i-1]+2*f[i-2])%mod;
        if(i%3==0) f[i]=(f[i]+1)%mod;
    }
}
int main()
{
    int t,n;
    scanf("%d",&t);
    init();
    while(t--)
    {
        scanf("%d",&n);
        printf("%lld\n",f[n]*4%mod);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/1024-xzx/p/13192476.html