牛客假日团队赛10 L乘积最大 (dp,大数)
鏈接:https://ac.nowcoder.com/acm/contest/1072/L?&headNav=acm&headNav=acm
來源:牛客網
乘積最大
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 262144K,其他語言524288K
64bit IO Format: %lld
題目描述
今年是國際數學聯盟確定的“2000——世界數學年”,又恰逢我國著名數學家華羅庚先生誕辰90周年。在華羅庚先生的家鄉江蘇金壇,組織了一場別開生面的數學智力競賽的活動,你的一個好朋友XZ也有幸得以參加。活動中,主持人給所有參加活動的選手出了這樣一道題目:
設有一個長度為N的數字串,要求選手使用K個乘號將它分成K+1個部分,找出一種分法,使得這K+1個部分的乘積能夠為最大。
同時,為了幫助選手能夠正確理解題意,主持人還舉了如下的一個例子:
有一個數字串:312, 當N=3,K=1時會有以下兩種分法:
1) 312=36
2) 312=62
這時,符合題目要求的結果是:31*2=62
現在,請你幫助你的好朋友XZ設計一個程序,求得正確的答案。
輸入描述:
第一行共有2個自然數N,K(6 ≤ N ≤ 40,1 ≤ K ≤ 6)
第二行是一個長度為N的數字串。
輸出描述:
輸出所求得的最大乘積(一個自然數)。
示例1
輸入
復制
4 2
1231
輸出
復制
62
思路:
定義dp狀態為 dp[i][j] 代表 到第i個數字,用了j個乘號,獲得的最大乘積。
由于長度是40,所以肯定是會爆longlong的,用大數模板即可。
細節見代碼:
#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++) ; //去前導0len = 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';}} }轉載于:https://www.cnblogs.com/qieqiemin/p/11347966.html
總結
以上是生活随笔為你收集整理的牛客假日团队赛10 L乘积最大 (dp,大数)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CustomValidator
- 下一篇: 牛客练习赛44 A小y的序列 (模拟,