CF 135 DIV2 B Special Offer! Super Price 999 Bourles!

本文探讨了如何解决Codeforces竞赛中特定类型的题目,通过深入分析问题,提出了一种有效的方法来求解最优解。重点在于理解题意、优化算法选择,并通过实例演示了实现过程和关键细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

来源:http://codeforces.com/contest/219/problem/B

题意:就是一个物品有一个价格,这个价格可以最多降低d,求在所下降价格不超过d的情况下,能够使价格有最多的9且价格最高.拿样例来说,

1029 102
原价为1029,最多可下降102元,在符合条件的范围内,能够取得最多的9且价格最高的是999.若没有 符合条件的情况,则输出原价。
思路:首先求出原价的位数,然后计算最多有多少个9,和原价比较以及判断是否符合条件。之后就是一个模拟的过程,一点一点加,直到找到符合条件的借。中间有一些细节需要注意,而且中间运算还有可能超过__int64,都需要处理一下。
代码:
#include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;

typedef unsigned __int64 LL;
int fun(LL x){
    int sum = 0;
    while(x){
      sum++;
      x /= 10;
    }
    return sum ;
}
LL cal(int x){
    LL sum = 0;
    for(int i = 1; i <= x; ++i){
       sum = sum * 10 + 9;
    }
    return sum;
}
LL mi(int cnt){
    LL s = 1;
    for(int i = 1; i <= cnt; ++i)
        s *= 10;
    return s;
}
int main(){
    LL p,d;
    while(scanf("%I64d%I64d",&p,&d) != EOF){
       LL value = p - d;
       int cnt = fun(p);
       LL ans = cal(cnt);
       if(ans >= value  && ans <= p){
          printf("%I64d\n",ans);
       }
       else{
         LL x = value % 10;
         LL y = value / 10;
         LL z = y * 10 + 9;
		 if(z > p){
		   z = z - 9;
		   printf("%I64d\n",p);
		   continue;
		 }
         LL num = 0,xx = 0,yy = 0;
         LL cnt = 1;
         while(z <= p){
             cnt++;
           xx = z / mi(cnt);
           yy = z % mi(cnt);
           z = xx * mi(cnt) + cal(cnt);
           num = z;
         }
         num = xx * mi(cnt) + yy;
         cnt--;
         while(num <= p){
           num += mi(cnt);
         }
         printf("%I64d\n",num - mi(cnt));
       }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值