結果

問題 No.2649 [Cherry 6th Tune C] Anthem Flower
ユーザー dyktr_06dyktr_06
提出日時 2024-02-23 21:24:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 17,105 bytes
コンパイル時間 2,938 ms
コンパイル使用メモリ 232,420 KB
実行使用メモリ 12,336 KB
最終ジャッジ日時 2024-09-29 05:24:47
合計ジャッジ時間 23,800 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
12,068 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 1,697 ms
7,600 KB
testcase_15 WA -
testcase_16 AC 1,919 ms
8,504 KB
testcase_17 TLE -
testcase_18 WA -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define overload4(_1, _2, _3, _4, name, ...) name
#define rep1(n) for(int i = 0; i < (int)(n); ++i)
#define rep2(i, n) for(int i = 0; i < (int)(n); ++i)
#define rep3(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep4(i, a, b, c) for(int i = (a); i < (int)(b); i += (c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)

#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
#define Sort(a) (sort((a).begin(), (a).end()))
#define RSort(a) (sort((a).rbegin(), (a).rend()))
#define UNIQUE(a) (a.erase(unique((a).begin(), (a).end()), (a).end()))

typedef long long int ll;
typedef unsigned long long ul;
typedef long double ld;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<char> vc;
typedef vector<string> vst;
typedef vector<double> vd;
typedef vector<long double> vld;
typedef pair<long long, long long> P;

template<class T> long long sum(const T& a){ return accumulate(a.begin(), a.end(), 0LL); }
template<class T> auto min(const T& a){ return *min_element(a.begin(), a.end()); }
template<class T> auto max(const T& a){ return *max_element(a.begin(), a.end()); }

const long long MINF = 0x7fffffffffff;
const long long INF = 0x1fffffffffffffff;
const long long MOD = 998244353;
const long double EPS = 1e-9;
const long double PI = acos(-1);
 
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p){ is >> p.first >> p.second; return is; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p){ os << "(" << p.first << ", " << p.second << ")"; return os; }
template<typename T> istream &operator>>(istream &is, vector<T> &v){ for(T &in : v) is >> in; return is; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v){ for(int i = 0; i < (int) v.size(); ++i){ os << v[i] << (i + 1 != (int) v.size() ? " " : ""); } return os; }
template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &mp){ for(auto &[key, val] : mp){ os << key << ":" << val << " "; } return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &st){ auto itr = st.begin(); for(int i = 0; i < (int)st.size(); ++i){ os << *itr << (i + 1 != (int)st.size() ? " " : ""); itr++; } return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st){ auto itr = st.begin(); for(int i = 0; i < (int)st.size(); ++i){ os << *itr << (i + 1 != (int)st.size() ? " " : ""); itr++; } return os; }
template <typename T> ostream &operator<<(ostream &os, queue<T> q){ while(q.size()){ os << q.front() << " "; q.pop(); } return os; }
template <typename T> ostream &operator<<(ostream &os, deque<T> q){ while(q.size()){ os << q.front() << " "; q.pop_front(); } return os; }
template <typename T> ostream &operator<<(ostream &os, stack<T> st){ while(st.size()){ os << st.top() << " "; st.pop(); } return os; }
template <class T, class Container, class Compare> ostream &operator<<(ostream &os, priority_queue<T, Container, Compare> pq){ while(pq.size()){ os << pq.top() << " "; pq.pop(); } return os; }

template<class T, class U> inline T vin(T& vec, U n) { vec.resize(n); for(int i = 0; i < (int) n; ++i) cin >> vec[i]; return vec; }
template<class T> inline void vout(T vec, string s = "\n"){ for(auto x : vec) cout << x << s; }
template<class... T> void in(T&... a){ (cin >> ... >> a); }
void out(){ cout << '\n'; }
template<class T, class... Ts> void out(const T& a, const Ts&... b){ cout << a; (cout << ... << (cout << ' ', b)); cout << '\n'; }
template<class T, class U> void inGraph(vector<vector<T>>& G, U n, U m, bool directed = false){ G.resize(n); for(int i = 0; i < m; ++i){ int a, b; cin >> a >> b; a--, b--; G[a].push_back(b); if(!directed) G[b].push_back(a); } }

namespace modcalc{

    template <typename T>
    T modpow(T x, T n, const T &m){
        T ret = 1 % m;
        x %= m;
        while(n > 0){
            if(n & 1) (ret *= x) %= m;
            (x *= x) %= m;
            n >>= 1;
        }
        return ret;
    }

