QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#793168 | #4064. 数位 | propane | 92 | 3345ms | 11452kb | C++20 | 5.3kb | 2024-11-29 17:25:33 | 2024-11-29 17:25:34 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<vector>
#include<array>
#include<cassert>
#include<climits>
using namespace std;
using LL = long long;
const int mod = 998244353;
void add(int &a, int b){
a += b;
if (a >= mod) a -= mod;
}
int mul(int a, int b){
return 1LL * a * b % mod;
}
int C[51][51];
int a[1010], b[1010], c[1010];
bool v[1010][10][2][2];
array<int, 50> f[1010][10][2][2];
int pow10[1010], pows[50], inv[50];
unsigned long long tmp[50];
const unsigned long long lim = ULLONG_MAX - (unsigned long long)1e18;
int k;
void update(array<int, 50> &ans, const array<int, 50> &t, int val){
pows[0] = 1;
for(int i = 1; i < k; i++) pows[i] = mul(pows[i - 1], val);
memset(tmp, 0, sizeof tmp);
for(int i = 0; i < k; i++){
for(int j = 0; j <= i; j++){
tmp[i] += 1ULL * mul(pows[j], t[i - j]) * C[i][j];
if (tmp[i] > lim){
tmp[i] %= mod;
}
}
}
for(int i = 0; i < k; i++){
add(ans[i], tmp[i] % mod);
}
}
int main(){
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
for(int i = 0; i <= 50; i++){
for(int j = 0; j <= i; j++){
if (!j) C[i][j] = 1;
else C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
}
}
inv[1] = 1;
for(int i = 2; i < 50; i++){
inv[i] = mul(mod - mod / i, inv[mod % i]);
}
string L, R;
cin >> L >> R;
int pos = R.size() - 1;
while(pos >= 0 and R[pos] == '9') pos--;
if (pos == -1){
R = "1" + string(R.size(), '0');
}
else{
R[pos] += 1;
for(int i = pos + 1; i < R.size(); i++){
R[i] = '0';
}
}
cin >> k;
array<int, 50> e{};
e[0] = 1;
for(int i = 1; i < k; i++){
e[i] = mul(e[i - 1], k - 1);
}
pow10[0] = 1;
for(int i = 1; i < 1010; i++){
pow10[i] = mul(pow10[i - 1], 10);
}
int c[51]{};
c[0] = 1;
for(int j = 0; j < k - 1; j++){
int nc[51]{};
for(int t = 0; t < 50; t++){
add(nc[t + 1], c[t]);
add(nc[t], mul(c[t], mod - j));
}
swap(c, nc);
}
int sum = 0;
for(int cnt = 0; cnt <= k; cnt++){
memset(b, 0, sizeof b);
for(int i = 0; i < L.size(); i++){
b[L.size() - i] += (L[i] - '0') * (k - cnt);
}
for(int i = 0; i < R.size(); i++){
b[R.size() - i] += (R[i] - '0') * cnt;
}
for(int i = 1; i + 1 < 1010; i++){
b[i + 1] += b[i] / 10;
b[i] %= 10;
}
for(int u = 1; u <= R.size() + 3; u++){
for(int last = 0; last < 10; last++){
for(int flag = 0; flag < 2; flag++){
for(int lead = 0; lead < 2; lead++){
f[u][last][flag][lead].fill(0);
}
}
}
}
for(int last = 0; last < 10; last++){
for(int flag = 0; flag <= 0; flag++){
for(int lead = 0; lead < 2; lead++){
f[0][last][flag][lead] = e;
}
}
}
for(int u = 1; u <= R.size() + 3; u++){
for(int last = 0; last < 10; last++){
for(int flag = 0; flag < 2; flag++){
for(int lead = 0; lead < 2; lead++){
if (lead and last != 9) continue;
auto &ans = f[u][last][flag][lead];
ans.fill(0);
if (flag){
for(int i = 0; i <= 9 and i <= b[u]; i++){
int r = i + 10 - b[u];
if (i <= last) update(ans, f[u - 1][(lead and (i == 0)) ? 9 : i][1][lead and (i == 0)], mul(r - 1, pow10[u - 1]));
if (r < 10 and i <= last){
if (i <= last) update(ans, f[u - 1][(lead and (i == 0)) ? 9 : i][0][lead and (i == 0)], mul(r, pow10[u - 1]));
}
}
}
else{
for(int i = b[u]; i <= 9; i++){
int r = i - b[u];
if (i <= last) update(ans, f[u - 1][(lead and (i == 0)) ? 9 : i][0][lead and (i == 0)], mul(r, pow10[u - 1]));
if (r - 1 >= 0 and i <= last){
if (i <= last) update(ans, f[u - 1][(lead and (i == 0)) ? 9 : i][1][lead and (i == 0)], mul(r - 1, pow10[u - 1]));
}
}
}
}
}
}
}
auto ans = f[R.size() + 3][9][0][1];
int sign = mul(cnt % 2 == 0 ? 1 : mod - 1, C[k][cnt]);
int t = 0;
for(int i = 0; i < k; i++){
add(t, mul(ans[i], c[i]));
}
for(int i = 1; i <= k - 1; i++){
t = mul(t, inv[i]);
}
add(sum, mul(sign, t));
}
cout << sum << '\n';
}
详细
Pretests
Final Tests
Test #1:
score: 4
Accepted
time: 1ms
memory: 3708kb
input:
3392 46912 1
output:
805
result:
ok single line: '805'
Test #2:
score: 4
Accepted
time: 2ms
memory: 3712kb
input:
13532 47266 10
output:
122959566
result:
ok single line: '122959566'
Test #3:
score: 4
Accepted
time: 10ms
memory: 3644kb
input:
15214 22278 20
output:
608248568
result:
ok single line: '608248568'
Test #4:
score: 4
Accepted
time: 27ms
memory: 3624kb
input:
6 99003 30
output:
482049151
result:
ok single line: '482049151'
Test #5:
score: 4
Accepted
time: 110ms
memory: 3632kb
input:
9 80769 50
output:
216492918
result:
ok single line: '216492918'
Test #6:
score: 4
Accepted
time: 5ms
memory: 3648kb
input:
1661415752712512 5964898077730636 10
output:
936625623
result:
ok single line: '936625623'
Test #7:
score: 4
Accepted
time: 5ms
memory: 3936kb
input:
1200579546507207 2780466319667767 10
output:
181223761
result:
ok single line: '181223761'
Test #8:
score: 4
Accepted
time: 23ms
memory: 3740kb
input:
1777841640199213 3258697463679490 20
output:
56613960
result:
ok single line: '56613960'
Test #9:
score: 4
Accepted
time: 65ms
memory: 3780kb
input:
4396068490813531 6104232936024870 30
output:
0
result:
ok single line: '0'
Test #10:
score: 4
Accepted
time: 260ms
memory: 4012kb
input:
16704 9927325292555697 50
output:
40597371
result:
ok single line: '40597371'
Test #11:
score: 4
Accepted
time: 1ms
memory: 4036kb
input:
90277591632981005637511891419553335008894215272 7151128610726280410864354374074497009953207950969 2
output:
698075340
result:
ok single line: '698075340'
Test #12:
score: 4
Accepted
time: 8ms
memory: 4012kb
input:
9477832587309291983340387546272743455167798613 1245715737463737313784854721100496317180360749290 10
output:
162942864
result:
ok single line: '162942864'
Test #13:
score: 4
Accepted
time: 2ms
memory: 4352kb
input:
344549471727489537610822623142934344796179924114613214695586176283275658977506126477184 969554227207884436922056771032600678937706198645623688000238210627413041301376963393077762280632554 2
output:
961546592
result:
ok single line: '961546592'
Test #14:
score: 4
Accepted
time: 3ms
memory: 4388kb
input:
55350509444556236446489457193605524917606412551996706958914890427820 796633666119509733722829652894763207010933659369167574477416331616753023016195847061482606535760353 3
output:
397248010
result:
ok single line: '397248010'
Test #15:
score: 4
Accepted
time: 23ms
memory: 5832kb
input:
138251241998583033991903059942269415823959009536421286667258656732346100589418242462412182985595442 339919849080126654459066794007676689386443370060788033742828108084237457545776545503179913636573448 10
output:
490872962
result:
ok single line: '490872962'
Test #16:
score: 4
Accepted
time: 6ms
memory: 5632kb
input:
927685205033 5575536794982905806425239990181295597257250491679740441581830071525048803618361049491471087844784966092904923890699550851039804070930668638933439325066713794313510011632557255258734207703758205116560 3
output:
181409096
result:
ok single line: '181409096'
Test #17:
score: 4
Accepted
time: 45ms
memory: 6556kb
input:
999569029973884755522795328733368112701348002412014586524980987651729105371176117825904775046301128496282074900254576434321396687405471037392275646394389957155 49393920830259762616940813985547280476949470893658354774636263855848574111237652505416643006097489537167480334086006837361271286305913947797...
output:
702502233
result:
ok single line: '702502233'
Test #18:
score: 4
Accepted
time: 64ms
memory: 7504kb
input:
8540840719139702289624790878247741981564159768603249001614258402171061066624257393702969028823865341113180421296914121333005108958790526773803098324001402111775984503375201804466808904069942757613152652915985666815559894900047034786298234767440923006333478 5755814463185413341244886153840162411268212...
output:
276677763
result:
ok single line: '276677763'
Test #19:
score: 4
Accepted
time: 60ms
memory: 7520kb
input:
4592572524766435916679222618738837049416894166053936895395505912960526507177233771043086690771099275174800794066982280394148031787941300813966995625296415838677651245593193131241422420580434739398657652566305582300249242054203622475682953956814446479923856986575382208157521440213688872793 3167046349...
output:
677937038
result:
ok single line: '677937038'
Test #20:
score: 4
Accepted
time: 350ms
memory: 7864kb
input:
5898816329482189046579078953503480776749598982266875508818427728825444183245110497323545852497668081389765954149072577903205251532234533068434666170086759184744071894408172002010950054326919213467664796456001726377624972619008916368790429445799644337206714697033315020479072988802192631326 3908936160...
output:
497869023
result:
ok single line: '497869023'
Test #21:
score: 4
Accepted
time: 109ms
memory: 8628kb
input:
416077135638317445536943867628542913052302934955640979356188973157589460344101136852730724985718368477325961186386856456820487785898764746554458199915929556462027164812506567366986435575266469646656045545361554974712299613777621011165556685609748945707932215438082820583574624798718852124775398348916...
output:
148524071
result:
ok single line: '148524071'
Test #22:
score: 4
Accepted
time: 583ms
memory: 8368kb
input:
295826594914760304325907309934549158421387322296161141878089722717427179715455470814923006074231600118320692610400298841949944432204702490787104302799797853612986674970446486667294428879359091974427817224968674631416076953231368 36310609856600413922296328635208035277798081477841392427515969406353329...
output:
237151087
result:
ok single line: '237151087'
Test #23:
score: 4
Accepted
time: 3345ms
memory: 11452kb
input:
381953364943939098902524953044328524168408632851913776188216901954517439038492667203820226478068621390897046782873629043791900541549306236673442157838603512468035618581682653840726911713269176012731482830013667977227042972776810686877451449181911604023475608546148703168105794003631353879536113541015...
output:
851817319
result:
ok single line: '851817319'
Test #24:
score: 0
Time Limit Exceeded
input:
221023242555605899900491008196492231074996838791376676416505250708428133985961719817170395921283156815859622871496409152323312423080394045262737049242076015662416159078412501135147834250684751265051793836549465577791390688041874385541001362905309176109389407929581342753081530034330083207737699687155...
output:
result:
Test #25:
score: 0
Time Limit Exceeded
input:
72515968946069616137331569198803027671625997413892339448066260495238126267822345884881140842795833095368403765972532276506951555130898786623069616794 210332671198602929306963624682411927128766482093191016379462700590170137088740959831354360319659063897359912121098422514099790138088919941883046490370...