QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#557407 | #6522. Digit Mode | chimera | AC ✓ | 350ms | 199216kb | C++11 | 3.6kb | 2024-09-11 09:33:35 | 2024-09-11 09:33:35 |
Judging History
answer
#include <bits/stdc++.h>
#define greedy int
#define mindset main()
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
#define FOR(a,b,c) for(ll a = b; a < (c); a++)
#define FORR(a,b,c) for(ll a = b; a > (c); a--)
#define READ(x) ll x;cin>>x;
#define READAR(x,n) vll x(n); FOR(readar,0,n) cin >> x[readar];
#define READS(x) string x; cin>>x;
#define SEP(n,mx) (((n) == (mx)-1) ? '\n' : ' ')
#define speedfirst ios_base::sync_with_stdio(false); cin.tie(NULL);
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef tuple<ll,ll,ll> tll;
const ll MOD = 1000000007;
vll facts(5000), ifacts(5000);
ll modexp(ll a, ll e) {
ll r = 1;
while(e) {
if(e & 1) r = (r*a) % MOD;
a = (a * a) % MOD;
e >>= 1;
}
return r;
}
ll nCk(ll n, ll k) {
if(k > n) return 0;
if(k < 0) return 0;
ll ans = facts[n]*ifacts[k];
ans %= MOD;
ans *= ifacts[n-k];
ans %= MOD;
return ans;
}
array<array<ll,5000>,5000> ncks;
array<ll, 51> dp;
array<ll, 51> dp2;
ll solve(const array<ll, 9>& mxes, ll N) {
ll su = 0;
for(auto m: mxes) {
if(m < 0) return 0;
su += m;
}
if(su < N) return 0;
dp = {}; // state = (n_spent, valid..) -> ways
dp[0] = 1;
for(ll i = 0; i < 9; i++) {
dp2 = {};
for(ll amt = 0; amt <= N; amt++) {
for(ll amti = 0; (amti + amt <= N) && (amti <= mxes[i]); amti++) {
dp2[amt + amti] += dp[amt] * ncks[N - amt][amti];
dp2[amt+amti] %= MOD;
}
}
dp = dp2;
}
return dp[N];
}
// last `nfree` digits are free, rest are fixed. calc answer.
ll calcsum(const array<ll, 10>& counts, ll nfree) {
ll ans = 0;
for(ll d = 0; d < 10; d++) {
for(ll nd = counts[d]; nd <= counts[d] + nfree; nd++) {
array<ll, 9> mxes;
for(ll d2 = 0; d2 < d; d2++) {
mxes[d2] = nd - counts[d2];
}
for(ll d2 = d+1; d2 < 10; d2++) {
mxes[d2-1] = nd - counts[d2]-1;
}
ans = (ans + d*nCk(nfree, nd - counts[d])*solve(mxes, nfree - (nd-counts[d])) ) % MOD;
}
}
return ans;
}
void solve() {
string S; cin >> S;
// assert(calcsum({0,1,0,0,0,0,0,0,0,0}, 1) == 46);
// assert()
ll ans = 0;
for(ll p = 0; p < S.size()-1; p++) {
for(ll fd = 1; fd < 10; fd++) {
array<ll, 10> cts = {};
cts[fd]++;
ans += calcsum(cts, p);
ans %= MOD;
}
}
array<ll, 10> cts_bef = {};
for(ll p = 0; p < S.size(); p++) {
ll d = S[p] - '0';
if(p == S.size()-1) d++;
ll lo = 0;
if(p == 0) lo++;
for(ll v = lo; v < d; v++) {
array<ll,10> cts = cts_bef;
cts[v]++;
ans += calcsum(cts, S.size() - 1 - p);
ans %= MOD;
}
if(p < S.size()-1) cts_bef[d]++;
}
cout << ans << "\n";
}
greedy mindset {
speedfirst;
facts[0]=1;
for(ll i = 1; i < facts.size(); i++) facts[i] = (i*facts[i-1]) % MOD;
ifacts.back() = modexp(facts.back(), MOD-2);
for(ll i = facts.size()-2; i >= 0; i--) {
ifacts[i] = ( (i+1)*ifacts[i+1] ) % MOD;
}
for(ll a = 0; a < 5000; a++) for(ll b = 0; b < 5000; b++) ncks[a][b] = nCk(a,b);
READ(T); FOR(t,0,T) solve();
}
详细
Test #1:
score: 100
Accepted
time: 40ms
memory: 198988kb
input:
5 9 99 999 99999 999999
output:
45 615 6570 597600 5689830
result:
ok 5 number(s): "45 615 6570 597600 5689830"
Test #2:
score: 0
Accepted
time: 44ms
memory: 198980kb
input:
34 7 48 8 76 1 97 7 5 7 7 2 89 9 4 84 46 6 73 86 78 5 3 8 9 31 24 78 7 11 45 2 65 88 6
output:
28 236 36 420 1 597 28 15 28 28 3 525 45 10 484 221 21 399 500 435 15 6 36 45 145 104 435 28 47 215 3 341 516 21
result:
ok 34 numbers
Test #3:
score: 0
Accepted
time: 35ms
memory: 198932kb
input:
16 935 888 429 370 499 881 285 162 178 948 205 858 573 249 773 615
output:
6009 5618 2456 2078 2905 5562 1603 887 993 6121 1174 5378 3333 1374 4724 3631
result:
ok 16 numbers
Test #4:
score: 0
Accepted
time: 57ms
memory: 198932kb
input:
12 1242 9985 6469 9310 4191 9497 3166 3495 9711 9698 4137 2257
output:
7292 63531 37910 58047 23987 59479 18076 19675 61184 61086 23672 12913
result:
ok 12 numbers
Test #5:
score: 0
Accepted
time: 45ms
memory: 198920kb
input:
10 61195 72739 10164 79164 57851 12326 29132 55992 67377 13873
output:
337575 408170 63792 450686 316513 70493 157773 305011 374163 77954
result:
ok 10 numbers
Test #6:
score: 0
Accepted
time: 42ms
memory: 198988kb
input:
8 529983 127270 421121 291729 461233 695056 365028 271160
output:
2744573 687141 2160067 1500426 2359204 3705475 1851172 1381981
result:
ok 8 numbers
Test #7:
score: 0
Accepted
time: 56ms
memory: 198976kb
input:
7 7934351 8474057 1287369 5845624 7796773 5805755 7349121
output:
42465725 45668947 6716401 30094426 41554096 29861098 38756757
result:
ok 7 numbers
Test #8:
score: 0
Accepted
time: 72ms
memory: 198932kb
input:
3 5014252832385738 8762796162648653 919997886706385
output:
892033338 297722019 462512414
result:
ok 3 number(s): "892033338 297722019 462512414"
Test #9:
score: 0
Accepted
time: 81ms
memory: 199136kb
input:
2 775701797726112292362823101 75927988177061355614
output:
371275551 566830847
result:
ok 2 number(s): "371275551 566830847"
Test #10:
score: 0
Accepted
time: 288ms
memory: 199000kb
input:
1 65760982925996012426370962570581226245366145016666
output:
661063035
result:
ok 1 number(s): "661063035"
Test #11:
score: 0
Accepted
time: 307ms
memory: 199172kb
input:
1 62597468169905757754175023836706426691470692832490
output:
9983261
result:
ok 1 number(s): "9983261"
Test #12:
score: 0
Accepted
time: 315ms
memory: 198932kb
input:
1 78912847369504885593964702297317051208901751786824
output:
817123221
result:
ok 1 number(s): "817123221"
Test #13:
score: 0
Accepted
time: 346ms
memory: 198988kb
input:
1 99999999999999999999999999999999999999999999999999
output:
25251932
result:
ok 1 number(s): "25251932"
Test #14:
score: 0
Accepted
time: 242ms
memory: 198916kb
input:
1 999999999999999999999999999999999999999999999
output:
439421821
result:
ok 1 number(s): "439421821"
Test #15:
score: 0
Accepted
time: 165ms
memory: 198988kb
input:
1 9999999999999999999999999999999999999999
output:
387537647
result:
ok 1 number(s): "387537647"
Test #16:
score: 0
Accepted
time: 350ms
memory: 198872kb
input:
1 99999999999999999999999998889999898988888889998888
output:
909431898
result:
ok 1 number(s): "909431898"
Test #17:
score: 0
Accepted
time: 341ms
memory: 199216kb
input:
1 99999999999999999999999998989899988889989889999888
output:
289727470
result:
ok 1 number(s): "289727470"
Test #18:
score: 0
Accepted
time: 346ms
memory: 198868kb
input:
1 99999999999999999999999998998988898888898889898999
output:
962896416
result:
ok 1 number(s): "962896416"
Test #19:
score: 0
Accepted
time: 176ms
memory: 199004kb
input:
1 9999999999999999999989988898888989888899
output:
995903330
result:
ok 1 number(s): "995903330"
Test #20:
score: 0
Accepted
time: 174ms
memory: 199004kb
input:
1 9999999999999999999989999889889998998898
output:
385460258
result:
ok 1 number(s): "385460258"
Test #21:
score: 0
Accepted
time: 345ms
memory: 198980kb
input:
1 99999999999999999999999999999999999999999999999999
output:
25251932
result:
ok 1 number(s): "25251932"