QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#188577#5482. Birthday GiftGamal74#AC ✓1459ms22468kbC++205.8kb2023-09-26 02:12:372023-09-26 02:12:37

Judging History

你现在查看的是最新测评结果

  • [2023-09-26 02:12:37]
  • 评测
  • 测评结果:AC
  • 用时:1459ms
  • 内存:22468kb
  • [2023-09-26 02:12:37]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef vector<int> vi;


#define endl '\n'

void Gamal() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef Clion
    freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif
}

int dx[] = {+0, +0, -1, +1, +1, +1, -1, -1};
int dy[] = {-1, +1, +0, +0, +1, -1, +1, -1};

const double EPS = 1e-9;
const ll OO = 0X3F3F3F3F3F3F3F3F;
const int N = 300 + 5, INF = INT_MAX, MOD = 1e9 + 7, LOG = 20, b = 225;


#define rep(aa, bb, cc) for(int aa = bb; aa < cc;aa++)
#define sz(a) (int)a.size()
typedef complex<double> C;
typedef vector<double> vd;

void fft(vector<C> &a) {
    int n = sz(a), L = 31 - __builtin_clz(n);
    static vector<complex<double>> R(2, 1);
    static vector<C> rt(2, 1);  // (^ 10% faster if double)
    for (static int k = 2; k < n; k *= 2) {
        R.resize(n);
        rt.resize(n);
        auto x = polar(1.0, acos(-1.0) / k);
        rep(i, k, 2 * k) rt[i] = R[i] = i & 1 ? R[i / 2] * x : R[i / 2];
    }
    vi rev(n);
    rep(i, 0, n) rev[i] = (rev[i / 2] | (i & 1) << L) / 2;
    rep(i, 0, n) if (i < rev[i]) swap(a[i], a[rev[i]]);
    for (int k = 1; k < n; k *= 2)
        for (int i = 0; i < n; i += 2 * k)
            rep(j, 0, k) {
                // C z = rt[j+k] * a[i+j+k]; // (25% faster if hand-rolled)  /// include-line
                auto x = (double *) &rt[j + k], y = (double *) &a[i + j + k];        /// exclude-line
                C z(x[0] * y[0] - x[1] * y[1], x[0] * y[1] + x[1] * y[0]);           /// exclude-line
                a[i + j + k] = a[i + j] - z;
                a[i + j] += z;
            }
}

vi convMod(const vi &a, const vi &b) {
    if (a.empty() || b.empty()) return {};
    vi res(sz(a) + sz(b) - 1);
    int B = 32 - __builtin_clz(sz(res)), n = 1 << B, cut = int(sqrt(MOD));
    vector<C> L(n), R(n), outs(n), outl(n);
    rep(i, 0, sz(a)) L[i] = C((int) a[i] / cut, (int) a[i] % cut);
    rep(i, 0, sz(b)) R[i] = C((int) b[i] / cut, (int) b[i] % cut);
    fft(L), fft(R);
    rep(i, 0, n) {
        int j = -i & (n - 1);
        outl[j] = (L[i] + conj(L[j])) * R[i] / (2.0 * n);
        outs[j] = (L[i] - conj(L[j])) * R[i] / (2.0 * n) / 1i;
    }
    fft(outl), fft(outs);
    rep(i, 0, sz(res)) {
        ll av = ll(real(outl[i]) + .5), cv = ll(imag(outs[i]) + .5);
        ll bv = ll(imag(outl[i]) + .5) + ll(real(outs[i]) + .5);
        res[i] = ((av % MOD * cut + bv) % MOD * cut + cv) % MOD;
    }
    return res;
}

int mul(int a, int b) {
    return 1ll * a * b % MOD;
}

int add(int a, int b) {
    if (a < 0)a += MOD;
    if (b < 0)b += MOD;
    return (a + b) % MOD;
}

int pw(int base, int p) {
    if (p < 0)return 0;
    if (!p) return 1LL;
    int ret = pw(base, p >> 1LL);
    ret = mul(ret, ret);
    if (p & 1LL)
        ret = mul(ret, base);
    return ret;
}

int pw2(int base, ll p) {
    if (p < 0)return 0;
    if (!p) return 1LL;
    int ret = pw2(base, p >> 1LL);
    ret = ret * ret % b;
    if (p & 1LL)
        ret = ret * base % b;
    return ret;
}


typedef array<array<array<int, 10>, 10>, 225> mat;


mat base;


// dp[mod][first][last]


mat shift(const mat x, ll sz) {
    mat ret = mat();
    ll s = pw2(10, sz);
    for (int i = 0; i < b; ++i) {
        for (int j = 0; j <= 9; ++j) {
            for (int k = 0; k <= 9; ++k) {
                ret[(s * i) % b][j][k] = add(ret[(s * i) % b][j][k], x[i][j][k]);
            }
        }
    }
    return ret;
}

