Advertisement
MaksymO

Number theory

Feb 5th, 2023
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <stack>
  5. #include <deque>
  6. #include <math.h>
  7. #include <iomanip>
  8. #define pb push_back
  9. #define int long long
  10. using namespace std;
  11.  
  12. const int N = 2e5 + 5;
  13. const int M = 1e9 + 7;
  14.  
  15. ///O(log N), every second step we divide n by 2
  16. //pow(12, 2), 143.999999999 -> 143
  17. //pow(a, n);
  18. int binpow(int a, int n) {
  19.     if (n == 0) return 1;
  20.     if (n % 2 == 1) return binpow(a, n - 1) * a % M;
  21.     int x = binpow(a, n / 2);
  22.     return x * x % M;
  23. }
  24.  
  25. int binpowNonRec(int a, int n) {
  26.     int res = 1;
  27.     ///0 --- false
  28.     ///!=0 --- true
  29.     ///How it works: look at binary representation of n
  30.     while (n) ///while (n != 0)
  31.         if (n % 2) {///if (n % 2 != 0)
  32.             res *= a;
  33.             res %= M;
  34.             n--;
  35.         } else {
  36.             a *= a;
  37.             a %= M;
  38.             n /= 2;
  39.         }
  40.     return res;
  41. }
  42.  
  43. ///how many divisors are there? O(cubic root of n) = O(n^(1/3))
  44. ///O(sqrt(N))
  45. vector<int> findDivisors(int n) {
  46.     vector<int> res;
  47.     for (int a = 1; a * a <= n; a++)
  48.         if (n % a == 0) {
  49.             res.push_back(a);
  50.             if (n / a != a)
  51.                 res.push_back(n / a);
  52.         }
  53.     sort(res.begin(), res.end());
  54.     return res;
  55. }
  56.  
  57. bool isPrime(int n) {
  58.     if (n == 1) return 0;
  59.     for (int a = 2; a * a <= n; a++)
  60.         if (n % a == 0)
  61.             return 0;
  62.     return 1;
  63. }
  64.  
  65. vector<pair<int, int>> factorize(int n) {
  66.     vector<pair<int, int>> res;
  67.     for (int p = 2; p * p <= n; p++)
  68.         if (n % p == 0) {
  69.             int a = 0;
  70.             while (n % p == 0) {
  71.                 n /= p;
  72.                 a++;
  73.             }
  74.             res.push_back({p, a});
  75.         }
  76.     if (n != 1)
  77.         res.push_back({n, 1});
  78.     return res;
  79. }
  80.  
  81. //O(log MAX)
  82. int gcd(int a, int b) {
  83.     /*
  84.     if (a == 0)
  85.         return b;
  86.     if (b == 0)
  87.         return a;*/
  88.     if (a == 0 || b == 0)
  89.         return a + b;
  90.     if (a <= b)
  91.         return gcd(a, b % a);
  92.     else
  93.         return gcd(a % b, b);
  94. }
  95.  
  96.  
  97. ///O(N log log N) ---> O(N)
  98. ///factorize the numbers
  99. ///marked[i] = not 0/1, but the smallest divisor of i
  100. vector<int> sieve(int N) {
  101.     vector<int> marked(N + 1);
  102.     ///bitset --- C++ builtin structure
  103.     vector<int> primes;
  104.     for (int i = 2; i <= N; i++)
  105.         if (!marked[i]) {
  106.             marked[i] = i; ///i is prime, so the smallest divisor of i is i
  107.             primes.push_back(i);
  108.             for (long long j = i; j * i <= N; j++)
  109.                 if (marked[j * i] == 0)
  110.                     marked[j * i] = i;
  111.         }
  112.     return marked;
  113. }
  114.  
  115. signed main()
  116. {
  117.     ios_base::sync_with_stdio(0);
  118.     cin.tie(0);
  119.     //cout << binpow(102, 2103019) << ' ' << binpowNonRec(102, 2103019);
  120.     /*vector<int> d = findDivisors(16);
  121.     for (auto div : d)
  122.         cout << div << ' ';*/
  123.  
  124.     /*
  125.     vector<pair<int, int>> fact = factorize(1000000007);
  126.     ///fact.size() is very small, even smaller than logarithm
  127.     for (auto [p, a] : fact) {
  128.         cout << p << ' ' << a << "\n";
  129.     }*/
  130.  
  131.     //cout << gcd(13, 37) << ' ' << gcd(12, 15) << ' ' << __gcd(13, 37);
  132.     /*vector<int> p = sieve(100);
  133.     for (auto pr : p)
  134.         cout << pr << ' ';
  135. */
  136. /// a lot of relatively small numbers -- it's fine.
  137.     vector<int> d = sieve(1000);
  138.     int x = 124;
  139.     while (x != 1) {
  140.         cout << d[x] << ' '; ///smallest divisor of x
  141.         x = x / d[x];
  142.     }
  143.     return 0;
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement