【CodeForces 219B】Special Offer! Super Price 999 Bourles


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 ≤ 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.

Examples
Input
1029 102
Output
999
Input
27191 17
Output
27189

題目大意:輸出不小於  p - d  的末尾 9 最多的數,若個數相同則輸出最大數
思路:模擬,依次將最後一位變爲9,判斷是否大於 p - d,每次用原數減去(當前位+1)* 10^位數,例如:345 - 6 = 339; 339 - 40 = 299……
           這道題卡的我要哭了,wa在第一個hhh,開始 c 沒初始化,結果破編譯器給我自動初始化了,交上去就不行了,和本地運行結果不一樣。後來又被 pow()函數坑,pow返回值是 double 型,就算強制轉換也會出現精度問題,最後自己寫了快速冪,終於A了,pow不敢用了
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
ll p,d,ans,a,t,c=1,cot,k;
ll fun(ll x,ll n){ //10 的 n次冪
	ll ans = 1;
	while (n) {
		if(n&1) ans=ans*x;
		x=x*x;
		n>>= 1;
	}
	return ans;
}
int main()
{
    cin>>p>>d;
    t=p;
    while(t >= p-d && c){
        ans=t;
        k=fun(10,cot);
        c=t/k;
        a=c%10;
        if(a!=9) t-=(a+1)*k;  //已經是9則跳過
        cot++;
    }
    cout<<ans<<endl;
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章