Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int MAX = 1e5+5;
- const int64_t MOD = 1e9+7;
- int64_t phi(int64_t n) {
- int64_t result = n;
- for (int64_t i = 2; i * i <= n; i++) {
- if (n % i == 0) {
- while (n % i == 0)
- n /= i;
- result -= result / i;
- }
- }
- if (n > 1)
- result -= result / n;
- return result;
- }
- const int64_t MOD2 = phi(MOD);
- int64_t extendedGcd(int64_t a, int64_t b, int64_t &x, int64_t &y) {
- if (b == 0) {
- x = 1; y = 0;
- return a;
- }
- int64_t g = extendedGcd(b, a%b, y, x);
- y -= x*(a/b);
- return g;
- }
- int64_t modularInverse(int64_t a, int64_t _MOD = MOD) {
- int64_t x, y;
- extendedGcd(a, _MOD, x, y);
- return (x%_MOD + _MOD)%_MOD;
- }
- int64_t modMul(int64_t a, int64_t b, int64_t _MOD = MOD) {
- a %= _MOD; b %= _MOD;
- return (__int128)a*b%_MOD;
- }
- int64_t binpow(int64_t base, int64_t exp) {
- base %= MOD;
- int64_t res = 1;
- while (exp > 0) {
- if (exp & 1) res = modMul(res, base);
- base = modMul(base, base);
- exp >>= 1;
- }
- return res;
- }
- int n;
- int64_t x[MAX], k[MAX];
- int main() {
- cin.tie(0)->sync_with_stdio(0);
- // Input
- cin >> n;
- for (int i = 0; i < n; ++i) {
- cin >> x[i] >> k[i];
- }
- // Number of divisors
- int64_t numOfDivisors = 1;
- for (int i = 0; i < n; ++i) {
- numOfDivisors = modMul(numOfDivisors, (k[i] + 1));
- }
- // Sum of divisors
- int64_t sumOfDivisors = 1;
- for (int i = 0; i < n; ++i) {
- sumOfDivisors = modMul(sumOfDivisors, modMul(binpow(x[i], k[i]+1)-1, modularInverse(x[i]-1)));
- }
- bool square = true;
- // Calculate N
- int64_t N = 1;
- for (int i = 0; i < n; ++i) {
- square &= (k[i] % 2 == 0);
- }
- if (square) {
- for (int i = 0; i < n; ++i) {
- N = modMul(N, binpow(x[i], k[i]/2));
- }
- } else {
- for (int i = 0; i < n; ++i) {
- N = modMul(N, binpow(x[i], k[i]));
- }
- }
- bool ok = false;
- // Number of divisors by phi(mod)
- int64_t numOfDivisorsPhiMod = 1;
- for (int i = 0; i < n; ++i) {
- if (!ok and (k[i] & 1)) {
- numOfDivisorsPhiMod = modMul(numOfDivisorsPhiMod, (k[i] + 1)/2, MOD2);
- ok = true;
- } else {
- numOfDivisorsPhiMod = modMul(numOfDivisorsPhiMod, (k[i] + 1), MOD2);
- }
- }
- // Product of divisors
- int64_t productOfDivisors = 1;
- productOfDivisors = binpow(N, numOfDivisorsPhiMod);
- cout << numOfDivisors << ' ';
- cout << sumOfDivisors << ' ';
- cout << productOfDivisors << '\n';
- }
Add Comment
Please, Sign In to add comment