题目
思路
不完全归纳法
不难发现
f
(
n
)
=
f
(
n
−
1
)
+
f
(
n
−
2
)
u
p
f(n) = f(n - 1) + f(n-2)_{up}
f(n)=f(n−1)+f(n−2)up
且
f
(
n
)
u
p
=
f
(
n
−
1
)
u
p
+
f
(
n
−
2
)
u
p
f(n)_{up} = f(n-1)_{up}+ f(n-2)_{up}
f(n)up=f(n−1)up+f(n−2)up
所以这个题目用迭代更好,用递归的话只能解出来每一段的delta,如果是解出delta再累加那时间复杂度太高了,其实不如迭代来的快
代码(递归)
#define ll long long
using namespace std;
int A[4] {1,0,1,0};
int B[4] {0,0,0,1};
ll CALDb(int times)
{
if(times == 1)
{
return B[0];
}
if(times == 2)
{
return B[0];
}
if(times == 3)
{
return B[0];
}
if(times == 4)
{
return B[3];
}
//cout << times << endl;
return CALDb(times - 1) + CALDb(times - 2);
}
ll CALDa(int times)
{
if(times == 1)
{
return A[0];
}
if(times == 2)
{
return A[1];
}
if(times == 3)
{
return A[2];
}
if(times == 4)
{
return A[3];
}
//cout << times << endl;
return CALDa(times - 1) + CALDa(times - 2);
}
ll calcoea(int a)
{
if(a == 1) return 1;
if(a == 2) return 1;
if(a == 3) return 2;
if(a == 4) return 2;
return CALDa(a) + calcoea(a - 1);
}
ll calcoeb(int b)
{
if(b < 4) return 0;
if(b == 4) return 1;
return CALDb(b) + calcoeb(b - 1);
}
int main()
{
int a;
int n;
int m;
int x;
cin >> a >> n >> m >> x;
ll b = (m - a * calcoea(n - 1)) / calcoeb(n - 1);
//cout << "b " << b << " a " << a << endl;//
// for(int i = 3; i< 10; i++)
// {
// cout << "b " << calcoeb(i) << " a " << calcoea(i) << endl;//
// }
ll people = calcoeb(x)* b + calcoea(x) * a;
cout << people << endl;
return 0;
}
代码(迭代)
#include<iostream>
#define ll long long
using namespace std;
struct STA
{
long long coea = 0;
long long coeb = 0;
ll da = 0;
ll db = 0;
ll upa = 0;
ll upb = 0;
};
//
int main()
{
int a;
int n;
int m;
int x;
cin >> a >> n >> m >> x;
STA * sta = new STA[n];
sta[0].coea = 1;
sta[1].coea = 1;
sta[0].upa = 1;
sta[1].upb = 1;
for(int i = 2; i < n; i++)
{
sta[i].upa = sta[i - 1].upa + sta[i - 2].upa;
sta[i].upb = sta[i - 1].upb + sta[i - 2].upb;
sta[i].da = sta[i - 2].upa;
sta[i].db = sta[i - 2].upb;
sta[i].coea = sta[i].da + sta[i - 1].coea;
sta[i].coeb = sta[i].db + sta[i - 1].coeb;
//cout <<"coea " <<sta[i].coea <<" coeb "<<sta[i].coeb << endl;
}
//a一开始上车 m一开始下车
//n车站 x车站
ll b = (m - a * sta[n - 2].coea) / sta[n - 2].coeb;
cout << "b " << b << " a " << a << endl;
ll people = sta[x - 1].coeb * b + sta[x - 1].coea * a;
cout << people << endl;
return 0;
}