2021 Niu Guest Winter Holiday Algorithm Basic Training Camp 6 G. Robot (state pressure dp+__int128)

Portal

Title

There are n robots, and each robot will read an x ​​and return ax+b.

Now sister Yinlin has a number x in her hand. She wants to arrange the robots in a certain order so that the x finally returned is as large as possible.

For all data, 1 ≤ n, x, ai, bi ≤ 20 \le n,x,a_i,b_i\le20n,x,ai,bi20

analysis

State pressure dp naked question:
f [i] f[i]f [ i ] means the state isiii (the jth digit in i is 0 means that the jth robot has not been used, and when it is 1, it means that the jth robot has been used, j=[0~n-1]), the maximum value of the result.
Example of status update:
f [(111) 2] f[(111)_2]f[(111)2] To start fromf [(011) 2], f [(101) 2], f [(110) 2] f[(011)_2],f[(101)_2],f[(110)_2]f[(011)2],f[(101)2],f[(110)2] Three statuses have been updated.
f [(111) 2] = max (a [2] ∗ f [(011) 2] + b [2], a [1] ∗ f [(101) 2] + b [1], a [0] ∗ f [(110) 2] + b [0]) f[(111)_2]=max(a[2]*f[(011)_2]+b[2],a[1]*f[( 101)_2]+b[1],a[0]*f[(110)_2]+b[0])f[(111)2]=max(a[2]f[(011)2]+b[2],a[1]f[(101)2]+b[1],a[0]f[(110)2]+b [ 0 ] ) The
final answer isf [(1111...11) 2] f[(1111...11)_2]f[(1111...11)2] (n 1)

But the upper bound will be super longlong if you count the hands.
The high-precision tuned for a long time has problems (the board was written a long time ago, and it seems that it was not written well at the time), and finally __int128 water passed.

About __int128

Carefully sort out __int128, it's pretty easy to use, it can be used normally on my computer, there is no Linux mentioned on the Internet to use it.
But:
1. The upper bound of __int128 is 3.4e38, which is too big or not.
2. To manually implement the input and output functions.
3. Pay attention to the writing method (it has been pitted) when performing bit operations.

Code

#include <bits/stdc++.h>

using namespace std;
//-----pre_def----
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
#define fir(i, a, b) for (int i = (a); i <= (b); i++)
#define rif(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
#define init_h memset(h, -1, sizeof h), idx = 0;
#define lowbit(x) x &(-x)

//---------------
const int N = 23;

int n, x;
int a[N], b[N];
__int128 f[1 << 21];
inline void print(__int128 x)
{
    
    
    if (x < 0)
    {
    
    
        putchar('-');
        x = -x;
    }
    if (x > 9)
        print(x / 10);
    putchar(x % 10 + '0');
}

int main()
{
    
    
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    int StartTime = clock();
#endif
    scanf("%d%d", &n, &x);
    f[0] = x;
    fir(i, 0, n - 1)
    {
    
    
        scanf("%d%d", &a[i], &b[i]);
    }
    for (int i = 1; i <= (1 << n) - 1; i++)
    {
    
    
        for (int j = 0; j < n; j++)
        {
    
    
            if ((i >> j) & 1)
            {
    
    
                f[i] = max(f[i], f[i - (1 << j)] * a[j] + b[j]);
            }
        }
    }
    print(f[(1 << n) - 1]);
#ifndef ONLINE_JUDGE
    printf("Run_Time = %d ms\n", clock() - StartTime);
#endif
    return 0;
}

A similar high-precision + dp problem

Guess you like

Origin blog.csdn.net/sancpp/article/details/114051248