【链接】:CF

【题意】:对于一个数n,每次加一的代价是a,每次减一的代价是b,求被m整除时的最小代价。

【分析】:分情况讨论,自己多举几个栗子。

【代码】:

  1. #include<cstdio>
  2. #include<string>
  3. #include<cstdlib>
  4. #include<cmath>
  5. #include<iostream>
  6. #include<cstring>
  7. #include<set>
  8. #include<queue>
  9. #include<algorithm>
  10. #include<vector>
  11. #include<map>
  12. #include<cctype>
  13. #include<stack>
  14. #include<sstream>
  15. #include<list>
  16. #include<assert.h>
  17. #include<bitset>
  18. #include<numeric>
  19. using namespace std;
  20. typedef long long ll;
  21. typedef unsigned long long ULL;
  22. typedef pair<int,int> P;
  23. const int INF = 0x3f3f3f3f;
  24. const ll LNF = 1e18;
  25. const int maxn = 1e3 + 100;
  26. const int maxm = 100;
  27. const double PI = acos(-1.0);
  28. const double eps = 1e-8;
  29. //const int dx[] = {-1,1,0,0,1,1,-1,-1};
  30. //const int dy[] = {0,0,1,-1,1,-1,1,-1};
  31. int dx[] = {-1,0,1,0};
  32. int dy[] = {0,1,0,-1};
  33. // ио/ср/об/вС
  34. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  35. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  36. const int dir[6][3]={ {0,0,1},{0,0,-1},{-1,0,0},{1,0,0},{0,1,0},{0,-1,0} };
  37. //int dir[4][2]= {{-1,0},{0,1},{1,0},{0,-1}};
  38. ll n,m;
  39. ll a,b;
  40. //对于一个数n,每次加一的代价是a,每次减一的代价是b,求被m整除时的最小代价
  41. int main()
  42. {
  43. while(cin>>n>>m>>a>>b)
  44. {
  45. ll ans;
  46. if(n%m==0)
  47. {
  48. cout<<"0"<<endl;
  49. continue;
  50. }
  51. else
  52. {
  53. if(n>m)
  54. {
  55. ans=min((n-m*(n/m))*b,(((n/m+1)*m-n)*a));
  56. }
  57. else ans=min(n*b,(m-n)*a);
  58. }
  59. cout<<ans<<endl;
  60. }
  61. }
  62. /*
  63. 9 7
  64. 9/7=1
  65. 9%7=2

  66. 7-2*2*2=1
  67. 4 8 1 5
  68. 7/2=3
  69. 7%2=1
  70. 8 3 3 7
  71. 8/3=2
  72. 8%3=2
  73. 2*7=14↓
  74. 1*3=3↑
  75. 9 6
  76. 6 6
  77. 12 6
  78. 9/6=1
  79. 9%6=3
  80. */

CF 990A. Commentary Boxes【数学/模拟】的更多相关文章

  1. CF 450E Jzzhu and Apples 数学+模拟

    E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. HDU 5810 Balls and Boxes 数学

    Balls and Boxes 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5810 Description Mr. Chopsticks is i ...

  3. C. Okabe and Boxes 思维 模拟 or 线段树

    C. Okabe and Boxes 这个题目是一个有点思维的模拟,当时没有想到, 思维就是这个栈的排序这里,因为每次直接排序肯定会t的,所以不可以这么写,那怎么表示排序呢? 就是直接把栈清空,如果栈 ...

  4. 2017 Multi-University Training Contest - Team 9 1004&&HDU 6164 Dying Light【数学+模拟】

    Dying Light Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  5. CF GukiZ hates Boxes 【二分+贪心】

    Professor GukiZ is concerned about making his way to school, because massive piles of boxes are bloc ...

  6. CF 999B. Reversing Encryption【模拟/string reverse】

    [链接]:CF [代码]: #include<bits/stdc++.h> #define PI acos(-1.0) #define pb push_back #define F fir ...

  7. zzulioj--1825-- 会长爱数学(模拟)

    1825: 会长爱数学 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 6  Solved: 2 SubmitStatusWeb Board Descr ...

  8. CF #366 DIV2 C. Thor 模拟 queue/stack降低复杂度

    C. Thor time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  9. 北京网络赛G BOXES 大模拟+BFS

    题目描述 Description There is a strange storehouse in PKU. In this storehouse there are n slots for boxe ...

随机推荐

  1. (转)Foundation-性能优化之NSDateFormatter

    性能优化之NSDateFormatter 为什么要优化NSDateFormatter? 首先,过度的创建NSDateFormatter用于NSDate与NSString之间转换,会导致App卡顿,打开 ...

  2. 【bzoj2957】楼房重建 分块+二分查找

    题目描述 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子.为了简化问题,我们考虑这些事件发生在一个二 ...

  3. 国内各运营商(ISP)IP段表

    国内各运营商(ISP)IP段表 来源:http://bbs.hh010.com/forum.php?mod=viewthread&tid=490529&orderby=dateline ...

  4. BZOJ3533 [Sdoi2014]向量集 【线段树 + 凸包 + 三分】

    题目链接 BZOJ3533 题解 我们设询问的向量为\((x_0,y_0)\),参与乘积的向量为\((x,y)\) 则有 \[ \begin{aligned} ans &= x_0x + y_ ...

  5. bzoj 1977 洛谷P4180 严格次小生成树

    Description: 给定一张N个节点M条边的无向图,求该图的严格次小生成树.设最小生成树边权之和为sum,那么严格次小生成树就是边权之和大于sum的最小的一个 Input: 第一行包含两个整数N ...

  6. JAVA List 一边遍历一边删除元素

    JAVA List 一边遍历一边删除元素,报java.util.ConcurrentModificationException异常 2015年02月10日 14:42:49 zhanzkw 阅读数:3 ...

  7. POJ2240:Arbitrage(最长路+正环)

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29374   Accepted: 12279 题目链接: ...

  8. Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)

    F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...

  9. Kafka自我学习2-Zookeeper cluster

    Test enviroment : zoo1, zoo2, zoo3 cluster 1. Install zookeeper, package in kafka [root@zoo1 ~]# pwd ...

  10. 【BZOJ5005】乒乓游戏 [线段树][并查集]

    乒乓游戏 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output Sample Input 5 1 1 5 1 5 11 2 ...