    template <typename T>
    T modinv(T a, T m){
        T b = m, u = 1, v = 0;
        while(b){
            T t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        u %= m;
        if (u < 0) u += m;
        return u;
    }

    template <typename T>
    T modarithmeticsum(T a, T d, T n, T m){
        T m2 = m * 2;
        a %= m2, n %= m2, d %= m2;
        T b = (n - 1) * d % m2;
        return ((n * (a * 2 + b) % m2) / 2) % m;
    }

    template <typename T>
    T modgeometricsum(T a, T r, T n, T m){
        a %= m;
        if(r == 1){
            n %= m;
            return a * n % m;
        }
        return a * (modpow(r, n, m) + m - 1) % m * modinv(r - 1, m) % m;
    }

    template <typename T>
    T modgeometricsum2(T a, T r, T n, T m){
        a %= m;
        if(r == 1){
            n %= m;
            return a * n % m;
        }
        T ret = 0;
        T x = 1 % m;
        T sum = 0;
        for(int i = 0; n > 0; ++i){
            if(n & 1){
                (ret += x * modpow(r, sum, m) % m) %= m;
                sum |= 1LL << i;
            }
            (x += x * modpow(r, 1LL << i, m) % m) %= m;
            n >>= 1;
        }
        return a * ret % m;
    }
}

template <long long base = 1000000LL, int digit = 6>
struct BigInt{
    int sign = 1;
    vector<long long> val;

    constexpr BigInt(const long long _val = 0) noexcept {
        if(_val != 0){
            val.assign(1, abs(_val));
            shift();
        }
        if(_val < 0) sign = -1;
    }
    constexpr BigInt(const vector<long long> &_val) noexcept : val(_val) {}
    constexpr BigInt(const string &s) noexcept {
        stoi(s);
    }

private:
    void normalize(){
        while(!val.empty() && val.back() == 0) val.pop_back();
        if(val.empty()) sign = 1;
    }
    vector<long long> karatsuba_algorithm(vector<long long> &a, vector<long long> &b){
        const int n = (int) a.size();
        const int h = n >> 1;
        assert(a.size() == b.size());
        assert((n & (n - 1)) == 0);
        if(n <= 64){
            vector<long long> res(2 * n - 1);
            for(int i = 0; i < n; ++i){
                for(int j = 0; j < n; ++j){
                    res[i + j] += a[i] * b[j];
                }
            }
            return res;
        }
        vector<long long> p(h), q(h), r(h), s(h), t(h), u(h);
        for(int i = 0; i < h; ++i){
            p[i] = a[i + h];
            q[i] = a[i];
            r[i] = b[i + h];
            s[i] = b[i];
            t[i] = p[i] + q[i];
            u[i] = r[i] + s[i];
        }
        p = karatsuba_algorithm(p, r);
        q = karatsuba_algorithm(q, s);
        t = karatsuba_algorithm(t, u);
        vector<long long> res(2 * n - 1, 0);
        for(int i = 0; i < n - 1; ++i){
            res[i] += q[i];
            res[i + h] += t[i] - p[i] - q[i];
            res[i + n] += p[i];
        }
        return res;
    }

    pair<BigInt, BigInt> divide_naive(const BigInt& rhs) const {
        assert(!rhs.val.empty());
        const int k = base / (rhs.val.back() + 1);
        const BigInt dividend = (sign == 1 ? *this : -(*this)) * k;
        const BigInt divisor = (rhs.sign == 1 ? rhs : -rhs) * k;
        BigInt quo, rem = 0;
        quo.val.resize(dividend.val.size());
        const int n = divisor.val.size();
        for(int i = (int) dividend.val.size() - 1; i >= 0; --i){
            rem.val.emplace(rem.val.begin(), dividend.val[i]);
            quo.val[i] = ((n < (int) rem.val.size() ? rem.val[n] * base : 0) + ((n - 1) < (int) rem.val.size() ? rem.val[n - 1] : 0)) / divisor.val.back();
            rem -= divisor * quo.val[i];
            while (rem.sign == -1) {
                rem += divisor;
                --quo.val[i];
            }
        }
        quo.sign = sign * rhs.sign;
        quo.normalize();
        rem.sign = sign;
        rem.normalize();
        return {quo, rem / k};
    }

