博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AC日记——Weird Rounding Codeforces 779b
阅读量:4472 次
发布时间:2019-06-08

本文共 2700 字,大约阅读时间需要 9 分钟。

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, ifk = 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 by10k. 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.

 

 思路:

  dfs~~~~;

 

来,上代码:

#include 
#include
#include
using namespace std;int n,k,len,ans=0x7fffffff,p;char ch[15];bool if_[15];bool check(){ int bzero=0; bool zero=true; long long int pos=0; for(int i=1;i<=len;i++) { if(if_[i]) continue; if(zero) { if(ch[i]=='0') bzero++; else { zero=false; pos=pos*10+ch[i]-'0'; } } else pos=pos*10+ch[i]-'0'; } if(bzero==1&&pos==0) return true; if(bzero>1) return false; if(pos%p==0) return true; return false;}void dfs(int step,int ci){ if(step>=ans) return ; if(check()) { ans=step; return ; } for(int i=ci+1;i<=len;i++) { if(!if_[i]) { if_[i]=true; dfs(step+1,i); if_[i]=false; } }}int main(){ cin>>ch+1; cin>>k; p=1; for(int i=1;i<=k;i++) p=p*10; len=strlen(ch+1); dfs(0,0); cout<

 

转载于:https://www.cnblogs.com/IUUUUUUUskyyy/p/6556008.html

你可能感兴趣的文章
HDU 1010 Temper of the bone(深搜+剪枝)
查看>>
如何使用BAT文件批量运行SQL语句,并保存执行结果
查看>>
JS中==和===的区别
查看>>
python—命名规范
查看>>
.Net常用正则判断方法
查看>>
我的第一个python web开发框架(39)——后台接口权限访问控制处理
查看>>
Redis MSET的极限在哪里
查看>>
iOS基础知识----SQLite数据库操作
查看>>
Linux命令之chown
查看>>
Linux命令之telnet
查看>>
java多线程下载
查看>>
取日期
查看>>
想你了
查看>>
敏捷冲刺每日报告二(Java-Team)
查看>>
《算法导论》——数论
查看>>
BaseAnimation是基于开源的APP,致力于收集各种动画效果(最新版本1.3)
查看>>
Linux的基本命令
查看>>
6.4 总结(关于正确率)
查看>>
Ubuntu-Python2.7安装 scipy,numpy,matplotlib
查看>>
JAVA与.NET的相互调用——利用JNBridge桥接模式实现远程通讯
查看>>