結果

問題 No.1648 Sum of Powers
ユーザー tomo0608tomo0608
提出日時 2023-10-07 16:52:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 14,706 bytes
コンパイル時間 3,235 ms
コンパイル使用メモリ 239,940 KB
実行使用メモリ 9,728 KB
最終ジャッジ日時 2024-07-26 17:51:13
合計ジャッジ時間 8,207 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
9,600 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 46 ms
9,728 KB
testcase_03 AC 46 ms
9,728 KB
testcase_04 AC 48 ms
9,600 KB
testcase_05 AC 48 ms
9,600 KB
testcase_06 AC 48 ms
9,600 KB
testcase_07 AC 47 ms
9,728 KB
testcase_08 AC 48 ms
9,728 KB
testcase_09 AC 47 ms
9,728 KB
testcase_10 AC 47 ms
9,728 KB
testcase_11 AC 48 ms
9,600 KB
testcase_12 AC 59 ms
9,600 KB
testcase_13 AC 54 ms
9,600 KB
testcase_14 AC 49 ms
9,600 KB
testcase_15 AC 46 ms
9,600 KB
testcase_16 AC 51 ms
9,728 KB
testcase_17 AC 57 ms
9,728 KB
testcase_18 AC 53 ms
9,728 KB
testcase_19 AC 50 ms
9,600 KB
testcase_20 AC 49 ms
9,728 KB
testcase_21 AC 47 ms
9,728 KB
testcase_22 AC 54 ms
9,600 KB
testcase_23 AC 47 ms
9,728 KB
testcase_24 AC 50 ms
9,600 KB
testcase_25 AC 48 ms
9,600 KB
testcase_26 AC 47 ms
9,600 KB
testcase_27 AC 49 ms
9,728 KB
testcase_28 AC 55 ms
9,728 KB
testcase_29 AC 52 ms
9,728 KB
testcase_30 AC 47 ms
9,728 KB
testcase_31 AC 54 ms
9,600 KB
testcase_32 AC 49 ms
9,728 KB
testcase_33 AC 49 ms
9,728 KB
testcase_34 AC 47 ms
9,728 KB
testcase_35 AC 51 ms
9,600 KB
testcase_36 AC 47 ms
9,728 KB
testcase_37 AC 52 ms
9,728 KB
testcase_38 AC 49 ms
9,728 KB
testcase_39 AC 54 ms
9,728 KB
testcase_40 AC 49 ms
9,728 KB
testcase_41 AC 57 ms
9,728 KB
testcase_42 AC 45 ms
9,600 KB
testcase_43 AC 47 ms
9,600 KB
testcase_44 AC 50 ms
9,600 KB
testcase_45 AC 49 ms
9,728 KB
testcase_46 AC 47 ms
9,600 KB
testcase_47 AC 43 ms
9,600 KB
testcase_48 AC 2 ms
6,944 KB
testcase_49 AC 43 ms
9,600 KB
testcase_50 AC 48 ms
9,728 KB
testcase_51 AC 2 ms
6,944 KB
testcase_52 AC 2 ms
6,944 KB
testcase_53 AC 2 ms
6,944 KB
testcase_54 AC 2 ms
6,944 KB
testcase_55 AC 49 ms
9,728 KB
testcase_56 AC 50 ms
9,728 KB
testcase_57 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma region competitive_programming
#ifdef __LOCAL
#define _GLIBCXX_DEBUG
#endif
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>


//#include "modint_plus.hpp"
//typedef atcoder::modint1000000007 mint;
//typedef atcoder::modint998244353 mint;
//#include<atcoder/convolution>
//#include "Formal_Power_Series.hpp"
//typedef tomo0608::Formal_Power_Series<mint> fps;
#include<atcoder/math>

namespace tomo0608 {
#define is_prime(n) atcoder::internal::is_prime_constexpr(n)

    std::map<long long, int> prime_factorization(long long n) {
        std::map<long long, int> res;
        n = abs(n);
        assert(n > 1);
        for (long long i = 2; i * i <= n; i++) {
            if (n % i == 0) {
                int num = 0;
                while (n % i == 0) {
                    num++;
                    n /= i;
                }
                res[i] = num;
            }
        }
        if (n != 1)res[n] = 1;
        return res;
    }

