跳到正文

个人记录

[WPOI-R1] T2 满江红 官方题解

ztntonny约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 7 分钟

版本

  1. v1当前

思路

不难发现,在经过至多 4343 项以后,就已经不可能有符合任意情况的数了,所以我们实际上只用考虑前 5050 项,用 O(n2)\mathcal{O}(n^2) 做法。

二阶线性递推通项公式推导。

由上面的推导我们得到,对于任意的 m,nm,n:

{pn=(1−52)n−15+52p1−5p25+(1+52)n−1(5−52p1+5p25)pm=(1−52)m−15+52p1−5p25+(1+52)m−1(5−52p1+5p25)\left\{ \begin{aligned} p_n=(\frac{1-\sqrt{5}}{2})^{n-1}\frac{\frac{5+\sqrt{5}}{2}p_1-\sqrt{5}p_2}{5}+(\frac{1+\sqrt{5}}{2})^{n-1}(\frac{\frac{5-\sqrt{5}}{2}p_1+\sqrt{5}p_2}{5})\\ p_m=(\frac{1-\sqrt{5}}{2})^{m-1}\frac{\frac{5+\sqrt{5}}{2}p_1-\sqrt{5}p_2}{5}+(\frac{1+\sqrt{5}}{2})^{m-1}(\frac{\frac{5-\sqrt{5}}{2}p_1+\sqrt{5}p_2}{5}) \end{aligned} \right.

设:

{L1=5((1−52)n−2+(1+52)n−2)L2=5((1−52)m−2+(1+52)m−2)K1=5((1+52)n−1−(1−52)n−1)K2=5((1+52)m−1−(1−52)m−1)\left\{ \begin{aligned} L_1=\sqrt{5}((\frac{1-\sqrt{5}}{2})^{n-2}+(\frac{1+\sqrt{5}}{2})^{n-2})\\ L_2=\sqrt{5}((\frac{1-\sqrt{5}}{2})^{m-2}+(\frac{1+\sqrt{5}}{2})^{m-2})\\ K_1=\sqrt{5}((\frac{1+\sqrt{5}}{2})^{n-1}-(\frac{1-\sqrt{5}}{2})^{n-1})\\ K_2=\sqrt{5}((\frac{1+\sqrt{5}}{2})^{m-1}-(\frac{1-\sqrt{5}}{2})^{m-1}) \end{aligned} \right. ∴{p1=K2pn−K1pmL1K2−L2K1p2=L2pn−L1pmL1K2−L2K1\therefore \left\{ \begin{aligned} p_1=\frac{K_2p_n-K_1p_m}{L_1K_2-L_2K_1}\\ p_2=\frac{L_2p_n-L_1p_m}{L_1K_2-L_2K_1} \end{aligned} \right. ∴{p1=pn((1+52)m−1−(1−52)m−1)−pm((1+52)n−1−(1−52)n−1))(−1)m−1((1+52)n−m−(1−52)n−m)p2=pm((1+52)n−2−(1−52)n−2)−pn((1+52)m−2−(1−52)m−2))(−1)m−1((1−52)n−m−(1+52)n−m)\therefore \left\{ \begin{aligned} p_1=\frac{p_n((\frac{1+\sqrt{5}}{2})^{m-1}-(\frac{1-\sqrt{5}}{2})^{m-1})-p_m((\frac{1+\sqrt{5}}{2})^{n-1}-(\frac{1-\sqrt{5}}{2})^{n-1}))}{(-1)^{m-1}((\frac{1+\sqrt{5}}{2})^{n-m}-(\frac{1-\sqrt{5}}{2})^{n-m})}\\ p_2=\frac{p_m((\frac{1+\sqrt{5}}{2})^{n-2}-(\frac{1-\sqrt{5}}{2})^{n-2})-p_n((\frac{1+\sqrt{5}}{2})^{m-2}-(\frac{1-\sqrt{5}}{2})^{m-2}))}{(-1)^{m-1}((\frac{1-\sqrt{5}}{2})^{n-m}-(\frac{1+\sqrt{5}}{2})^{n-m})} \end{aligned} \right.

我们可以枚举 (m,n)(m,n),并对于每一对都记录得到的 (p1,p2)(p_1,p_2),最后对于每一个 (p1,p2)(p_1,p_2) 都用 O(n)\mathcal{O}(n) 来进行计算前 5050 项中有多少个正确,最后用总数减去计算数量即可。注意还有一个特判:在全都是 00 的时候是可以的,所以需要都遍历一遍找到 00 的个数,与计算数量取 max⁡\max 就可以了。精度方面,考虑记录 ([p1],[p2]),([p1]+1,[p2]),([p1],[p2]+1),([p1]+1,[p2]+1)([p_1],[p_2]),([p_1]+1,[p_2]),([p_1],[p_2]+1),([p_1]+1,[p_2]+1) 四对,时间复杂度不会差多少。

C++
#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
ll t , p[1000005] , ans = 2 , zero;
ld powmns[55] , powpls[55];

struct N
{
	ll pp1 , pp2;
	N()
	{
		pp1 = 0 , pp2 = 0;
	}
};

ll check( N x )
{
	ll s = 0 , p_1 = x.pp1 , p_2 = x.pp2 , cnt = 0;
	if ( p[1] == p_1 )	cnt++;
	if ( p[2] == p_2 )	cnt++;
	for ( int i = 3; i <= min( 50ll , t ) && p_1 + p_2 <= 1000000000; i++ )
	{
		s = p_1 + p_2 , p_1 = p_2 , p_2 = s;
		if ( s == p[i] )	cnt++;
	}
	return cnt;
}

queue<N> q;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL) , cout.tie(NULL);
	for ( int i = 1; i <= 50; i++ )	powmns[i] = pow( ( 1 - sqrtl(5) ) / 2 , i ) , powpls[i] = pow( ( 1 + sqrtl(5) ) / 2 , i );
	cin >> t;
	for ( int i = 1; i <= t; i++ )	cin >> p[i];
	for ( int m = 2; m <= min( 50ll , t ); m++ )
	{
		for ( int n = m + 1; n <= min( 50ll , t ); n++ )
		{
			ld mnsn1 = powmns[n - 1] , mnsm1 = powmns[m - 1] , mnsn2 = powmns[n - 2] , mnsm2 = powmns[m - 2] , mnsnm = powmns[n - m] ,
			   plsn1 = powpls[n - 1] , plsm1 = powpls[m - 1] , plsn2 = powpls[n - 2] , plsm2 = powpls[m - 2] , plsnm = powpls[n - m];
			if ( plsnm == mnsnm )	continue;
			ld p1 = abs( ( (ld)p[n] * ( plsm1 - mnsm1 ) - (ld)p[m] * ( plsn1 - mnsn1 ) ) / ( plsnm - mnsnm ) ) ,
			   p2 = abs( ( (ld)p[m] * ( plsn2 - mnsn2 ) - (ld)p[n] * ( plsm2 - mnsm2 ) ) / ( plsnm - mnsnm ) );
			ll lp1 = p1 , lp2 = p2;
			for ( int i = 0; i <= 1; i++ )
			{
				for ( int j = 0; j <= 1; j++ )
				{
					N pp;
					pp.pp1 = lp1 + i , pp.pp2 = lp2 + j;
					q.push( pp );
				}
			}
		}
	}
	while ( !q.empty() ) ans = max( ans , check( q.front() ) ) , q.pop();
	for ( int i = 1; i <= t; i++ )	if ( !p[i] )	zero++;
	ans = max( ans , zero );
	cout << t - ans << endl;
	return 0;
}
原文