mat merge(mat x, mat y) {
    mat ret = mat();
    for (int start = 0; start <= 9; ++start) {
        for (int end = 0; end <= 9; ++end) {
            vector<int> lf(b), rt(b);
            for (int i = 0; i <= 9; ++i) {
                for (int j = 0; j < b; ++j) {
                    lf[j] = add(lf[j], x[j][start][i]);
                    rt[j] = add(rt[j], y[j][i][end]);
                }
            }
            vector<int> res = convMod(lf, rt);
            for (int i = 0; i < res.size(); ++i) {
                ret[i % b][start][end] = add(ret[i % b][start][end], res[i]);
            }
            for (int i = 0; i <= 9; ++i) {
                for (int j = 0; j < b; ++j) {
                    lf[j] = x[j][start][i];
                    rt[j] = y[j][i][end];
                }
                res = convMod(lf, rt);
                for (int j = 0; j < res.size(); ++j) {
                    ret[j % b][start][end] = add(ret[j % b][start][end], -res[j]);
                }
            }
        }
    }
    return ret;
}


mat calc(ll n) {
    if (n == 1) {
        return base;
    }
    auto dp = calc(n / 2);
    dp = merge(shift(dp, n / 2), dp);
    if (n & 1) {
        auto ret = mat();
        for (int j = 0; j < b; ++j) {
            for (int start = 0; start <= 9; ++start) {
                int sum = 0;
                for (int end = 0; end <= 9; ++end) {
                    sum = add(sum, dp[j][start][end]);
                }
                for (int end = 0; end <= 9; ++end) {
                    ret[(j * 10 + end) % b][start][end] = add(ret[(j * 10 + end) % b][start][end],
                                                              add(sum, -dp[j][start][end]));
                }
            }
        }
        return ret;
    }
    return dp;
}

void solve() {
    ll a;
    cin >> a;
    int val;
    cin >> val;
    base = mat();
    for (int i = 0; i <= 9; ++i)base[i][i][i] = 1;
    auto ret = calc(a);
    ll ans = 0;
    for (int i = 1; i <= 9; ++i) {
        for (int j = 0; j <= 9; ++j) {
            ans = add(ans, ret[val][i][j]);
        }
    }
    cout << ans;
}


signed main() {
    Gamal();
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 362ms
memory: 8808kb

input:

12345 200

output:

323756255

result:

ok single line: '323756255'

Test #2:

score: 0
Accepted
time: 169ms
memory: 6360kb

input:

100 87

output:

896364174

result:

ok single line: '896364174'

Test #3:

score: 0
Accepted
time: 171ms
memory: 6356kb

input:

100 35

output:

785970618

result:

ok single line: '785970618'

Test #4:

score: 0
Accepted
time: 334ms
memory: 8608kb

input:

5000 5

output:

176058968

result:

ok single line: '176058968'

Test #5:

score: 0
Accepted
time: 535ms
memory: 10932kb

input:

888888 88

output:

906317283

result:

ok single line: '906317283'

Test #6:

score: 0
Accepted
time: 658ms
memory: 12304kb

input:

9999999 99

output:

133442170

result:

ok single line: '133442170'

Test #7:

score: 0
Accepted
time: 1013ms
memory: 16880kb

input:

101010101010 127

output:

893501348

result:

ok single line: '893501348'

Test #8:

score: 0
Accepted
time: 1289ms
memory: 20480kb

input:

100000000000000 224

output:

106818918

result:

ok single line: '106818918'

Test #9:

score: 0
Accepted
time: 0ms
memory: 3768kb

input:

1 2

output:

1

result:

ok single line: '1'

Test #10:

score: 0
Accepted
time: 29ms
memory: 4600kb

input:

2 1

output:

0

result:

ok single line: '0'

Test #11:

score: 0
Accepted
time: 86ms
memory: 5300kb

input:

10 10

output:

17218742

result:

ok single line: '17218742'

Test #12:

score: 0
Accepted
time: 1383ms
memory: 21444kb

input:

987654321012345 188

output:

687465868

result:

ok single line: '687465868'

Test #13:

score: 0
Accepted
time: 1286ms
memory: 20496kb

input:

123123123123123 123

output:

281426738

result:

ok single line: '281426738'

Test #14:

score: 0
Accepted
time: 1098ms
memory: 17892kb

input:

836692041405 205

output:

878852049

result:

ok single line: '878852049'

Test #15:

score: 0
Accepted
time: 1376ms
memory: 21556kb

input:

686847356299056 65

output:

734639818

result:

ok single line: '734639818'

Test #16:

score: 0
Accepted
time: 81ms
memory: 5348kb

input:

8 8

output:

159456

result:

ok single line: '159456'

Test #17:

score: 0
Accepted
time: 1459ms
memory: 22468kb

input:

9000000000000000 87

output:

824013175

result:

ok single line: '824013175'

Test #18:

score: 0
Accepted
time: 1ms
memory: 3820kb

input:

1 1

output:

1

result:

ok single line: '1'

Test #19:

score: 0
Accepted
time: 29ms
memory: 4732kb

input:

2 2

output:

0

result:

ok single line: '0'