Advertisement
MinhNGUYEN2k4

Untitled

Sep 4th, 2021
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. //Nguyen Huu Hoang Minh
  2. #include <bits/stdc++.h>
  3. #define sz(x) int(x.size())
  4. #define all(x) x.begin(),x.end()
  5. #define reset(x) memset(x, 0,sizeof(x))
  6. #define pb push_back
  7. #define mp make_pair
  8. #define fi first
  9. #define se second
  10. #define N 2005
  11. #define remain(x) if (x > MOD) x -= MOD
  12. #define ii pair<int, int>
  13. #define iiii pair< ii , ii >
  14. #define viiii vector< iiii >
  15. #define vi vector<int>
  16. #define vii vector< ii >
  17. #define bit(x, i) (((x) >> (i)) & 1)
  18. #define Task "test"
  19. #define int long long
  20.  
  21. using namespace std;
  22.  
  23. typedef long double ld;
  24. const int inf = 1e10;
  25. const int minf = -1e10;
  26.  
  27. int t;
  28. int n, m;
  29.  
  30. void readfile()
  31. {
  32.     ios_base::sync_with_stdio(false);
  33.     cin.tie(0);cout.tie(0);
  34.     if (fopen(Task".inp","r"))
  35.     {
  36.         freopen(Task".inp","r",stdin);
  37.         //freopen(Task".out","w",stdout);
  38.     }
  39.     cin >> t;
  40. }
  41.  
  42. void solve(){
  43.     cin >> n >> m;
  44.     //1,2,..,m-1,0,1,2,...,m-1,0,1,2,...,y
  45.     int x = n/m;
  46.     int y = n%m;
  47.     int kq = 0;
  48.     //TH1 : 1 <= a < b <= x*m
  49.     //a%m = 0 -> b%m = 0 -> co x*(x-1) cap (ko xet tinh chat a < b)
  50.     kq += x*(x-1);
  51.     //a%m = d -> b%m = m-d -> có x*x cap, co m-1 so
  52.     kq += (m-1)*x*x;
  53.     //if m%2==0 -> loai bo cap trung d = m-d
  54.     if (m%2==0) kq -= x;
  55.     //bo cac cap a > b
  56.     kq /= 2;
  57.  
  58.     //TH2: 1 <= a <= x*m < b <= y (neu y>0)
  59.     //b%m = d (co y so) -> a%m = m-d  -> moi y so co x cach chon
  60.     if (y) kq += y*x;
  61.  
  62.     //TH3: m*x <= a < b <= y
  63.     //a%m = d (d < m/2) -> b%m = m-d(m-d > m/2) -> co y-m/2 cach chon
  64.     if (y>m/2) kq += y-m/2;
  65.     cout << kq << '\n';
  66. }
  67.  
  68. void proc()
  69. {
  70.     while (t--){
  71.         solve();
  72.     }
  73. }
  74.  
  75. signed main()
  76. {
  77.     readfile();
  78.     proc();
  79.     return 0;
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement