Little Q is learning programming languages. He often uses Python as it is one of the most used programming languages. However, when he codes in Python, he finds that the programs do not run very efficiently, especially when compared to C/C++. He wonders if the running efficiency of Python has any improvement with version updates, and conducts an experiment.
The algorithm used in the experiment is Monte Carlo, estimating π by generating a large number of random points in a square. He codes this algorithm in Python and runs it with different versions of Python, logging the running time. Formally, there are a total of n released versions of Python, namely 3.1, 3.2, ..., 3.n. The running time of the algorithm on version 3. is ms. Little Q is surprised to find that the running efficiency of Python does change with version iterations.
Assistant
i
ai
Little Q also tests the running time of this algorithm in C++, which is stably k ms. Then he comes up with a funny idea: using the experimental data to predict which future version of Python will have a higher efficiency than C++. Unfortunately, he forgets how to apply the regression model, so he uses a brute prediction method:
For a future version i that i>n, ai=max(0,2ai−1−ai−2).
With this prediction method, please tell him the earliest version of Python that has a strictly higher efficiency than C++, i.e., find the minimum i such that ai<k.
输入格式
Input
The first line contains two integers n and k (2≤n≤10, 1≤k≤1000), indicating the number of released versions of Python and the running time of the algorithm in C++.
The second line contains n integers a1,a2,…,an (k<ai≤105), indicating the running time of the algorithm on each version of Python.
输出格式
Output
Output Python 3.x will be faster than C++, where x is an integer indicating the earliest version that ax<k. If such a version does not exist, output Python will never be faster than C++.
样例 #1
样例输入 #1
10 1
11 45 14 19 19 8 10 13 10 8
样例输出 #1
Python 3.14 will be faster than C++
提示
Note
The first sample can be expressed in the following picture, in which the blue line represents the efficiency of Python, and the red line represents the efficiency of C++. The dashed line indicates the prediction. As you see, Python 3.14 will be faster than C++.
For the second sample, it is easy to notice that Python always runs for 2 ms, including the prediction of future versions. Therefore, Python will never be faster than C++, which runs for 1 ms.
用c++写
To solve this problem, we need to determine the earliest version of Python in which the running time of Little Q's algorithm becomes strictly less than the running time in C++.
Problem Breakdown:
Initial Data:
We are given the number of Python versions n and the running time of C++ k.
We also have the running times of each of the n Python versions.
Prediction Formula:
For future Python versions beyond the initial n, the running time is calculated using the formula:
[
a_i = \max(0, 2a_{i-1} - a_{i-2})
]
Our goal is to find the smallest version number x such that a_x < k.
Approach:
Calculate the Difference (B): Determine the difference between the last two Python versions' running times.
Determine the Constant (A): Using the formula derived from the recurrence relation, calculate the constant term.
Check the Slope (B):
If B >= 0, the running time is non-decreasing, and Python will never be faster than C++.
If B < 0, proceed to calculate the version number x where a_x < k.
Calculate the Version Number (x):
Use the formula derived from the recurrence to find the smallest x where the running time is less than k.
If the running time equals k at some version, increment x to ensure a_x < k.
Edge Cases:
If the initial versions already meet the condition, handle accordingly.
If no such version exists where Python becomes faster, output the appropriate message.
Implementation:
#include <bits/stdc++.h>
using namespace std;
int main(){
int n, k;
cin >> n >> k;
// Using 1-based indexing
vector<long long> a(n+1);
for(int i=1;i<=n;i++) cin >> a[i];
// Handle the case when n >=2
long long a_n_minus1 = a[n-1];
long long a_n = a[n];
long long B = a_n - a_n_minus1;
long long A = a_n - B * n;
if(B >=0){
cout << "Python will never be faster than C++.";
return 0;
}
long long d = A - k;
long long x;
if(d <=0){
x = n +1;
}
else{
long long div = d / (-B);
long long rem = d % (-B);
if(rem >0){
div +=1;
}
x = div;
}
// Ensure x >=n +1
x = max(x, (long long)(n +1));
// Compute a_x
long long a_x = A + B * x;
if(a_x < k){
// Output x
cout << "Python 3." << x << " will be faster than C++";
return 0;
}
// Else, try x +1
x +=1;
a_x = A + B * x;
if(a_x < k){
cout << "Python 3." << x << " will be faster than C++";
return 0;
}
// Else, never
cout << "Python will never be faster than C++.";
return 0;
}
Explanation:
Input Parsing: We first read the number of Python versions n and the running time k of C++. We then read the running times of the n Python versions.
Calculations:
Calculate B as the difference between the last two Python versions.
Calculate A using the formula derived from the recurrence relation.
Decision Making:
If B is non-negative, Python's running time will not decrease below C++’s, so output that Python will never be faster.
If B is negative, calculate the earliest version x where Python's running time becomes less than k using the derived formulas.
Final Output:
If such a version x exists where a_x < k, output the version number.
Otherwise, output that Python will never be faster than C++.
Example Walkthrough:
Sample Input:
10 1
11 45 14 19 19 8 10 13 10 8
Calculation:
B = 8 - 10 = -2
A = 8 - (-2)*10 = 28
d = 28 - 1 = 27
x = ceil(27 / 2) = 14
a_14 = 28 - 2*14 = 0 < 1
Output:
Python 3.14 will be faster than C++
This ensures that the solution efficiently computes the required version while handling all edge cases appropriately.