• 【题解】Luogu P4910 帕秋莉的手环


    原题传送门

    “连续的两个中至少有1个金的”珂以理解为“不能有两个木相连”

    我们考虑一个一个将元素加入手环

    设f([i][0/1])表示长度为(i)手环末尾有(0/1)个木的种类数

    仔细想想发现它实际就是一个斐波那契数列((fib[1]=fib[2]=1)

    因为首尾相接,所以开头要分类讨论

    • 第一个是金:对答案的贡献为(fib[n]+fib[n-1])

    • 第一个是木:对答案的贡献为(fib[n-1])

    矩阵快速幂即可

    #include <bits/stdc++.h>
    #define ll long long
    #define mod 1000000007
    #define getchar nc
    using namespace std;
    inline char nc(){
        static char buf[100000],*p1=buf,*p2=buf;
        return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
    }
    inline ll read()
    {
        register ll x=0,f=1;register char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
        return x*f;
    }
    inline void write(register int x)
    {
        if(!x)putchar('0');if(x<0)x=-x,putchar('-');
        static int sta[20];register int tot=0;
        while(x)sta[tot++]=x%10,x/=10;
        while(tot)putchar(sta[--tot]+48);
    }
    struct mat{
    	int a[2][2];
    	inline mat()
    	{
    		memset(a,0,sizeof(a));
    	}
    	inline mat operator*(const mat&b)const{
    		mat c;
    		for(register int i=0;i<2;++i)
    			for(register int j=0;j<2;++j)
    				for(register int k=0;k<2;++k)
    					c.a[i][j]=(c.a[i][j]+1ll*a[i][k]*b.a[k][j])%mod;
    		return c;
    	}
    }s,o,ans;
    inline mat fastpow(register mat a,register ll b)
    {
    	mat res;
    	res.a[0][0]=res.a[1][1]=1;
    	while(b)
    	{
    		if(b&1)
    			res=res*a;
    		a=a*a;
    		b>>=1;
    	}
    	return res;
    }
    int T;
    ll n;
    int main()
    {
    	T=read();
    	s.a[0][0]=s.a[0][1]=s.a[1][0]=1;
    	o.a[0][0]=1;
    	while(T--)
    	{
    		n=read();
    		ans=o*fastpow(s,n-1);
    		write((1ll*ans.a[0][0]+ans.a[0][1]*2ll)%mod),puts("");
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    [LeetCode]4Sum
    [LeetCode]3Sum
    [LeetCode]Two Sum
    [LeetCode]Maximal Rectangle
    [LeetCode]Largest Rectangle in Histogram
    [LeetCode]Sudoku Solver
    [LeetCode]Group Anagrams
    jQuery验证控件jquery.validate.js使用说明+中文API
    js操作cookie,实现登录密码保存
    Java中getResourceAsStream的用法
  • 原文地址:https://www.cnblogs.com/yzhang-rp-inf/p/10890998.html
Copyright © 2020-2023  润新知