diff options
Diffstat (limited to '')
| -rwxr-xr-x | 07_mathematics/a.out | bin | 0 -> 48984 bytes | |||
| -rw-r--r-- | 07_mathematics/common_divisors_1081.cpp | 53 | ||||
| -rw-r--r-- | 07_mathematics/counting_divisors_1713.cpp | 34 | ||||
| -rw-r--r-- | 07_mathematics/exponentiation_1095.cpp | 19 | ||||
| -rw-r--r-- | 07_mathematics/exponentiation_ii_1712.cpp | 20 | ||||
| -rw-r--r-- | 07_mathematics/josephus_queries_2164.cpp | 17 |
6 files changed, 143 insertions, 0 deletions
diff --git a/07_mathematics/a.out b/07_mathematics/a.out new file mode 100755 index 0000000..d03560d --- /dev/null +++ b/07_mathematics/a.out | |||
| Binary files differ | |||
diff --git a/07_mathematics/common_divisors_1081.cpp b/07_mathematics/common_divisors_1081.cpp new file mode 100644 index 0000000..497ce4b --- /dev/null +++ b/07_mathematics/common_divisors_1081.cpp | |||
| @@ -0,0 +1,53 @@ | |||
| 1 | #include <array> | ||
| 2 | #include <bitset> | ||
| 3 | #include <iostream> | ||
| 4 | |||
| 5 | // This method is very different (and more complicated) than the one used | ||
| 6 | // in the official solution. | ||
| 7 | // First we save in spf[i] the smallest prime number that divides i. | ||
| 8 | // Then we initialize an array d with d[i] being 1 if i is in the input. | ||
| 9 | // Then we loop backwards and we search all divisors of the numbers marked | ||
| 10 | // in d. For each number we encounter, we look at its maximal divisors. | ||
| 11 | // If any of them was already found, we update our candidate solution. | ||
| 12 | // To avoid looking at a divisor more than once, we save in d[i] the | ||
| 13 | // smallest prime we want to continue diving i by to find more divisors. | ||
| 14 | |||
| 15 | constexpr size_t max = 1000001; | ||
| 16 | std::array<size_t, max> spf; // Smallest prime factor of i | ||
| 17 | std::array<size_t, max> d; | ||
| 18 | |||
| 19 | int main() { | ||
| 20 | for (size_t i = 2; i < max; i++) { | ||
| 21 | if (spf[i] != 0) continue; | ||
| 22 | spf[i] = i; | ||
| 23 | for (size_t j = 2; i*j < max; j++) | ||
| 24 | if (spf[i*j] == 0) | ||
| 25 | spf[i*j] = i; | ||
| 26 | } | ||
| 27 | |||
| 28 | size_t n, sol{1}; | ||
| 29 | std::cin >> n; | ||
| 30 | for (size_t i = 0; i < n; i++) { | ||
| 31 | size_t x; | ||
| 32 | std::cin >> x; | ||
| 33 | if (d[x] != 0) sol = std::max(sol, x); | ||
| 34 | d[x] = 1; | ||
| 35 | } | ||
| 36 | |||
| 37 | for (size_t i = max-1; i >= sol; i--) { | ||
| 38 | if (d[i] == 0) continue; | ||
| 39 | |||
| 40 | // Loop over maximal divisors of i | ||
| 41 | size_t y{i}; | ||
| 42 | while (y != 1) { | ||
| 43 | size_t p = spf[y]; | ||
| 44 | if (i/p < sol) break; | ||
| 45 | if (p >= d[i]) { | ||
| 46 | if (d[i/p] != 0) sol = std::max(sol, i/p); | ||
| 47 | d[i/p] = p; | ||
| 48 | } | ||
| 49 | while (y % p == 0) y /= p; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | std::cout << sol << "\n"; | ||
| 53 | } | ||
diff --git a/07_mathematics/counting_divisors_1713.cpp b/07_mathematics/counting_divisors_1713.cpp new file mode 100644 index 0000000..fe30ab4 --- /dev/null +++ b/07_mathematics/counting_divisors_1713.cpp | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | #include <array> | ||
| 2 | #include <iostream> | ||
| 3 | |||
| 4 | constexpr size_t max = 1000001; | ||
| 5 | std::array<size_t, max> spf; // Smallest prime factor of i | ||
| 6 | |||
| 7 | size_t ndiv(size_t x) { | ||
| 8 | size_t n{1}, d{0}, e{0}; | ||
| 9 | for (size_t i = x; i > 1; i /= spf[i]) { | ||
| 10 | if (spf[i] != d) { | ||
| 11 | n *= e+1; | ||
| 12 | d = spf[i]; | ||
| 13 | e = 1; | ||
| 14 | } else e++; | ||
| 15 | } | ||
| 16 | return n * (e+1); | ||
| 17 | } | ||
| 18 | |||
| 19 | int main() { | ||
| 20 | for (size_t i = 2; i < max; i++) { | ||
| 21 | if (spf[i] != 0) continue; | ||
| 22 | spf[i] = i; | ||
| 23 | for (size_t j = 2; i*j < max; j++) | ||
| 24 | if (spf[i*j] == 0) | ||
| 25 | spf[i*j] = i; | ||
| 26 | } | ||
| 27 | |||
| 28 | size_t n, x; | ||
| 29 | std::cin >> n; | ||
| 30 | for (size_t i = 0; i < n; i++) { | ||
| 31 | std::cin >> x; | ||
| 32 | std::cout << ndiv(x) << "\n"; | ||
| 33 | } | ||
| 34 | } | ||
diff --git a/07_mathematics/exponentiation_1095.cpp b/07_mathematics/exponentiation_1095.cpp new file mode 100644 index 0000000..1ba8509 --- /dev/null +++ b/07_mathematics/exponentiation_1095.cpp | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | #include <iostream> | ||
| 2 | |||
| 3 | static constexpr unsigned long long MOD = 1000000007; | ||
| 4 | |||
| 5 | unsigned long long pow(unsigned long long a, unsigned long long b) { | ||
| 6 | if (b == 0) return 1; | ||
| 7 | if (a == 0) return 0; | ||
| 8 | if (b % 2 == 0) return pow(a*a % MOD, b/2) % MOD; | ||
| 9 | return (a * pow(a, b-1)) % MOD; | ||
| 10 | } | ||
| 11 | |||
| 12 | int main() { | ||
| 13 | unsigned long long n, a, b; | ||
| 14 | std::cin >> n; | ||
| 15 | for (unsigned long long i = 0; i < n; i++) { | ||
| 16 | std::cin >> a >> b; | ||
| 17 | std::cout << pow(a, b) << "\n"; | ||
| 18 | } | ||
| 19 | } | ||
diff --git a/07_mathematics/exponentiation_ii_1712.cpp b/07_mathematics/exponentiation_ii_1712.cpp new file mode 100644 index 0000000..6a894f5 --- /dev/null +++ b/07_mathematics/exponentiation_ii_1712.cpp | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | #include <iostream> | ||
| 2 | |||
| 3 | static constexpr unsigned long long MOD = 1000000007; | ||
| 4 | |||
| 5 | unsigned long long pow(unsigned long long a, unsigned long long b, | ||
| 6 | unsigned long long mod) { | ||
| 7 | if (b == 0) return 1; | ||
| 8 | if (a == 0) return 0; | ||
| 9 | if (b % 2 == 0) return pow(a*a % mod, b/2, mod) % mod; | ||
| 10 | return (a * pow(a, b-1, mod)) % mod; | ||
| 11 | } | ||
| 12 | |||
| 13 | int main() { | ||
| 14 | unsigned long long n, a, b, c; | ||
| 15 | std::cin >> n; | ||
| 16 | for (unsigned long long i = 0; i < n; i++) { | ||
| 17 | std::cin >> a >> b >> c; | ||
| 18 | std::cout << pow(a, pow(b, c, MOD-1), MOD) << "\n"; | ||
| 19 | } | ||
| 20 | } | ||
diff --git a/07_mathematics/josephus_queries_2164.cpp b/07_mathematics/josephus_queries_2164.cpp new file mode 100644 index 0000000..b17f3c9 --- /dev/null +++ b/07_mathematics/josephus_queries_2164.cpp | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | #include <iostream> | ||
| 2 | |||
| 3 | int f(int n, int k, int m) { | ||
| 4 | int h = n/2 + m*(n%2); | ||
| 5 | if (n == 1) return 1; | ||
| 6 | if (k <= h) return 2*k-m; | ||
| 7 | return 2*f(n-h, k-h, m^(n%2))+m-1; | ||
| 8 | } | ||
| 9 | |||
| 10 | int main() { | ||
| 11 | int q, n, k; | ||
| 12 | std::cin >> q; | ||
| 13 | for (int i = 0; i < q; i++) { | ||
| 14 | std::cin >> n >> k; | ||
| 15 | std::cout << f(n, k, 0) << "\n"; | ||
| 16 | } | ||
| 17 | } | ||
