Codeforces Round #402 (Div. 2)B. Weird Rounding【暴力枚举】

B. Weird Rounding
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp is crazy about round numbers. He especially likes the numbers divisible by 10k.

In the given number of n Polycarp wants to remove the least number of digits to get a number that is divisible by 10k. For example, if k = 3, in the number 30020 it is enough to delete a single digit (2). In this case, the result is 3000 that is divisible by 103 = 1000.

Write a program that prints the minimum number of digits to be deleted from the given integer number n, so that the result is divisible by 10k. The result should not start with the unnecessary leading zero (i.e., zero can start only the number 0, which is required to be written as exactly one digit).

It is guaranteed that the answer exists.

Input

The only line of the input contains two integer numbers n and k (0 ≤ n ≤ 2 000 000 000, 1 ≤ k ≤ 9).

It is guaranteed that the answer exists. All numbers in the input are written in traditional notation of integers, that is, without any extra leading zeros.

Output

Print w — the required minimal number of digits to erase. After removing the appropriate w digits from the number n, the result should have a value that is divisible by 10k. The result can start with digit 0 in the single case (the result is zero and written by exactly the only digit 0).

Examples
Input
30020 3
Output
1
Input
100 9
Output
2
Input
10203049 2
Output
3
Note

In the example 2 you can remove two digits: 1 and any 0. The result is number 0 which is divisible by any number.


题目大意:

给你一个数N,让你去掉最少的数字,使得剩余的所组成的数(相对位子不能改变)可以整除10^k.不能包含前导0.


思路:


因为n的范围并不大,是在int之内的,所以我们可以将数字进行位压缩,对于每一位来讲,只有两种情况,要么是留下,要么是删除。

那么我们只要2^10去枚举每一位的状态暴力判断即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
int vis[15];
int a[15];
int cnt;
int output;
int n,k;
void Slove()
{
    int f=0;
    int cont=0;
    int sum=0;
    for(int i=0;i<cnt;i++)
    {
        if(vis[i]==1)
        {
            if(f==0&&a[i]==0)f=1;
            else if(f==1&&a[i]==0)return ;
            if(a[i]!=0)f=2;
            sum=sum*10+a[i];
        }
        else cont++;
    }
    if(sum%(int)pow(10,k)==0)
    {
        output=min(output,cont);
    }
}
void Dfs(int now)
{
    if(now==cnt)
    {
        Slove();
        return ;
    }
    vis[now]=1;
    Dfs(now+1);
    vis[now]=0;
    Dfs(now+1);
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        memset(vis,0,sizeof(vis));
        cnt=0;
        while(n)
        {
            a[cnt++]=n%10;
            n/=10;
        }
        reverse(a,a+cnt);
        output=0x3f3f3f3f;
        Dfs(0);
        printf("%d\n",output);
    }
}












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值