codeforces 415B Mashmokh and Tokens

举报
Linux猿 发表于 2021/08/05 01:40:41 2021/08/05
【摘要】 题目链接~~> 做题感悟:在比赛时老马想到了这个方法但是这种方法在无形当中就被忽略了,导致最后十几分钟写二分的方法也没写出来。 解题思路:1)员工在保证钱最多的情况下,剩下的 tokens 尽量多,so  ( w * a ) % b  但这并不是最终结果(这样交了一次果断wa),因为 这是 w*a 对 b 取余的结果,这样得到...

题目链接~~>

做题感悟:在比赛时老马想到了这个方法但是这种方法在无形当中就被忽略了,导致最后十几分钟写二分的方法也没写出来。

解题思路:1)员工在保证钱最多的情况下,剩下的 tokens 尽量多,so  ( w * a ) % b  但这并不是最终结果(这样交了一次果断wa),因为 这是 w*a 对 b 取余的结果,这样得到的并不是剩下的 tokens ,还必须除以 a ;

                  2)二分(补充):你得到的钱最多为 w * a / b ,但是有可能取少于 w 个也能达到这些钱,so 只要不断用二分在 0 ~ w 之间找到最小的 y 使得 y*a/b = w*a / b 即可。

代码:


  
  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<map>
  4. #include<stack>
  5. #include<string>
  6. #include<string.h>
  7. #include<stdlib.h>
  8. #include<math.h>
  9. #include<vector>
  10. #include<queue>
  11. #include<algorithm>
  12. using namespace std ;
  13. #define lld __int64
  14. const double PI = 3.1415926 ;
  15. const double esp = 1e-4 ;
  16. const int md= 2810778 ;
  17. const int INF = 99999999 ;
  18. const int MX = 1000005 ;
  19. int main()
  20. {
  21. lld n,a,b ;
  22. while(~scanf("%I64d%I64d%I64d",&n,&a,&b))
  23. {
  24. lld x ;
  25. for(lld i=0 ;i<n ;i++)
  26. {
  27. scanf("%I64d",&x) ;
  28. if(!i) printf("%I64d",(x*a)%b/a) ;
  29. else printf(" %I64d",(x*a)%b/a) ;
  30. }
  31. printf("\n") ;
  32. }
  33. return 0 ;
  34. }

代码:


  
  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<map>
  4. #include<stack>
  5. #include<string>
  6. #include<string.h>
  7. #include<stdlib.h>
  8. #include<math.h>
  9. #include<vector>
  10. #include<queue>
  11. #include<algorithm>
  12. using namespace std ;
  13. #define lld __int64
  14. const double PI = 3.1415926 ;
  15. const double esp = 1e-4 ;
  16. const int md= 2810778 ;
  17. const int INF = 99999999 ;
  18. const int MX = 1000005 ;
  19. lld a,b,n ;
  20. lld binary_search(lld le,lld rt,lld n) // 二分找最小的 tokens
  21. {
  22. lld mid ;
  23. while(le<rt)
  24. {
  25. mid=(le+rt)/2 ;
  26. if(mid*a/b==n)
  27. rt=mid ;
  28. else le=mid+1 ;
  29. }
  30. return le ;
  31. }
  32. int main()
  33. {
  34. while(~scanf("%I64d%I64d%I64d",&n,&a,&b))
  35. {
  36. lld x ;
  37. for(lld i=0 ;i<n ;i++)
  38. {
  39. scanf("%I64d",&x) ;
  40. lld le=0,rt=x ;
  41. lld mx= binary_search(le,rt,(x*a)/b) ;// 返回用了多少tokens
  42. if(!i)
  43. printf("%I64d",x-mx) ;
  44. else printf(" %I64d",x-mx) ;
  45. }
  46. printf("\n") ;
  47. }
  48. return 0 ;
  49. }


 

文章来源: blog.csdn.net,作者:Linux猿,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/nyist_zxp/article/details/23792171

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。