    class Osa_k {
    public:
        const int MAX_N;
        std::vector<int> min_prime;
        Osa_k(int size) : MAX_N(size), min_prime(size + 1) {
            if (MAX_N < 2)return;
            std::vector<int> primes{ 2, 3 };
            for (int i = 2; i <= MAX_N; i += 2)min_prime[i] = 2;
            for (int i = 3; i <= MAX_N; i += 6)min_prime[i] = 3;
            for (int i = 5, d = 4; i <= MAX_N; i += d = 6 - d) {
                if (!min_prime[i]) {
                    min_prime[i] = i;
                    primes.emplace_back(i);
                }
                for (int j = 2; j < (int)primes.size() && i * primes[j] <= MAX_N; j++) {
                    min_prime[i * primes[j]] = primes[j];
                    if (primes[j] == min_prime[i])break;
                }
            }
        }
        std::vector<std::pair<int, int>> factorization(int n) {
            assert(0 < n && n <= MAX_N);
            std::vector<std::pair<int, int>> res;
            while (n > 1) {
                int p = min_prime[n];
                int num = 0;
                while (min_prime[n] == p) {
                    num++;
                    n /= p;
                }
                res.emplace_back(p, num);
            }
            return res;
        }
    };


    std::vector<int> prime_enumerate(int N) {
        std::vector<bool> sieve(N / 3 + 1, 1);
        for (int p = 5, d = 4, i = 1, sqn = std::sqrt(N); p <= sqn; p += d = 6 - d, i++) {
            if (!sieve[i]) continue;
            for (int q = p * p / 3, r = d * p / 3 + (d * p % 3 == 2), s = 2 * p, qe = sieve.size();q < qe; q += r = s - r)sieve[q] = 0;
        }
        std::vector<int> ret{ 2, 3 };
        for (int p = 5, d = 4, i = 1; p <= N; p += d = 6 - d, i++)
            if (sieve[i]) ret.push_back(p);
        while (!ret.empty() && ret.back() > N) ret.pop_back();
        return ret;
    }

    template<class T>
    void div_zeta(std::vector<T>& a) {
        std::vector<int> sieve = prime_enumerate((int)a.size() - 1);
        for (auto& p : sieve)for (int i = 1; i * p < a.size(); i++)a[i * p] += a[i];
    }
    template<class T>
    void div_mobius(std::vector<T>& a) {
        std::vector<int> sieve = prime_enumerate((int)a.size() - 1);
        for (auto& p : sieve)for (int i = ((int)a.size() - 1) / p; i > 0; i--)a[i * p] -= a[i];
    }
    template<class T>
    void mul_zeta(std::vector<T>& a) {
        std::vector<int> sieve = prime_enumerate((int)a.size() - 1);
        for (auto& p : sieve)for (int i = ((int)a.size() - 1) / p; i > 0; i--)a[i] += a[i * p];
    }
    template<class T>
    void mul_mobius(std::vector<T>& a) {
        std::vector<int> sieve = prime_enumerate((int)a.size() - 1);
        for (auto& p : sieve)for (int i = 1; i * p < a.size(); i++)a[i] -= a[i * p];
    }
    
    template<class T>
    std::vector<T> gcd_convolution(std::vector<T> a, std::vector<T> b){
        mul_zeta(a);
        mul_zeta(b);
        for(int i = 0; i < a.size(); i++)a[i] *= b[i];
        mul_mobius(a);
        return a;
    }
    template<class T>
    std::vector<T> lcm_convolution(std::vector<T> a, std::vector<T> b){
        div_zeta(a);
        div_zeta(b);
        for(int i = 0; i < a.size(); i++)a[i] *= b[i];
        div_mobius(a);
        return a;
    }

