题面:
贝壳找房的攻城狮最近在研究一次函数 f(x) = ax + bf(x)=ax+b。
现在有 nn 个一次函数,f_i(x) = a_ix+b_i,\ i = \{1 \mathellipsis n\}fi(x)=aix+bi, i={1…n}。
容易发现,一次函数嵌套一次函数,还是一次函数。
\displaystyle f_{i}(f_{j}(x)) = a_{i} ( a_{j}x + b_{j}) + b_{i}fi(fj(x))=ai(ajx+bj)+bi
给定 xx,并且对于所有的 f_i(x)fi(x) 可以任意改变顺序嵌套函数,求 f(f(f(…f(x))…)f(f(f(…f(x))…) 的最大值。
思路:
其实就是一个排序问题,一开始我直接按b从大到小排序,交上去wa了,发现有点问题,仔细想了想,其实应该是一个按贡献排序,ai*bj>aj*bi逻辑较大的在内层那么之后所有的ai乘起来肯定最大
代码:
#include<algorithm>
#include<typeinfo>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<iomanip>
#include<stdio.h>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
#define pi acos(-1)
#define mod (1e9+7)
#define fin(num,n) for(int aaa=0;aaa<n;cin>>num[aaa++])
#define fout(num,n) for(int aaa=0;aaa<n;cout<<num[aaa++]<<(aaa==n-1?'\n':' '))
#define ffin(num,n,m) for(int aaa=0;aaa<n;aaa++)for(int bbb=0;bbb<m;cin>>num[aaa][bbb++])
#define ffout(num,n,m) for(int aaa=0;aaa<n;aaa++)for(int bbb=0;bbb<m;cout<<num[aaa][bbb++]<<(bbb==m-1?'\n':' '))
ll gcd(ll x, ll y) { return x ? gcd(y%x, x) : y; }
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }
int t, n, x;
pair<int, int>num[100005];
int cmp(pair<int, int> x, pair<int, int> y) {
return x.first*y.second + x.second < y.first*x.second + y.second;
}
int main() {
ios::sync_with_stdio(false);
cout << fixed << setprecision(0);
while (cin >> t)
while (t--) {
cin >> n >> x;
for (int a = 0; a < n; a++)
cin >> num[a].first;
for (int a = 0; a < n; a++)
cin >> num[a].second;
sort(num, num + n, cmp);
for (int a = 0; a < n; a++)
x *= num[a].first, x += num[a].second, x %= 10;
cout << x << endl;
}
return 0;
}