Special Offer! Super Price 999 Bourles!

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 ≤ 1018; 0 ≤ 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.

Sample Input

Input
1029 102
Output
999
Input
27191 17
Output
27189
 #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int nn(long long a)
{
int si=;
while(a)
{
a=a/;
si++;
}
return si;
} long long po(long long s,int y)
{
for(int i=;i<=y;i++)
{
s=s*;
}
return s;
} int main()
{
long long p,d,Q;
int n,i,j,k;
while(scanf("%I64d %I64d",&p,&d)!=EOF)
{
n=nn(p);
if(n==)
printf("%I64d\n",p);
else
for(i=;i<=n;i++)
{
Q=po(p/po(,n-i),n-i);
for(j=n-i;j>=;j--)
{
Q=Q+po(,j-);
}
if(Q==p)
{
printf("%I64d\n",Q);
break;
}
Q=po(p/po(,n-i)-,n-i);
for(j=n-i;j>=;j--)
{
Q=Q+po(,j-);
}
if(p-Q<=d)
{
printf("%I64d\n",Q);
break;
}
}
}
return ;
}

CodeForces 219B Special Offer! Super Price 999 Bourles!的更多相关文章

  1. 构造 Codeforces Round #135 (Div. 2) B. Special Offer! Super Price 999 Bourles!

    题目传送门 /* 构造:从大到小构造,每一次都把最后不是9的变为9,p - p MOD 10^k - 1,直到小于最小值. 另外,最多len-1次循环 */ #include <cstdio&g ...

  2. Special Offer! Super Price 999 Bourles!

    Description Polycarpus is an amateur businessman. Recently he was surprised to find out that the mar ...

  3. codeforces Rockethon 2015 C Second price auction [想法]

    传送门 C. Second price auction time limit per test 2 seconds memory limit per test 256 megabytes input ...

  4. codeforces 1282B2. K for the Price of One (Hard Version) (dp)

    链接 https://codeforces.com/contest/1282/problem/B2 题意: 商店买东西,商店有n个物品,每个物品有自己的价格,商店有个优惠活动,当你买恰好k个东西时可以 ...

  5. Codeforces 1383F - Special Edges(状态压缩+最大流)

    Codeforces 题目传送门 & 洛谷题目传送门 首先暴力显然是不行的,如果你暴力最大流过了我请你吃糖 注意到本题的 \(k\) 很小,考虑以此为突破口解题.根据最大流等于最小割定理,点 ...

  6. codeforces 1156E Special Segments of Permutation

    题目链接:https://codeforc.es/contest/1156/problem/E 题目大意: 在数组p中可以找到多少个不同的l,r满足. 思路: ST表+并查集. ST表还是需要的,因为 ...

  7. Codeforces 1156E Special Segments of Permutation(单调栈)

    可以用单调栈直接维护出ai所能覆盖到的最大的左右范围是什么,然后我们可以用这个范围暴力的去查询这个区间的是否有满足的点对,一个小坑点,要对左右区间的大小进行判断,只需要去枚举距离i最近的一段区间去枚举 ...

  8. Codeforces 1156E Special Segments of Permutation(启发式合并)

    题意: 给一个n的排列,求满足a[l]+a[r]=max(l,r)的(l,r)对数,max(l,r)指的是l到r之间的最大a[p] n<=2e5 思路: 先用单调栈处理出每个点能扩展的l[i], ...

  9. Codeforces Round #135 (Div. 2)

    A. k-String 统计每个字母出现次数即可. B. Special Offer! Super Price 999 Bourles! 枚举末尾有几个9,注意不要爆掉\(long\ long\)的范 ...

随机推荐

  1. 关于linux的systemd的一些事

    1. 输出运行失败的单元: systemctl --failed 2. 所有的单元文件存放在 /usr/lib/systemd/system/ 和 /etc/systemd/system/ 这两个目录 ...

  2. yii2多语言

    1.页面视图(我放在了布局文件main.php中): <a href="javascript:;" onclick="changeLanguage('zh-CN') ...

  3. jquery tab键转换

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. ubuntu的目录结构

    /:根目录,一般根目录下只存放目录,不要存放文件,/etc./bin./dev./lib./sbin应该和根目录放置在一个分区中 /bin:/usr/bin:可执行二进制文件的目录,如常用的命令ls. ...

  5. Java中的get()和set()方法

    对于JAVA初学者来说,set和get这两个方法似乎已经很熟悉了,这两个方法是JAVA变成中的基本用法,也是出现频率相当高的两个方法. 如果你对于这两个方法还有困惑甚至完全不知道这两个方法是做什么的, ...

  6. 目前主流的国产WebApp框架

    1.FrozenUI:QQ 2.WeUI:WeChat 3.SUI:Taobao

  7. "服务器 '' 上的 MSDTC 不可用。"

    调试程序的时候,出现异常消息“The underlying provider failed on Open”: 详细信息:和{"服务器 '' 上的 MSDTC 不可用."}: 网上 ...

  8. YUI3 CSS

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  9. django ORM model filter 条件过滤,及多表连接查询、反向查询,某字段的distinct

    版权归作者所有,任何形式转载请联系作者.作者:petanne(来自豆瓣)来源:https://www.douban.com/note/301166150/ 1.多表连接查询:感觉django太NX了. ...

  10. linux设备驱动归纳总结(八):3.设备管理的分层与面向对象思想【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-110738.html linux设备驱动归纳总结(八):3.设备管理的分层与面向对象思想 xxxxxx ...