CF educational Codeforces Round D. Round subset

Source: Internet
Author: User
Tags integer numbers time limit


Original question:
D. Round Subset
Time limit per test2 seconds
Memory limit per test256 megabytes
Inputstandard input
Outputstandard output
Let's call the roundness of the number the number of zeros to which it ends.



You have an array of n numbers. You need to choose a subset of exactly k numbers so that the roundness of the product of the selected numbers would be maxi Mum possible.



Input
The first line contains the numbers n and K (1≤n≤200, 1≤k≤n).



The second line contains n space-separated integer numbers a1, a2, ..., an (1≤ai≤1018).



Output
Print maximal roundness of product of the chosen subset of length K.



Examples
Input
3 2
50 4 20
Output
3
Input
5 3
15 16 3) 25 9
Output
3
Input
3 3
9 77 13
Output
0
Note
In the first example there is 3 subsets of 2 numbers. [4] has product roundness 2, [4, 20]-product, roundness 1, [, 20]-product, roundness 3.



In the second example subset [+] has product 6000, roundness 3.



The third example all subsets have product with roundness 0.



English:
Give you the number of n, let you choose K to multiply. Ask you the number of the last 0 at the end of the maximum number of.


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
ll n,k;
ll dp[201][10001];
ll two[201],five[201],num[201];
pii get_five_two(ll x)
{
    int f=0,t=0;
    while(true)
    {
        if(x%5)
            break;
        x/=5;
        f++;
    }
    while(true)
    {
        if(x%2)
            break;
        x/=2;
        t++;
    }
    return make_pair(f,t);
}

int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>k)
    {
        ll tot=0;
        for(int i=1;i<=n;i++)
        {
            cin>>num[i];
            pii p=get_five_two(num[i]);
            two[i]=p.second;
            five[i]=p.first;
            tot+=five[i];
        }
        for(int i=0;i<=200;i++)
        {
            for(int j=0;j<=10000;j++)
                dp[i][j]=INT_MIN;
        }
        dp[0][0]=0;
        for(ll i=1;i<=n;i++)
        {
            for(ll j=min(i,k);j>=1;j--)
            {
                for(ll kk=tot;kk>=five[i];kk--)
                    dp[j][kk]=max(dp[j][kk],dp[j-1][kk-five[i]]+two[i]);
            }
        }
        ll ans=0;
        for(ll i=1;i<=tot;i++)
            ans=max(ans,min(dp[k][i],i));//5和2最小的
        cout<<ans<<endl;
    }
    return 0;
}


}


Answer:
0 of the occurrences are only 2 and 5 multiplied, so the number of n, each of which is recorded by the number of 2 and 5 factors.



N number of selected K, obvious knapsack problem. If you apply the 01 Knapsack model directly, you cannot know the number of the previous state 2 and 5 when the state transitions.



Add one dimension and record the number of 5 (2 is also possible). Set the state transition equation
Dp[i][j][k]=max (Dp[[i−1][j][k],dp[i−1][j−1][k−five[i]]+two[i]) Dp[i][j][k]=max (dp[[i-1][j][k],dp[i-1][j-1][ K-five[i]]+two[i])
Indicates the maximum number of 2 in cases where the number of first I is selected J can have K 5.



So the final result is to find out the number of K selected, the least 2 and 5 is the number of 0



Note Using a scrolling array to become 2-dimensional, otherwise burst memory



Dp[j][k]=max (Dp[j][k],dp[j−1][k−five[i]]+two[i]) Dp[j][k]=max (Dp[j][k],dp[j-1][k-five[i]]+two[i])


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.