跳到正文

学习·文化课

TJ

Lovely_Mickey约 5 分钟
原文
文章信息与版本

文章信息

发布
2024年8月9日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 5 分钟

版本

  1. v2当前
  2. v1

此题很简单

前置知识:逆元,(矩阵),简单线性dp

下列inv(i)表示i的逆元

首先,显然是可以DP的,转移方程如下(简单线性DP)

C++
a[i][0]=(6*inv(10)%mod*a[i-1][0]+2*inv(10)%mod*a[i-1][1])%mod;
a[i][1]=(4*inv(10)%mod*a[i-1][0]+8*inv(10)%mod*a[i-1][1])%mod;

其中a[i][0]表示第i次是Mickey投篮概率,a[i][1]表示第i次是CXK投篮概率

然后,我们考虑优化 : 矩阵加速

懒得讲,根据上面转移方程,十分容易得到,代码如下(转移矩阵寄了不能怪别人)

C++
#include <bits/stdc++.h>

using namespace std;

#define int long long

const int mod=1e9+7;

struct Matrix
{
    int a[3][3];
    Matrix operator *(Matrix b)
    {
        Matrix c;
        memset(c.a,0,sizeof(c.a));
        for(int i=1;i<=2;i++)
            for(int j=1;j<=2;j++)
                for(int k=1;k<=2;k++)
                    c.a[i][j]=(c.a[i][j]+1ll*a[i][k]*b.a[k][j]%mod)%mod;
        return c;
    }
    void print()
    {
        for(int i=1;i<=2;i++)
        {
            for(int j=1;j<=2;j++) cout<<a[i][j]<<" ";
            cout<<endl;
        }
    }
};

inline int qpow(int a,int b)
{
    int ans=1;
    for(;b;b>>=1,a=1ll*a*a%mod) if(b&1) ans=1ll*ans*a%mod;
    return ans;
}

inline Matrix qpow(Matrix a,int b)
{
    Matrix ans;
    ans.a[1][2]=ans.a[2][1]=0;
    ans.a[1][1]=ans.a[2][2]=1;
    for(;b;b>>=1,a=a*a) if(b&1) ans=ans*a;
    return ans;
}

signed main()
{
    int n;
    cin>>n;
    int X=qpow(5,mod-2),Y=qpow(2,mod-2);
    int x=4ll*X%mod,y=2ll*X%mod,z=1ll*X%mod,k=3ll*X%mod;
    Matrix ans;
    ans.a[1][1]=Y;
    ans.a[2][1]=Y;
    ans.a[2][2]=ans.a[1][2]=0;
    Matrix tmp;
    tmp.a[1][1]=x;
    tmp.a[1][2]=y;
    tmp.a[2][1]=z;
    tmp.a[2][2]=k;
    ans=ans*qpow(tmp,n-1);
    cout<<ans.a[2][1]<<endl;
    return 0;
}

法二 : 数学

讲题.jpeg

图上讲的很明白,不懂可以找数学老师

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline long long read()
{
	long long f=1,x=0;
	char c=getchar();
	while(c<'0'||c>'9')
	{
		if(c=='-')
		{
			f=-1;
		}
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		x=x*10+c-'0';
		c=getchar();
	}
	return x*f;
}
const ll mod=1e9+7;
ll pw(ll x,ll a)
{
	ll tot=1;
	while(a!=0)
	{
		if(a%2==0)
		{
			x*=x;
			a/=2;
			x%=mod;
		}
		else
		{
			a--;
			x%=mod;
			tot*=x;
			tot%=mod;
		}
	}
	return tot%mod;
}
ll inv(ll x)
{
	return pw(x,mod-2);
}
int main()
{

	ll n=read();
	if(n==1)
	{
		cout<<inv(2);
		return 0;
	}
	ll ans=(inv(6)*pw(2*inv(5)%mod,n-1)%mod+inv(3))%mod;
	cout<<ans;
	return 0;
}

idea来自高考2023 21题

鸣谢:感谢csr提供矩阵做法的代码

原文