Codeforces Round #135 (Div. 2)——B

来源:互联网 发布:des算法java实现 编辑:程序博客网 时间:2024/06/09 14:09
B. Special Offer! Super Price 999 Bourles!
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarpus is an amateur businessman. Recently he was surprised to find out that the market for paper scissors is completely free! Without further ado, Polycarpus decided to start producing and selling such scissors.

Polycaprus calculated that the optimal celling price for such scissors would be p bourles. However, he read somewhere that customers are attracted by prices that say something like "Special Offer! Super price 999 bourles!". So Polycarpus decided to lower the price a little if it leads to the desired effect.

Polycarpus agrees to lower the price by no more than d bourles so that the number of nines at the end of the resulting price is maximum. If there are several ways to do it, he chooses the maximum possible price.

Note, Polycarpus counts only the trailing nines in a price.

Input

The first line contains two integers p and d (1 ≤ p ≤ 10180 ≤ d < p) — the initial price of scissors and the maximum possible price reduction.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.

Output

Print the required price — the maximum price that ends with the largest number of nines and that is less than p by no more than d.

The required number shouldn't have leading zeroes.

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>using namespace std;#define maxn 30//题目大意:一个数字p,在最多减少d的情况下,p-x末尾数字'9'的个数最多(0≤x≤d)。//这个题的方向是,我们尽量让p的末尾0最多,然后减去一个1,后面的0就都变成9了。int a[maxn];int main(){    long long p,d,ans;    scanf("%I64d%I64d",&p,&d);    ans=++p;    for(long long t=10;; t*=10)    {        if(p%t>d) break;        ans=p-p%t;    }    printf("%I64d\n",ans-1);    return 0;}


原创粉丝点击