結果
問題 | No.2846 Birthday Cake |
ユーザー | GOTKAKO |
提出日時 | 2024-08-23 22:09:36 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,784 bytes |
コンパイル時間 | 2,043 ms |
コンパイル使用メモリ | 206,004 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-08-23 22:09:40 |
合計ジャッジ時間 | 3,542 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,940 KB |
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 | 2 ms
6,944 KB |
testcase_15 | AC | 3 ms
6,940 KB |
testcase_16 | AC | 4 ms
6,940 KB |
testcase_17 | AC | 8 ms
6,940 KB |
testcase_18 | AC | 11 ms
6,940 KB |
testcase_19 | AC | 2 ms
6,940 KB |
testcase_20 | WA | - |
testcase_21 | AC | 2 ms
6,944 KB |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | AC | 2 ms
6,940 KB |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | AC | 2 ms
6,944 KB |
testcase_29 | AC | 2 ms
6,944 KB |
testcase_30 | AC | 1 ms
6,944 KB |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
ソースコード
#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(__int128_t a,__int128_t b,bool redu = true){ assert(b != 0); if(redu) reduce(a,b); n = a,d = b; } private: __int128_t gcd(__int128_t a,__int128_t b){ if(a%b == 0) return b; return gcd(b,a%b); } __int128_t gcd128(__int128_t a,__int128_t b){ //絶対値gcd128. if(b == 0) return abs(a); return gcd(abs(a),abs(b)); } void reduce(__int128_t &a,__int128_t &b){ //約分. if(b < 0) a = -a,b = -b; __int128_t 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((__int128_t)a.n*b.d+(__int128_t)b.n*a.d,(__int128_t)a.d*b.d); } friend frac operator-(const frac &a,const frac &b){ return frac((__int128_t)a.n*b.d-(__int128_t)b.n*a.d,(__int128_t)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 (__int128_t)a.n*b.d > (__int128_t)b.n*a.d;} friend bool operator>=(const frac &a,const frac &b){return (__int128_t)a.n*b.d >= (__int128_t)b.n*a.d;} friend bool operator<(const frac &a,const frac &b){return (__int128_t)a.n*b.d < (__int128_t)b.n*a.d;} friend bool operator<=(const frac &a,const frac &b){return (__int128_t)a.n*b.d <= (__int128_t)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; long long answer = 0; auto dfs = [&](auto dfs,int pick,int now,frac left,__int128_t div) -> void { if(pick == K){ if(left == 0) answer += fac.at(K)/div; return; } for(int i=now; i<=N; i++){ frac next = left,dec = frac(1,i); for(int k=1; k<=K-pick; k++){ next -= dec; if(next.d > N || next < 0) break; dfs(dfs,pick+k,i+1,next,div*fac.at(k)); } } }; dfs(dfs,0,1,1,1); cout << answer << endl; }