    pair<BigInt, BigInt> divide_newton(const BigInt& rhs) const {
        assert(!rhs.val.empty());
        int preci = val.size() - rhs.val.size();
        BigInt t(1);
        BigInt two = BigInt(2) << rhs.val.size();
        BigInt pre;
        int lim = min(preci, 3);
        int rhslim = min(int(rhs.val.size()), 6);
        t <<= lim;
        while(pre != t){
            BigInt rb = rhs >> (rhs.val.size() - rhslim);
            if(rhslim != (int) rhs.val.size()) rb += BigInt(1);
            pre = t;
            t *= (BigInt(2) << (rhslim + lim)) - rb * t;
            t.val = vector<long long>(t.val.begin() + lim + rhslim, t.val.end());
        }
        if(lim != preci){
            pre = BigInt();
            while(pre != t){
                BigInt rb = rhs >> (rhs.val.size() - rhslim);
                if(rhslim != (int) rhs.val.size()) rb += BigInt(1);
                pre = t;
                t *= (BigInt(2) << (rhslim + lim)) - rb * t;
                t.val = vector<long long>(t.val.begin() + lim + rhslim, t.val.end());
                int next_lim = min(lim * 2 + 1, preci);
                if (next_lim != lim) t <<= next_lim - lim;
                int next_rhslim = min(rhslim * 2 + 1, int(rhs.val.size()));
                lim = next_lim;
                rhslim = next_rhslim;
            }
        }
        BigInt quo = (*this) * t;
        quo.val = vector<long long>(quo.val.begin() + val.size(), quo.val.end());
        BigInt mul = quo * rhs;
        while(mul + rhs <= (*this)){
            quo += BigInt(1);
            mul += rhs;
        }
        BigInt rem = *this - quo * rhs;
        return {quo, rem};
    }

public:
    void stoi(string &s){
        if(s == "0") return;
        int n = s.size(), idx = 0;
        if(s[0] == '-'){
            n -= 1;
            sign = -1;
            idx = 1;
        }
        int len = (n + digit - 1) / digit, rem = n % digit;
        if(rem == 0) rem += digit;
        val.resize(len);
        for(int i = len - 1; i >= 0; --i){
            if(i == len - 1){
                val[i] = stoll(s.substr(idx, rem));
                idx += rem;
            }else{
                val[i] = stoll(s.substr(idx, digit));
                idx += digit;
            }
        }
    }
    string itos() const {
        string res = "";
        if(sign == -1) res += "-";
        bool flag = false;
        for(int i = (int) val.size() - 1; i >= 0; --i){
            if(val[i] > 0 && !flag){
                res += to_string(val[i]);
                flag = true;
            }else if(flag){
                string rem = to_string(val[i]);
                res += string(digit - rem.size(), '0') + rem;
            }
        }
        return (res.empty() || res == "-") ? "0" : res;
    }
    pair<BigInt, BigInt> divide_mod(const BigInt& rhs){
        assert(!rhs.val.empty());
        BigInt div = *this / rhs;
        return make_pair(div, *this - div * rhs);
    }
    BigInt& shift(){
        for(int i = 0; i < (int) val.size() - 1; ++i){
            if(val[i] >= 0){
                val[i + 1] += val[i] / base;
                val[i] %= base;
            }else{
                long long x = (-val[i] + base - 1) / base;
                val[i] += x * base;
                val[i + 1] -= x;
            }
        }
        while(val.back() >= base){
            val.emplace_back(val.back() / base);
            val[val.size() - 2] %= base;
        }
        return *this;
    }
    BigInt& operator=(const BigInt& x) = default;
    inline BigInt& operator+=(const BigInt& rhs) noexcept {
        if(rhs.val.empty()) return *this;
        if(sign != rhs.sign) return *this -= -rhs;
        if(val.size() < rhs.val.size()){
            val.resize(rhs.val.size());
        }
        for(int i = 0; i < (int) rhs.val.size(); ++i){
            val[i] += rhs.val[i];
        }
        return (*this).shift();
    }
    inline BigInt& operator-=(const BigInt& rhs) noexcept {
        if(rhs.val.empty()) return *this;
        if(sign != rhs.sign) return *this += -rhs;
        if((sign == 1 ? *this : -(*this)) < (rhs.sign == 1 ? rhs : -rhs)){
            return *this = -(rhs - *this);
        }
        for(int i = 0; i < (int) rhs.val.size(); ++i){
            val[i] -= rhs.val[i];
        }
        
        shift();
        normalize();
        return *this;
    }
    // Karatsuba Algorithm (O(N^(1.58)))
    inline BigInt& operator*=(const BigInt& rhs) noexcept {
        if(val.empty() || rhs.val.empty()){
            return *this = BigInt();
        }

        sign *= rhs.sign;
        vector<long long> rhsval = rhs.val;
        int k = 1;
        while(k < (int) max(val.size(), rhsval.size())){
            k *= 2;
        }
        val.resize(k), rhsval.resize(k);
        val = karatsuba_algorithm(val, rhsval);

        shift();
        normalize();
        return *this;
    }
    // Newton method
    inline BigInt& operator/=(const BigInt& rhst) noexcept {
        assert(!rhst.val.empty());
        if(val.empty()) return *this;
        if((int) val.size() <= 32 && (int) rhst.val.size() <= 32){
            return *this = divide_naive(rhst).first;
        }

        BigInt rhs = rhst;
        int mulsign = sign * rhs.sign;
        sign = 1, rhs.sign = 1;
        if(*this < rhs){
            return *this = BigInt();
        }

        *this = divide_newton(rhs).first;
        sign = mulsign;
        normalize();
        return *this;
    }
    inline BigInt& operator%=(const BigInt& rhs) noexcept {
        assert(!rhs.val.empty());
        return *this = *this - (*this / rhs) * rhs;
    }
    inline BigInt& operator++() { return *this += 1; }
    inline BigInt operator++(int) {
        const BigInt res = *this;
        ++(*this);
        return res;
    }
    inline BigInt& operator--() { return *this -= 1; }
    inline BigInt operator--(int) {
        const BigInt res = *this;
        --(*this);
        return res;
    }
    inline BigInt operator+() const { return *this; }
    inline BigInt operator-() const {
        BigInt res = *this;
        if (!res.val.empty()) res.sign = -res.sign;
        return res;
    }
    inline BigInt& operator<<=(const unsigned int rhs){
        if(val.back() >= 1 || (int) val.size() >= 2){
            vector<long long> tmp(rhs, 0);
            val.insert(val.begin(), tmp.begin(), tmp.end());
        }
        return *this;
    }
    inline BigInt& operator>>=(const unsigned int rhs){
        if(rhs == 0) return *this;
        if(rhs > val.size()) val = {0};
        else val = vector<long long>(val.begin() + rhs, val.end()); 
        return *this;
    }
    inline bool operator<(const BigInt& rhs) const {
        if(sign != rhs.sign) return sign < rhs.sign;
        if(val.size() != rhs.val.size()) return sign * val.size() < rhs.sign * rhs.val.size();
        for(int i = (int) val.size() - 1; i >= 0; --i){
            if(val[i] != rhs.val[i]) return sign * val[i] < rhs.sign * rhs.val[i];
        }
        return false;
    }
    inline bool operator>(const BigInt& rhs) const { return rhs < (*this); }
    inline bool operator<=(const BigInt& rhs) const { return !((*this) > rhs); }
    inline bool operator>=(const BigInt& rhs) const { return !((*this) < rhs); }
    friend inline BigInt operator+(const BigInt& lhs, const BigInt& rhs) noexcept { return BigInt(lhs) += rhs; }
    friend inline BigInt operator-(const BigInt& lhs, const BigInt& rhs) noexcept { return BigInt(lhs) -= rhs; }
    friend inline BigInt operator*(const BigInt& lhs, const BigInt& rhs) noexcept { return BigInt(lhs) *= rhs; }
    friend inline BigInt operator/(const BigInt& lhs, const BigInt& rhs) noexcept { return BigInt(lhs) /= rhs; }
    friend inline BigInt operator%(const BigInt& lhs, const BigInt& rhs) noexcept { return BigInt(lhs) %= rhs; }
    friend inline BigInt operator<<(const BigInt& lhs, const unsigned int rhs) noexcept { return BigInt(lhs) <<= rhs; }
    friend inline BigInt operator>>(const BigInt& lhs, const unsigned int rhs) noexcept { return BigInt(lhs) >>= rhs; }
    friend inline bool operator==(const BigInt& lhs, const BigInt& rhs) noexcept { return lhs.val == rhs.val; }
    friend inline bool operator!=(const BigInt& lhs, const BigInt& rhs) noexcept { return lhs.val != rhs.val; }
    friend inline istream& operator>>(istream& is, BigInt& x) noexcept {
        string s;
        is >> s;
        x.stoi(s);
        return is;
    }
    friend inline ostream& operator<<(ostream& os, const BigInt& x) noexcept { return os << x.itos(); }
};

using bint = BigInt<1000000LL, 6>;

ll T;

void input(){
    in(T);
}

void solve(){
    bint n;
    ll m; in(n, m);
    n %= m;
    ll nn = stoll(n.itos());
    out(modcalc::modarithmeticsum(1LL, 1LL, nn, m));
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(20);
    
    input();
    while(T--) solve();
}
0