    long long euler_phi(long long n) {
        long long res = n;
        for (long long i = 2; i * i <= n; i++) {
            if (n % i == 0) {
                res -= res / i;
                while (n % i == 0)n /= i;
            }
        }
        if (n > 1)res -= res / n;
        return res;
    }

#if __cplusplus >= 201703L
    template<class Set,
        class Monoid,
        auto action,
        auto composition,
        auto id>
    long long discrete_logarithm(Monoid f, Set s, Set t, long long MAX_N) {// return the smallest integer n lower than MAX_N such that f^n s = t if exists, otherwise -1
        static_assert(std::is_convertible_v<decltype(action),
        std::function<Set(Monoid, Set)>>, "action must work as Set(Monoid, Set)");
        static_assert(std::is_convertible_v<decltype(composition),
        std::function<Monoid(Monoid, Monoid)>>, "composition must work as Monoid(Monoid, Monoid)");
        static_assert(std::is_convertible_v<decltype(id),
        std::function<Monoid()>>, "id must work as Monoid()");

#else
    template<class Set,
        class Monoid,
        Set(*action)(Monoid, Set),
        Monoid(*composition)(Monoid, Monoid),
        Monoid(*id)()>
    long long discrete_logarithm(Monoid f, Set s, Set t, long long MAX_N) {

#endif
        if (s == t)return 0;
        long long SQRT_N = sqrt(MAX_N) + 1;
        std::set<Set> T;
        Set f_t = t;
        for (int i = 1; i <= SQRT_N; i++) {
            f_t = action(f, f_t);
            T.emplace(f_t);
        }
        Monoid f_m = id();

        {
            long long n = SQRT_N;
            Monoid _f = f;
            while(n > 0){
                if(n&1)f_m = composition(f_m, _f);
                _f = composition(_f, _f);
                n >>= 1;
            }
        }

        int num_of_failures = 0;
        Set pre_s = s;
        for (int k = 1; k <= SQRT_N; k++) {
            Set now_s = action(f_m, pre_s);
            if (T.count(now_s)) {
                Set lhs = pre_s;
                for (int i = 0; i < SQRT_N; i++, lhs = action(f, lhs)) {
                    if (lhs == t)return (k - 1) * SQRT_N + i;
                }
                num_of_failures++;
                if (num_of_failures == 2)return -1;
            }
            std::swap(pre_s, now_s);
        }
        return -1;
    };
}


namespace tomo0608 {
    typedef long long ll;
    typedef long double ld;
    template <class T> using V = std::vector<T>;
    template <class T> using VV = V<V<T>>;
    template <class T> using VVV = V<VV<T>>;
    typedef std::pair<int, int> pii;
    typedef std::pair<long long, long long> pll;
    template<class... T>void input(T&... a) { (std::cin >> ... >> a); };

#define INT(...) int __VA_ARGS__; IN(__VA_ARGS__)
#define LL(...) long long __VA_ARGS__; IN(__VA_ARGS__)
#define CHAR(...) char __VA_ARGS__; IN(__VA_ARGS__)
#define STR(...) string __VA_ARGS__; IN(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__; IN(__VA_ARGS__)
#define VEC(type, name, size) std::vector<type> name(size);IN(name)
#define VECVEC(type, name, h, w) std::vector<std::vector<type>> name(h, std::vector<type>(w));IN(name)
    template<class T1, class T2> std::istream& operator>>(std::istream& is, std::pair<T1, T2>& p) { is >> p.first >> p.second; return is; }
    template<class T1, class T2> std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) { os << '(' << p.first << ", " << p.second << ')'; return os; }
    template<class T> std::istream& operator>>(std::istream& is, std::vector<T>& v) { for (auto& e : v) is >> e; return is; }
    template<class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) { for (auto& e : v) os << e << ' '; return os; }
    template<typename T> std::ostream& operator << (std::ostream& os, std::set<T>& set_var) { os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr;++itr;if (itr != set_var.end()) os << ", ";itr--; }os << "}";return os; }
    template <typename T, typename U> std::ostream& operator<<(std::ostream& os, std::map<T, U>& map_var) { os << "{";for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << *itr;itr++;if (itr != map_var.end()) os << ", ";itr--; }os << "}";return os; }
    template<int i, class T> void print_tuple(std::ostream&, const T&) {}
    template<int i, class T, class H, class ...Args> void print_tuple(std::ostream& os, const T& t) {
        if (i){os << ", ";} os << std::get<i>(t); print_tuple<i + 1, T, Args...>(os, t);
    }
    template<class ...Args> std::ostream& operator<<(std::ostream& os, const std::tuple<Args...>& t) {
        os << "{"; print_tuple<0, std::tuple<Args...>, Args...>(os, t); return os << "}";
    }

    void IN() {}
    template <class Head, class... Tail> void IN(Head& head, Tail &...tail) {
        std::cin >> head;
        IN(tail...);
    }
    void print() { std::cout << '\n'; }
    template<class T, class... Ts>void print(const T& a, const Ts&... b) { std::cout << a; (std::cout << ... << (std::cout << ' ', b)); std::cout << '\n'; }
    void drop() { std::cout << '\n';exit(0); }
    template<class T, class... Ts>void drop(const T& a, const Ts&... b) { std::cout << a; (std::cout << ... << (std::cout << ' ', b)); std::cout << '\n';exit(0); }
#ifdef __LOCAL
    void debug_out() { std::cerr << std::endl; }
    template < class Head, class... Tail> void debug_out(Head H, Tail... T) { std::cerr << ' ' << H; debug_out(T...); }
#define debug(...) std::cerr << 'L' << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) std::cerr << 'L' << __LINE__ << " " << #x << " = " << (x) << std::endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif


