fork download
  1. #include<stdio.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n = 0, k = 0;
  6.  
  7. scanf("%i", &n);
  8. scanf("%i", &k);
  9.  
  10. int xTotal = 0;
  11.  
  12. for (int i = 0; i < n; i++) {
  13. // initalize left and right for this segment
  14. int l, r;
  15. scanf("%i", &l);
  16. scanf("%i", &r);
  17.  
  18. int x = (r-l)+1; // calculate how many nums are (x) (the value of this current set)
  19. xTotal+=x;
  20. }
  21.  
  22. // find the nearest number divisible by k
  23.  
  24. int currentNumber = xTotal;
  25. while (currentNumber%k != 0) {
  26. currentNumber++; // only increment since the only moves we can do is increase (l-1, r+1)
  27. }
  28.  
  29. printf("%i", currentNumber-xTotal);
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0s 5292KB
stdin
2 3
1 2
3 4
stdout
2