ACM integer Division (iv)

Source: Internet
Author: User
Tags integer division

Integer Division (iv) time limit:MS | Memory limit:65535 KB Difficulty:3
Describe

Summer vacation came, HRDV again to stay in the school to participate in ACM training, Training Life very happy (PS: you know), but he recently encountered a problem, let him baffled his solution, he was very depressed. Honey, can you help him?

The problem is that we often see the integer division, given two integers n, m, required to add m-1 multiplication sign in n, divide n into m segments, and find the maximum product of this m segment

Input
The first line is an integer t, which indicates that there is a T group of test data
Next T line, each line has two positive integers n,m (1<= n < 10^19, 0 < m <= n digits);
Output
output Each set of test sample results as an integer in one row
Sample input
2111 21111 2
Sample output
11121


Using the idea of dynamic planning, thought for a whole afternoon, did not expect to finally make a 5-layer cycle of dynamic planning. Fortunately, the topic of AC, and then see the answer on the Internet, using the DP interval dynamic programming algorithm, today do not study, some tired. Here's my algorithm.

#include <string>#include<sstream>#include<iostream>#include<string.h>#include<stdio.h>using namespacestd;#defineNUM 20Long LongAtoll (Const Char*p)    {StringStream strvalue; strvalue<<p; Long Longvalue; strvalue>>value; returnvalue;}intMain () {intcount; scanf ("%d", &count);  while(count--)    {        intI,j,k,l,m,n; Long LongMm[num][num][num]; CharNum[num]; CharTemp[num]; scanf ("%s", num); N=strlen (num); scanf ("%d", &m);  for(k =0; K < M; k++)        {             for(i =0; I < n; i++)            {                 for(j = i + K; j < N; j + +)                {                    if(k = =0) {strncpy (temp, num+i, j-i+1); Temp[j-i+1] =' /'; MM[I][J][K]=Atoll (temp); }                    Else                    {                        Long Longres =-1;  for(L =0; L < J-i; l++)                        {                            intO, k1=0, k2=0;  for(o =0; o < L +1&& O < K; o++) {K1=o; K2= K-1-K1; Long LongTRes = mm[i][i+l][k1] * mm[i+l+1][J][K2]; Res= res > TRes?Res:tres; }} Mm[i][j][k]=Res; } }}} cout<< mm[0][n-1][m-1] <<Endl; }    return 0;}

DP interval algorithm

#include <iostream>#include<cstdio>#include<string>#include<cmath>#include<algorithm>typedefLong LongLl;typedefLong Longarray[ A]; Array ob;ll n,m,res,reco,rec,cur,maxs,temp;using namespaceStd;ll Pow_dfs (ll i) {ll ress=1;  for(LL j=1; j<=i;j++) {ress*=Ten; }    returnress;} ll Maxmin (ll A,ll b) {returnA>b?a:b;}voidDFS (ll pos,ll rec,ll now) {if(rec==m-1) {Ob[rec]=Now ; Cur=1;  for(intI=0; i<m;i++) {cur*=Ob[i]; } maxs=maxmin (CUR,MAXS); return ; }    if(pos>=reco)return ; DFS (POS+1, Rec,now); OB[REC]=now/pow_dfs (reco-POS); now=now%pow_dfs (reco-POS); DFS (POS+1, rec+1, now);}intMain () {ll T; //freopen ("D://imput.txt "," R ", stdin);scanf"%lld",&T);  while(t--) {Reco=1; maxs=0; scanf ("%lld%lld",&n,&m); Temp=N;  while(temp/Ten) {Temp=temp/Ten; Reco++; } DFS (1,0, N); printf ("%lld", MAXS); if(t!=0) printf ("\ n"); }    return 0;}

ACM integer Division (iv)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.