Holiday teams cattle off season 10 L product of the maximum (dp, large numbers)

Links: https://ac.nowcoder.com/acm/contest/1072/L?&headNav=acm&headNav=acm
Source: Cattle-off network

Product of the maximum
time limit: C / C ++ 1 second, other languages 2 seconds
to space constraints: C / C ++ 262144K, other languages 524288K
64bit IO the Format:% LLD
subject description
This year is the International Mathematical Union to determine the "2000-- World Mathematical Year" 90 also coincides with a famous mathematician Mr. Zhou Nian Hua birthday. Mr. Hua's hometown Jintan, math quiz organized activity of a spectacular, one of your good friend XZ also had the honor to participate. Activity, the host to all the players participating in a such a Title:
with a length of N strings of numbers, the player requires the use of the K multiplication sign it into K + 1 parts, to find a points, K + 1 so that this can be part-pieces of multiplied maximum.
Meanwhile, in order to help the player can correctly understanding the problem, a moderator is also cited in the following examples:
a string of numbers: 312, when N = 3, K = 1 there will be the following two points Method:
1) 3 12 = 36
2) 31
2 = 62
in this case, the result is in line with requirements of the subject: 31 * 2 = 62
now, please help your friends XZ design a program to obtain the correct answer.
Input Description:
The first line there are 2 natural numbers N, K (6 ≤ N ≤ 40,1 ≤ K ≤ 6)
The second line is a numeric string of length N.
Description Output:
output the determined maximum product of (a natural number).
Example 1
Input
replication
. 4 2
1231 of
Output
Copy
62

Thinking:
defined as the state dp dp [i] [j] to the representative of the i-th digit, j-th multiplication with the largest product obtained.

Due to the length it is 40, so it must be longlong will burst, and with large numbers template.

See details Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
const int MAXN=50;
struct bign
{
    int len, s[MAXN];
    bign ()
    {
        memset(s, 0, sizeof(s));
        len = 1;
    }
    bign (int num) { *this = num; }
    bign (const char *num) { *this = num; }
    bign operator = (const int num)
    {
        char s[MAXN];
        sprintf(s, "%d", num);
        *this = s;
        return *this;
    }
    bign operator = (const char *num)
    {
        for(int i = 0; num[i] == '0'; num++) ;  //去前导0
        len = strlen(num);
        for(int i = 0; i < len; i++) s[i] = num[len-i-1] - '0';
        return *this;
    }
    bign operator + (const bign &b) const //+
    {
        bign c;
        c.len = 0;
        for(int i = 0, g = 0; g || i < max(len, b.len); i++)
        {
            int x = g;
            if(i < len) x += s[i];
            if(i < b.len) x += b.s[i];
            c.s[c.len++] = x % 10;
            g = x / 10;
        }
        return c;
    }
    bign operator += (const bign &b)
    {
        *this = *this + b;
        return *this;
    }
    void clean()
    {
        while(len > 1 && !s[len-1]) len--;
    }
    bign operator * (const bign &b) //*
    {
        bign c;
        c.len = len + b.len;
        for(int i = 0; i < len; i++)
        {
            for(int j = 0; j < b.len; j++)
            {
                c.s[i+j] += s[i] * b.s[j];
            }
        }
        for(int i = 0; i < c.len; i++)
        {
            c.s[i+1] += c.s[i]/10;
            c.s[i] %= 10;
        }
        c.clean();
        return c;
    }
    bign operator *= (const bign &b)
    {
        *this = *this * b;
        return *this;
    }
    bign operator - (const bign &b)
    {
        bign c;
        c.len = 0;
        for(int i = 0, g = 0; i < len; i++)
        {
            int x = s[i] - g;
            if(i < b.len) x -= b.s[i];
            if(x >= 0) g = 0;
            else
            {
                g = 1;
                x += 10;
            }
            c.s[c.len++] = x;
        }
        c.clean();
        return c;
    }
    bign operator -= (const bign &b)
    {
        *this = *this - b;
        return *this;
    }
    bign operator / (const bign &b)
    {
        bign c, f = 0;
        for(int i = len-1; i >= 0; i--)
        {
            f = f*10;
            f.s[0] = s[i];
            while(f >= b)
            {
                f -= b;
                c.s[i]++;
            }
        }
        c.len = len;
        c.clean();
        return c;
    }
    bign operator /= (const bign &b)
    {
        *this  = *this / b;
        return *this;
    }
    bign operator % (const bign &b)
    {
        bign r = *this / b;
        r = *this - r*b;
        return r;
    }
    bign operator %= (const bign &b)
    {
        *this = *this % b;
        return *this;
    }
    bool operator < (const bign &b)
    {
        if(len != b.len) return len < b.len;
        for(int i = len-1; i >= 0; i--)
        {
            if(s[i] != b.s[i]) return s[i] < b.s[i];
        }
        return false;
    }
    bool operator > (const bign &b)
    {
        if(len != b.len) return len > b.len;
        for(int i = len-1; i >= 0; i--)
        {
            if(s[i] != b.s[i]) return s[i] > b.s[i];
        }
        return false;
    }
    bool operator == (const bign &b)
    {
        return !(*this > b) && !(*this < b);
    }
    bool operator != (const bign &b)
    {
        return !(*this == b);
    }
    bool operator <= (const bign &b)
    {
        return *this < b || *this == b;
    }
    bool operator >= (const bign &b)
    {
        return *this > b || *this == b;
    }
    string str() const
    {
        string res = "";
        for(int i = 0; i < len; i++) res = char(s[i]+'0') + res;
        return res;
    }
};
istream& operator >> (istream &in, bign &x)
{
    string s;
    in >> s;
    x = s.c_str();
    return in;
}
ostream& operator << (ostream &out, const bign &x)
{
    if (x.str()=="") out<<0;
    else out << x.str();
    return out;
}
string a;
int n,k;
bign dp[41][7];
bign temp;
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    gbtb;
    cin>>n>>k>>a;
    a="0"+a;
    for(int i=1;i<=n;++i)
    {
        for(int j=0;j<=k;j++)
        {
            dp[i][j]=0;
        }
    }
    for(int i=0;i<=k;++i)
    {
        dp[0][i]=1;
    }
    for(int i=1;i<=n;++i)
    {
        dp[i][0]=bign(a.substr(1,i).c_str());
        for(int j=1;j<=k;j++)
        {
            for(int z=0;z<=i;z++)
            {
                temp=dp[z][j-1]*bign(a.substr(z+1,(i-z)).c_str());
                if(temp>dp[i][j])
                    dp[i][j]=temp;
            }
        }
    }
    cout<<dp[n][k]<<endl;
    return 0;
}
 
inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}
 
 

Guess you like

Origin www.cnblogs.com/qieqiemin/p/11347966.html
Recommended