結果

問題 No.2846 Birthday Cake
ユーザー GOTKAKOGOTKAKO
提出日時 2024-08-23 22:46:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,934 bytes
コンパイル時間 2,187 ms
コンパイル使用メモリ 215,248 KB
実行使用メモリ 55,752 KB
最終ジャッジ日時 2024-08-23 22:47:03
合計ジャッジ時間 5,965 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 TLE -
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 TLE -
testcase_06 TLE -
testcase_07 AC 1,756 ms
44,124 KB
testcase_08 AC 1,258 ms
35,328 KB
testcase_09 AC 739 ms
26,560 KB
testcase_10 AC 281 ms
17,024 KB
testcase_11 AC 102 ms
9,216 KB
testcase_12 AC 1,568 ms
48,856 KB
testcase_13 AC 674 ms
34,140 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,948 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 15 ms
6,944 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 122 ms
9,344 KB
testcase_23 AC 83 ms
8,108 KB
testcase_24 AC 193 ms
16,128 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 27 ms
6,944 KB
testcase_27 AC 9 ms
6,944 KB
testcase_28 AC 3 ms
6,944 KB
testcase_29 AC 0 ms
6,940 KB
testcase_30 AC 3 ms
6,944 KB
testcase_31 AC 24 ms
6,944 KB
testcase_32 AC 114 ms
9,728 KB
testcase_33 AC 164 ms
16,128 KB
testcase_34 AC 127 ms
12,032 KB
testcase_35 AC 21 ms
6,944 KB
testcase_36 AC 360 ms
20,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct frac{ //最終的に分子分母64bitに収まる計算のみ.
    public:
    long long n,d;
    frac() : n(0),d(1){}
    frac(long long v) : n(v),d(1) {}
    frac(long long a,long long b,bool redu = true){
        assert(b != 0);
        if(redu) reduce(a,b);
        n = a,d = b; 
    }
    private:
    long long gcd(long long a,long long b){
        if(a%b == 0) return b;
        return gcd(b,a%b);
    } 
    long long gcd128(long long a,long long b){ //絶対値gcd128.
        if(b == 0) return abs(a);
        return gcd(abs(a),abs(b));
    }
    void reduce(long long &a,long long &b){ //約分.
        if(b < 0) a = -a,b = -b;
        long long div = gcd128(a,b);
        a /= div; b /= div;
    }
    public:
    //計算量 O(logmax(d,b.d)).
    friend frac operator+(const frac &b){return b;}
    friend frac operator-(const frac &b){return frac(-b.n,b.d,false);}
    friend frac operator+(const frac &a,const frac &b){
        return frac((long long)a.n*b.d+(long long)b.n*a.d,(long long)a.d*b.d);
    } 
    friend frac operator-(const frac &a,const frac &b){
        return frac((long long)a.n*b.d-(long long)b.n*a.d,(long long)a.d*b.d);
    }
    friend frac operator*(const frac &a,const frac &b){
        long long g1 = std::gcd(a.n,b.d),g2 = std::gcd(a.d,b.n);
        return frac((a.n/g1)*(b.n/g2),(a.d/g2)*(b.d/g1),false);
    }
    friend frac operator/(const frac &a,const frac &b){
        assert(b.n != 0);
        long long g1 = std::gcd(a.n,b.n),g2 = std::gcd(a.d,b.d);
        if(b.n < 0) return frac((-a.n/g1)*(b.d/g2),(a.d/g2)*(-b.n/g1));
        else return frac((a.n/g1)*(b.d/g2),(a.d/g2)*(b.n/g1));
    }
    friend bool operator==(const frac &a,const frac &b){return a.n==b.n && a.d==b.d;}
    friend bool operator!=(const frac &a,const frac &b){return a.n!=b.n || a.d!=b.d;}
    friend bool operator>(const frac &a,const frac &b){return (long long)a.n*b.d > (long long)b.n*a.d;}
    friend bool operator>=(const frac &a,const frac &b){return (long long)a.n*b.d >= (long long)b.n*a.d;}
    friend bool operator<(const frac &a,const frac &b){return (long long)a.n*b.d < (long long)b.n*a.d;}
    friend bool operator<=(const frac &a,const frac &b){return (long long)a.n*b.d <= (long long)b.n*a.d;}
 
    frac &operator=(const frac &b) = default;
    frac operator+=(const frac &b){return *this=*this+b;}
    frac operator-=(const frac &b){return *this=*this-b;}
    frac operator*=(const frac &b){return *this=*this*b;}
    frac operator/=(const frac &b){return *this=*this/b;}
    frac operator++(int){*this += frac(1); return *this;}
    frac operator--(int){*this -= frac(1); return *this;}
 
    double decimal(){return (n+0.0)/d;}
    long double decimall(){return ((long double)n)/d;}
    long long num(){return n;} long long den(){return d;}
    long long floor(){return n<0?(n+1)/d-1:n/d;}
    long long ceil(){return n>0?(n-1)/d+1:n/d;}
    frac inv(){return frac(n>=0?d:-d,n>=0?n:-n,false);}
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int K,N; cin >> K >> N;
    vector<__int128_t> fac(K+1);
    fac.at(0) = 1;
    for(int i=1; i<=K; i++) fac.at(i) = fac.at(i-1)*i;
    vector<frac> mindec(K+1);
    for(int i=1; i<=K; i++) mindec.at(i) = frac(i,N);
    long long inf = 1000000000;
    vector<unordered_map<long long,__int128_t>> dp(K+1);
    dp.at(0)[inf+1] += fac.at(K);
    
    for(int i=1; i<=N; i++){
        frac dec(1,i);
        for(int k=K-1; k>=0; k--){
            for(auto &[ke,v] : dp.at(k)){
                frac now(ke/inf,ke%inf);
                for(int l=1; l<=K-k; l++){
                    now -= dec;
                    if(now < 0 || now.d > 20000) break;
                    if(now-mindec.at(K-(k+l)) < 0) break;
                    dp.at(k+l)[now.n*inf+now.d] += v/fac.at(l);
                }
            }
        }
    }

    cout << (long long)dp.at(K)[1] << endl;
}
0