結果

問題 No.2302 Carry X Times
ユーザー Jeroen Op de BeekJeroen Op de Beek
提出日時 2023-05-12 22:01:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 3,583 bytes
コンパイル時間 3,280 ms
コンパイル使用メモリ 203,260 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-19 04:46:31
合計ジャッジ時間 3,720 ms
ジャッジサーバーID
(参考情報)
judge15 / judge9
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
4,380 KB
testcase_01 AC 5 ms
4,384 KB
testcase_02 AC 9 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 13 ms
4,380 KB
testcase_11 AC 13 ms
4,380 KB
testcase_12 AC 14 ms
4,380 KB
testcase_13 AC 12 ms
4,380 KB
testcase_14 AC 13 ms
4,380 KB
testcase_15 AC 9 ms
4,384 KB
testcase_16 AC 8 ms
4,380 KB
testcase_17 AC 14 ms
4,384 KB
testcase_18 AC 11 ms
4,380 KB
testcase_19 AC 11 ms
4,384 KB
testcase_20 AC 11 ms
4,384 KB
testcase_21 AC 11 ms
4,384 KB
testcase_22 AC 10 ms
4,384 KB
testcase_23 AC 11 ms
4,380 KB
testcase_24 AC 10 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1e5+1, oo = 1e9;
const long long MD =998244353 ;
template<long long MOD=MD> struct mdint {
    int d=0;
    mdint () {d=0;}
    mdint (long long _d) : d(_d%MOD){
        if(d<0) d+=MOD;
    };
    friend mdint& operator+=(mdint& a, const mdint& o) {
        a.d+=o.d; if(a.d>=MOD) a.d-=MOD;
        return a;
    }
    friend mdint& operator-=(mdint& a, const mdint& o) {
        a.d-=o.d; if(a.d<0) a.d+=MOD;
        return a;
    }
    friend mdint& operator*=(mdint& a, const mdint& o) {
        return a = mdint((ll)a.d*o.d);
    }
    mdint operator*(const mdint& o) const {
        mdint res = *this;
        res*=o;
        return res;
    }
    mdint operator+(const mdint& o) const {
        mdint res = *this;
        res+=o;
        return res;
    }
    mdint operator-(const mdint& o) const {
        mdint res = *this;
        res-=o;
        return res;
    }
    mdint operator^(long long b) const {
        mdint tmp = 1;
        mdint power = *this;
        while(b) {
            if(b&1) {
                tmp = tmp*power;
            }
            power = power*power;
            b/=2;
        }
        return tmp;
    }
    friend mdint operator/=(mdint& a, const mdint& o) {
        a *= (o^(MOD-2));
        return a;
    }
    mdint operator/(const mdint& o) {
        mdint res = *this;
        res/=o;
        return res;
    }
    bool operator==(const mdint& o) { return d==o.d;}
    bool operator!=(const mdint& o) { return d!=o.d;}
    friend istream& operator>>(istream& c, mdint& a) {return c >> a.d;}
    friend ostream& operator<<(ostream& c, const mdint& a) {return c << a.d;}
};
using  mint = mdint<MD>;
vi digs;
int x;
mint dp[2][2][20][20][2];
bool vis[2][2][20][20][2];
mint solve(bool ba, bool bb, int i, int carry, bool curc) {
    if(carry<0) return 0;
    if(i==-1) {
        if(curc) return 0;
        if(carry==0) return 1;
        return 0;
    }
    mint& ans = dp[ba][bb][i][carry][curc];
    if(vis[ba][bb][i][carry][curc]) return  ans;
    vis[ba][bb][i][carry][curc]=1;
    ans=0;
    int la = ba?digs[i]:9, lb = bb?digs[i]:9;
    
    for(int da=0;da<=la;++da) {
        for(int db=0;db<=lb;++db) {
            // assume carried:
            auto res = solve(ba and la==da,bb and lb==db,i-1,carry-curc,1);
            if(curc== (da+db+1>=10)) ans+=res;
            res = solve(ba and la==da,bb and lb==db,i-1,carry-curc,0);
            if(curc == (da+db>=10)) ans+=res;


        }
    }
    return ans;
}
void testcase() {
    string n; cin >> n;
    reverse(all(n));
    digs.resize(n.size());
    for(int i=0;i<n.size();++i) {
        digs[i]=n[i]-'0';
    }
    for(int i=0;i<2;++i) for(int j=0;j<2;++j) for(int k=0;k<20;++k) for(int o=0;o<20;++o) for(int l=0;l<2;++l) {
        vis[i][j][k][o][l]=0;
    }
    cin >> x;
    cout << solve(1,1,n.size()-1,x,1)+solve(1,1,n.size()-1,x,0) << '\n';

}
int main() {
    int t; cin >> t;
    while(t--) testcase();
}
0