#define rep1(a)          for(long long i = 0; i < (a); i++)
#define rep2(i, a)       for(long long i = 0; i < (a); i++)
#define rep3(i, a, b)    for(long long i = (a); i < (b); i++)
#define rep4(i, a, b, c) for(long long i = (a); i < (b); i += (c))
#define drep1(a)          for(long long i = (a)-1; i >= 0; i--)
#define drep2(i, a)       for(long long i = (a)-1; i >= 0; i--)
#define drep3(i, a, b)    for(long long i = (a)-1; i >= (b); i--)
#define drep4(i, a, b, c) for(long long i = (a)-1; i >= (b); i -= (c))
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define drep(...) overload4(__VA_ARGS__, drep4, drep3, drep2, drep1)(__VA_ARGS__)
#define endl '\n'

    struct io_setup {
        io_setup() {
            std::cin.tie(0);
            std::ios_base::sync_with_stdio(false);
            std::cout << std::setprecision(20);
        }
    }io_setup;
} // namespace tomo0608

namespace tomo0608 {
#define ALL(x) x.begin(),x.end()
    template <class T = long long, class S> T SUM(const S& v) { return accumulate(ALL(v), T(0)); }
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define RSORT(v) sort(ALL(v)); reverse(ALL(v))
#define UNIQUE(x) SORT(x), x.erase(unique(ALL(x)), x.end())
#define lb(c, x) distance((c).begin(), lower_bound(ALL(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(ALL(c), (x)))
    template <typename T> void zip(std::vector<T>& x) { std::vector<T> y = x;UNIQUE(y);for (int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); } }
    template<class T> using priority_queue_rev = std::priority_queue<T, std::vector<T>, std::greater<T>>;
    template<class T, class U> inline bool chmax(T& a, const U& b) { if (a < b) { a = b; return 1; } return 0; }
    template<class T, class U> inline bool chmin(T& a, const U& b) { if (a > b) { a = b; return 1; } return 0; }
    template<class T> inline int count_between(std::vector<T>& a, T l, T r) { return lower_bound(ALL(a), r) - lower_bound(ALL(a), l); } // [l, r)
    template<typename T> int bittest(T n, int k) { return (n >> k) & 1; }
    int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
    int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
    int lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
    int lowbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }
#define perm(v) for(bool permrepflag = true; (permrepflag ? exchange(permrepflag, false) : next_permutation(ALL(v)));)
    template <typename T, typename S> T ceil(T x, S y) {
        assert(y);
        return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y));
    }

    template <typename T, typename S> T floor(T x, S y) {
        assert(y);
        return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1)));
    }

    template <typename T> std::vector<int> id_sort(const std::vector<T>& v, bool greater = false) {
        int n = v.size();
        std::vector<int> ret(n);
        std::iota(ALL(ret), 0);
        std::sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; });
        return ret;
    }
}
//using namespace atcoder;
using namespace std;
using namespace tomo0608;
int dx[8] = { 1, 0, -1, 0, 1, 1, -1, -1 };
int dy[8] = { 0, 1, 0, -1, 1, -1, -1, 1 };



// インタラクティブ問題のときは出力するたびにcout.flush();を忘れない!!!!!

void solve();
int main() {
    int codeforces = 1;
    //cin >> codeforces;
    while (codeforces--) {
        solve();
    }
    return 0;
}
#pragma endregion

typedef array<ll,2> Set;
typedef array<ll, 4> Monoid;
const ll MOD = 998244353;
Set action(Monoid M, Set s){
    Set res;
    res[0] = M[0] * s[0] + M[1] * s[1];
    res[1] = M[2] * s[0] + M[3] * s[1];
    res[0] = (res[0] + MOD)%MOD;
    res[1] = (res[1] + MOD)%MOD;
    return res;
};

Monoid composition(Monoid M_1, Monoid M_2){
    Monoid res;
    res[0] = M_1[0] * M_2[0] + M_1[1] * M_2[2];
    res[1] = M_1[0] * M_2[1] + M_1[1] * M_2[3];
    res[2] = M_1[2] * M_2[0] + M_1[3] * M_2[2];
    res[3] = M_1[2] * M_2[1] + M_1[3] * M_2[3];
    res[0] = (res[0] + MOD)%MOD;
    res[1] = (res[1] + MOD)%MOD;
    res[2] = (res[2] + MOD)%MOD;
    res[3] = (res[3] + MOD)%MOD;
    return res;
};

Monoid id(){
    Monoid e;
    e[0] = 1;
    e[1] = 0;
    e[2] = 0;
    e[3] = 1;
    return e;
};

void solve() {
    LL(a, b, p, q);
    Monoid f;
    f[0] = a;
    f[1] = MOD-b;
    f[2] = 1;
    f[3] = 0;
    Set s, t;
    s[0] = (a*a-2*b+MOD)%MOD;
    s[1] = a;
    t[0] = p;
    t[1] = q;
    ll MAX_N = 10000000001;
    print(discrete_logarithm<Set, Monoid, action, composition, id>(f, s, t, MAX_N)+2);
}
0