fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. ll phi(ll n) {
  6. ll result = n;
  7. for (ll p = 2; p * p <= n; ++p) {
  8. if (n % p == 0) {
  9. while (n % p == 0) n /= p;
  10. result -= result / p;
  11. }
  12. }
  13. if (n > 1) result -= result / n;
  14. return result;
  15. }
  16. int main() {
  17. ios::sync_with_stdio(0);
  18. cin.tie(0);
  19. cout.tie(0);
  20.  
  21. ll l, r, i = 2, n, d;
  22. int mod, t;
  23. cin >> mod >> t;
  24.  
  25. while (t--) {
  26. cin >> l >> r;
  27. n = 0;
  28. for (; l; l >>= 1, r <<= 1) {
  29. if (l % 2) {
  30. n += r;
  31. }
  32. }
  33. cout << phi(n) % mod << "\n";
  34. }
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0.01s 5408KB
stdin
998244353
3
1 2
2 5
5 7
stdout
1
4
24