QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#792691 | #4064. 数位 | propane | 92 | 5575ms | 19776kb | C++20 | 6.7kb | 2024-11-29 13:01:34 | 2024-11-29 13:01:36 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<vector>
#include<array>
#include<cassert>
#include<algorithm>
using namespace std;
using LL = long long;
const int maxn = 2e6 + 5, mod = 998244353, G = 3, Gi = 332748118;
int qpow(int a, int b, int mod){
int res = 1;
while (b){
if (b & 1) res = 1LL * res * a % mod;
a = 1LL * a * a % mod;
b >>= 1;
}
return res;
}
inline int mul(int a, int b){
return 1LL * a * b % mod;
}
inline void add(int &a, int b){
a += b;
if (a >= mod) a -= mod;
}
inline void sub(int &a, int b){
a -= b;
if (a < 0) a += mod;
}
namespace NTT{
vector<int> Omega(int L){
int wn = qpow(G, mod / L, mod);
vector<int> w(L);
w[L >> 1] = 1;
for(int i = L / 2 + 1; i < L; i++) w[i] = mul(w[i - 1], wn);
for(int i = L / 2 - 1; i >= 1; i--) w[i] = w[i << 1];
return w;
}
auto W = Omega(1 << 21);
void DIF(int *a, int n) {
for(int k = n >> 1; k; k >>= 1)
for(int i = 0, y; i < n; i += k << 1)
for(int j = 0; j < k; j++){
y = a[i + j + k], a[i + j + k] = mul(a[i + j] - y + mod, W[k + j]),
add(a[i + j], y);
}
}
void IDIT(int *a, int n) {
for (int k = 1; k < n; k <<= 1)
for (int i = 0, x, y; i < n; i += k << 1)
for(int j = 0; j < k; j++){
x = a[i + j], y = mul(a[i + j + k], W[k + j]),
a[i + j + k] = x - y < 0 ? x - y + mod : x - y;
add(a[i + j], y);
}
int inv = mod - (mod - 1) / n;
for(int i = 0; i < n; i++) a[i] = mul(a[i], inv);
reverse(a + 1, a + n);
}
}
using Poly = std::vector<int>;
void DFT(Poly &a){
NTT::DIF(a.data(), a.size());
}
void IDFT(Poly &a){
NTT::IDIT(a.data(), a.size());
}
int normed(int n) {
return n == 1 ? 1 : (1 << (32 - __builtin_clz(n - 1)));
}
void norm(Poly &a) {
if (!a.empty()) a.resize(normed((int)a.size()), 0);
}
void dot(Poly &a, Poly &b) {
for(int i = 0; i < a.size(); i++)
a[i] = mul(a[i], b[i]);
}
Poly operator*(Poly a, Poly b) {
int n = a.size() + b.size() - 1, L = normed(n);
if (a.size() <= 8 || b.size() <= 8) {
Poly c(n);
for(int i = 0; i < a.size(); i++)
for(int j = 0; j < b.size(); j++)
c[i + j] = (c[i + j] + 1LL * a[i] * b[j]) % mod;
return c;
}
a.resize(L), b.resize(L);
DFT(a), DFT(b), dot(a, b), IDFT(a);
return a.resize(n), a;
}
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[51], inv[51], fact[51], invfact[51];
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);
vector<int> a(k), b(k);
for(int i = 0; i < k; i++){
a[i] = mul(invfact[i], t[i]);
b[i] = mul(invfact[i], pows[i]);
}
a = a * b;
for(int i = 0; i < k; i++){
add(ans[i], mul(fact[i], a[i]));
}
}
array<int, 50> e;
// 当前位数, lim, 是否需要往上借位, 是否全是前导零
array<int, 50> dfs(int u, int last, bool flag, bool lead){
if (u == 0){
return flag ? array<int, 50>{} : e;
}
if (lead) last = 9;
if (v[u][last][flag][lead]) return f[u][last][flag][lead];
v[u][last][flag][lead] = true;
const int up = 9;
array<int, 50> ans{};
if (flag){
for(int i = 0; i <= up and i <= b[u]; i++){
int r = i + 10 - b[u];
if (i <= last) update(ans, dfs(u - 1, i, 1, lead and (i == 0)), mul(r - 1, pow10[u - 1]));
if (r < 10 and i <= last){
update(ans, dfs(u - 1, i, 0, lead and (i == 0)), mul(r, pow10[u - 1]));
}
}
}
else{
for(int i = b[u]; i <= up; i++){
int r = i - b[u];
if (i <= last) update(ans, dfs(u - 1, i, 0, lead and (i == 0)), mul(r, pow10[u - 1]));
if (r - 1 >= 0 and i <= last){
update(ans, dfs(u - 1, i, 1, lead and (i == 0)), mul(r - 1, pow10[u - 1]));
}
}
}
return f[u][last][flag][lead] = ans;
}
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]);
}
fact[0] = 1;
for(int i = 1; i <= 50; i++){
fact[i] = mul(fact[i - 1], i);
}
invfact[0] = 1;
for(int i = 1; i <= 50; i++){
invfact[i] = mul(invfact[i - 1], inv[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;
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 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;
}
memset(v, 0, sizeof v);
auto ans = dfs(R.size() + 3, 9, false, true);
int sign = mul(cnt % 2 == 0 ? 1 : mod - 1, C[k][cnt]);
int c[50]{};
c[0] = 1;
for(int j = 0; j < k - 1; j++){
int nc[50]{};
for(int t = 0; t < 50; t++){
if (t + 1 < 50) add(nc[t + 1], c[t]);
add(nc[t], mul(c[t], mod - j));
}
swap(c, nc);
}
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: 7ms
memory: 11296kb
input:
3392 46912 1
output:
805
result:
ok single line: '805'
Test #2:
score: 4
Accepted
time: 14ms
memory: 11216kb
input:
13532 47266 10
output:
122959566
result:
ok single line: '122959566'
Test #3:
score: 4
Accepted
time: 30ms
memory: 11260kb
input:
15214 22278 20
output:
608248568
result:
ok single line: '608248568'
Test #4:
score: 4
Accepted
time: 44ms
memory: 11668kb
input:
6 99003 30
output:
482049151
result:
ok single line: '482049151'
Test #5:
score: 4
Accepted
time: 142ms
memory: 11712kb
input:
9 80769 50
output:
216492918
result:
ok single line: '216492918'
Test #6:
score: 4
Accepted
time: 24ms
memory: 11628kb
input:
1661415752712512 5964898077730636 10
output:
936625623
result:
ok single line: '936625623'
Test #7:
score: 4
Accepted
time: 21ms
memory: 13644kb
input:
1200579546507207 2780466319667767 10
output:
181223761
result:
ok single line: '181223761'
Test #8:
score: 4
Accepted
time: 73ms
memory: 11664kb
input:
1777841640199213 3258697463679490 20
output:
56613960
result:
ok single line: '56613960'
Test #9:
score: 4
Accepted
time: 109ms
memory: 11532kb
input:
4396068490813531 6104232936024870 30
output:
0
result:
ok single line: '0'
Test #10:
score: 4
Accepted
time: 351ms
memory: 11580kb
input:
16704 9927325292555697 50
output:
40597371
result:
ok single line: '40597371'
Test #11:
score: 4
Accepted
time: 4ms
memory: 11796kb
input:
90277591632981005637511891419553335008894215272 7151128610726280410864354374074497009953207950969 2
output:
698075340
result:
ok single line: '698075340'
Test #12:
score: 4
Accepted
time: 55ms
memory: 11856kb
input:
9477832587309291983340387546272743455167798613 1245715737463737313784854721100496317180360749290 10
output:
162942864
result:
ok single line: '162942864'
Test #13:
score: 4
Accepted
time: 7ms
memory: 12512kb
input:
344549471727489537610822623142934344796179924114613214695586176283275658977506126477184 969554227207884436922056771032600678937706198645623688000238210627413041301376963393077762280632554 2
output:
961546592
result:
ok single line: '961546592'
Test #14:
score: 4
Accepted
time: 13ms
memory: 12148kb
input:
55350509444556236446489457193605524917606412551996706958914890427820 796633666119509733722829652894763207010933659369167574477416331616753023016195847061482606535760353 3
output:
397248010
result:
ok single line: '397248010'
Test #15:
score: 4
Accepted
time: 94ms
memory: 12092kb
input:
138251241998583033991903059942269415823959009536421286667258656732346100589418242462412182985595442 339919849080126654459066794007676689386443370060788033742828108084237457545776545503179913636573448 10
output:
490872962
result:
ok single line: '490872962'
Test #16:
score: 4
Accepted
time: 20ms
memory: 14208kb
input:
927685205033 5575536794982905806425239990181295597257250491679740441581830071525048803618361049491471087844784966092904923890699550851039804070930668638933439325066713794313510011632557255258734207703758205116560 3
output:
181409096
result:
ok single line: '181409096'
Test #17:
score: 4
Accepted
time: 190ms
memory: 14672kb
input:
999569029973884755522795328733368112701348002412014586524980987651729105371176117825904775046301128496282074900254576434321396687405471037392275646394389957155 49393920830259762616940813985547280476949470893658354774636263855848574111237652505416643006097489537167480334086006837361271286305913947797...
output:
702502233
result:
ok single line: '702502233'
Test #18:
score: 4
Accepted
time: 277ms
memory: 14196kb
input:
8540840719139702289624790878247741981564159768603249001614258402171061066624257393702969028823865341113180421296914121333005108958790526773803098324001402111775984503375201804466808904069942757613152652915985666815559894900047034786298234767440923006333478 5755814463185413341244886153840162411268212...
output:
276677763
result:
ok single line: '276677763'
Test #19:
score: 4
Accepted
time: 280ms
memory: 14552kb
input:
4592572524766435916679222618738837049416894166053936895395505912960526507177233771043086690771099275174800794066982280394148031787941300813966995625296415838677651245593193131241422420580434739398657652566305582300249242054203622475682953956814446479923856986575382208157521440213688872793 3167046349...
output:
677937038
result:
ok single line: '677937038'
Test #20:
score: 4
Accepted
time: 1080ms
memory: 14636kb
input:
5898816329482189046579078953503480776749598982266875508818427728825444183245110497323545852497668081389765954149072577903205251532234533068434666170086759184744071894408172002010950054326919213467664796456001726377624972619008916368790429445799644337206714697033315020479072988802192631326 3908936160...
output:
497869023
result:
ok single line: '497869023'
Test #21:
score: 4
Accepted
time: 464ms
memory: 16484kb
input:
416077135638317445536943867628542913052302934955640979356188973157589460344101136852730724985718368477325961186386856456820487785898764746554458199915929556462027164812506567366986435575266469646656045545361554974712299613777621011165556685609748945707932215438082820583574624798718852124775398348916...
output:
148524071
result:
ok single line: '148524071'
Test #22:
score: 4
Accepted
time: 1799ms
memory: 17572kb
input:
295826594914760304325907309934549158421387322296161141878089722717427179715455470814923006074231600118320692610400298841949944432204702490787104302799797853612986674970446486667294428879359091974427817224968674631416076953231368 36310609856600413922296328635208035277798081477841392427515969406353329...
output:
237151087
result:
ok single line: '237151087'
Test #23:
score: 4
Accepted
time: 5575ms
memory: 19776kb
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...