1 条题解

  • 0
    @ 2024-12-24 9:48:48

    C++ :

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<ctime>
    #include<cmath>
    #include<string>
    #include<cstdio>
    #include<stack>
    using namespace std;
    
    char ans[100], tmp[100], c[100];
    char s[100];
    char t1[100], t3[100];
    
    void mul(char *a, char *b)
    {
    
        int len1 = strlen(a);
        int len2 = strlen(b);
        int d[100];
        //cout<<a<<"***"<<b<<endl;
        memset(d, 0, sizeof(d));
        for(int i = 0; i < len1; ++i)
        {
            for(int j = 0; j < len2; ++j)
            {
                d[i+j] += (a[i] - '0') * (b[j] - '0');
            }
        }
        int t = 0, len = len1 + len2 - 1;
        for(int i = len - 1; i >= 0; --i)
        {
            d[i] += t;
            t = d[i] / 10;
            d[i] = d[i] % 10;
        }
        stack<int> st;
        while(t > 0)
        {
            st.push(t%10);
            t /= 10;
        }
        int x = 0;
        while(!st.empty())
        {
            c[x++] = char(st.top() + '0');
            st.pop();
        }
        for(int i = 0; i < len; ++i)
        {
            c[x++] = char(d[i] + '0');
        }
        c[x] = '\0';
        int i = 0;
        while(c[i] == '0') ++i;
        strcpy(c, c+i);
        if(strlen(c) == 0) c[0] = '0', c[1] = '\0';
        //cout<<c<<"<------->"<<endl;
    }
    
    bool Big(char *a, char *b)
    {
        int len1 = strlen(a);
        int len2 = strlen(b);
    
        if(len1 > len2) return true;
        else if(len1 < len2) return false;
        else
        {
            for(int i = 0; i < len1; ++i)
            {
                if(a[i] != b[i])
                    return a[i] > b[i] ? 1 : 0;
            }
        }
        return false;
    }
    
    void dfs(int l, int r, int k)
    {
        //cout<<"dfs("<<l<<","<<r<<","<<k<<")"<<endl;
        if(k >= r - l) return ;
        if(k == 0)
        {
            int j, x = 0;
            for(j = l; j < r; ++j) t1[x++] = s[j];
            t1[x] = '\0';
            mul(tmp, t1);
            if(Big(c, ans))
                strcpy(ans, c);
            return;
        }
        for(int i = l; i < r; ++i)
        {
            int j, x = 0;
            for(j = l; j <= i; ++j) t1[x++] = s[j];
            t1[x] = '\0';
            char t2[100];
            strcpy(t2, tmp);
            mul(tmp, t1);
            strcpy(tmp, c);
            dfs(i+1, r, k-1);
            strcpy(tmp, t2);
        }
    }
    
    int main()
    {
        //mul("2", "345");
        int n, k;
        cin>>n>>k>>s;
        ans[0] = '\0';
        tmp[0] = '1'; tmp[1] = '\0';
        dfs(0, n, k);
        cout<<ans<<endl;
        return 0;
    }
    
    
    • 1

    信息

    ID
    690
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者