fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <algorithm>
  5. #include <set>
  6. using namespace std;
  7.  
  8. int cover(int l1, int r1, int l2, int r2){
  9. return max(min(r1, r2) - max(l1, l2) + 1, 0);
  10. }
  11.  
  12. int main() {
  13. cin.tie(0)->sync_with_stdio(0);
  14. int t;
  15. cin >> t;
  16. while (t--) {
  17. int k, l1, r1, l2, r2, sum = 0;
  18. cin >> k >> l1 >> r1 >> l2 >> r2;
  19. while(r2 >= l1){
  20. sum += max(min(r1, r2) - max(l1, l2) + 1, 0);
  21. l2 = (l2 + k - 1)/k;
  22. r2 = r2 / k;
  23. }
  24. cout << sum << endl;
  25. }
  26. }
Success #stdin #stdout 0s 5256KB
stdin
5
2 2 6 2 12
2 1 1000000000 1 1000000000
3 5 7 15 63
1000000000 1 5 6 1000000000
15 17 78 2596 20914861
stdout
12
1999999987
6
1
197