洛谷 P1011 [NOIP 1998 提高组] 车站
思路:
我的思路是可以把这题可以看成是一元一次表达式的斐波那契,只需要记录上车人数即可,因为下车人数是上一站的上车人数,我在代码中设第二站上下车人数都是y,依次递推出后面的上下车人数,以及车上人数,中途对查询车站做记录就行了。
ps:洛谷题解大佬写的代码是短之又短。
AcCode:
#include<iostream>
using namespace std;
struct station{
int k, b;
};
int main(){
int a, n, m, x;
cin >> a >> n >> m >> x;
if(x == 1 || x == 2){
cout << a;
return 0;
}
station f1 = {0, a}, f2 = {1, 0};
station ans, end = {0, a};
for(int i = 3; i < n; i++){
station f3 = {f1.k + f2.k, f1.b + f2.b};
end.k += f3.k - f2.k;
end.b += f3.b - f2.b;
if(i == x){
ans = end;
}
f1 = f2;
f2 = f3;
}
int y = (m - end.b) / end.k;
cout << ans.k * y + ans.b;
return 0;
}