QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#344709 | #4064. 数位 | hos_lyric | 100 ✓ | 1455ms | 8056kb | C++14 | 7.4kb | 2024-03-04 22:51:22 | 2024-03-04 22:51:23 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
static constexpr unsigned M = M_;
unsigned x;
constexpr ModInt() : x(0U) {}
constexpr ModInt(unsigned x_) : x(x_ % M) {}
constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
ModInt pow(long long e) const {
if (e < 0) return inv().pow(-e);
ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
}
ModInt inv() const {
unsigned a = M, b = x; int y = 0, z = 1;
for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
assert(a == 1U); return ModInt(y);
}
ModInt operator+() const { return *this; }
ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
explicit operator bool() const { return x; }
bool operator==(const ModInt &a) const { return (x == a.x); }
bool operator!=(const ModInt &a) const { return (x != a.x); }
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////
constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;
constexpr int LIM_INV = 110;
Mint inv[LIM_INV], fac[LIM_INV], invFac[LIM_INV];
void prepare() {
inv[1] = 1;
for (int i = 2; i < LIM_INV; ++i) {
inv[i] = -((Mint::M / i) * inv[Mint::M % i]);
}
fac[0] = invFac[0] = 1;
for (int i = 1; i < LIM_INV; ++i) {
fac[i] = fac[i - 1] * i;
invFac[i] = invFac[i - 1] * inv[i];
}
}
Mint binom(Int n, Int k) {
if (n < 0) {
if (k >= 0) {
return ((k & 1) ? -1 : +1) * binom(-n + k - 1, k);
} else if (n - k >= 0) {
return (((n - k) & 1) ? -1 : +1) * binom(-k - 1, n - k);
} else {
return 0;
}
} else {
if (0 <= k && k <= n) {
assert(n < LIM_INV);
return fac[n] * invFac[k] * invFac[n - k];
} else {
return 0;
}
}
}
/*
problem
count (a[1], ..., a[K]) s.t.
- L <= a[i] <= R
- digits of (a[1] + ... + a[K]): non-incr. from highest to lowest
*/
int LLen, RLen;
char L[1010], R[1010];
int K;
Mint dp[1010][2][10][50];
Mint bnss[10][50];
int main() {
prepare();
for (; ~scanf("%s%s%d", L, R, &K); ) {
LLen = strlen(L);
RLen = strlen(R);
reverse(L, L + LLen);
reverse(R, R + RLen);
const int len = RLen + 3;
vector<Mint> ten(len + 1);
ten[0] = 1;
for (int i = 1; i <= len; ++i) ten[i] = ten[i - 1] * 10;
Mint ans = 0;
for (int k = 0; k <= K; ++k) {
// <= (K-k) R + k (L-1)
vector<int> ms(len, 0);
for (int i = 0; i < RLen; ++i) ms[i] += (K - k) * (R[i] - '0');
for (int i = 0; i < LLen; ++i) ms[i] += k * (L[i] - '0');
ms[0] -= k;
for (int i = 0; i < len - 1; ++i) {
int q = ms[i] / 10;
int r = ms[i] % 10;
if (r < 0) {
--q;
r += 10;
}
ms[i + 1] += q;
ms[i] = r;
}
int top = -1;
for (int i = 0; i < len; ++i) if (ms[i]) top = i;
// cerr<<"k = "<<k<<": ms = "<<ms<<endl;
// (pos, smaller, last, l) -> \sum[b: valid sum] binom(-b,l)
memset(dp, 0, sizeof(dp));
for (int i = len; --i >= 0; ) {
for (int x = 0; x < 10; ++x) {
bnss[x][0] = 1;
for (int l = 1; l <= K-1; ++l) {
bnss[x][l] = bnss[x][l - 1] * (-ten[i] * x + 1 - l) * inv[l];
}
}
for (int s = 0; s < 2; ++s) {
for (int x = 0; x < 10; ++x) if (s || x <= ms[i]) {
const int ss = (s || x < ms[i]) ? 1 : 0;
for (int l = 0; l <= K-1; ++l) for (int dl = 0; l + dl <= K-1; ++dl) {
dp[i][ss][x][l + dl] += dp[i + 1][s][x][l] * bnss[x][dl];
}
}
}
// start
for (int x = 0; x < 10; ++x) if (i < top || x <= ms[i]) {
const int ss = (i < top || x < ms[i]) ? 1 : 0;
for (int dl = 0; dl <= K-1; ++dl) {
dp[i][ss][x][dl] += bnss[x][dl];
}
}
// decrease last
for (int s = 0; s < 2; ++s) {
for (int x = 10; --x >= 1; ) {
for (int l = 0; l <= K-1; ++l) {
dp[i][s][x - 1][l] += dp[i][s][x][l];
}
}
}
}
// binom(m-b+K-1, K-1) ways
Mint m = 0;
for (int i = len; --i >= 0; ) (m *= 10) += ms[i];
m += (K-1);
Mint here = 0;
for (int s = 0; s < 2; ++s) {
Mint bn = 1;
for (int l = 0; l <= K-1; ++l) {
here += bn * dp[0][s][0][K-1 - l];
bn *= (m - l);
bn *= inv[1 + l];
}
}
cerr<<k<<": "<<here<<endl;
ans += binom(K, k) * (k&1?-1:+1) * here;
}
printf("%u\n", ans.x);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 4
Accepted
time: 2ms
memory: 7836kb
input:
3392 46912 1
output:
805
result:
ok single line: '805'
Test #2:
score: 4
Accepted
time: 3ms
memory: 7764kb
input:
13532 47266 10
output:
122959566
result:
ok single line: '122959566'
Test #3:
score: 4
Accepted
time: 5ms
memory: 7736kb
input:
15214 22278 20
output:
608248568
result:
ok single line: '608248568'
Test #4:
score: 4
Accepted
time: 9ms
memory: 7740kb
input:
6 99003 30
output:
482049151
result:
ok single line: '482049151'
Test #5:
score: 4
Accepted
time: 20ms
memory: 8012kb
input:
9 80769 50
output:
216492918
result:
ok single line: '216492918'
Test #6:
score: 4
Accepted
time: 3ms
memory: 7740kb
input:
1661415752712512 5964898077730636 10
output:
936625623
result:
ok single line: '936625623'
Test #7:
score: 4
Accepted
time: 3ms
memory: 7764kb
input:
1200579546507207 2780466319667767 10
output:
181223761
result:
ok single line: '181223761'
Test #8:
score: 4
Accepted
time: 7ms
memory: 7764kb
input:
1777841640199213 3258697463679490 20
output:
56613960
result:
ok single line: '56613960'
Test #9:
score: 4
Accepted
time: 12ms
memory: 8036kb
input:
4396068490813531 6104232936024870 30
output:
0
result:
ok single line: '0'
Test #10:
score: 4
Accepted
time: 37ms
memory: 7760kb
input:
16704 9927325292555697 50
output:
40597371
result:
ok single line: '40597371'
Test #11:
score: 4
Accepted
time: 2ms
memory: 7764kb
input:
90277591632981005637511891419553335008894215272 7151128610726280410864354374074497009953207950969 2
output:
698075340
result:
ok single line: '698075340'
Test #12:
score: 4
Accepted
time: 4ms
memory: 7756kb
input:
9477832587309291983340387546272743455167798613 1245715737463737313784854721100496317180360749290 10
output:
162942864
result:
ok single line: '162942864'
Test #13:
score: 4
Accepted
time: 2ms
memory: 7764kb
input:
344549471727489537610822623142934344796179924114613214695586176283275658977506126477184 969554227207884436922056771032600678937706198645623688000238210627413041301376963393077762280632554 2
output:
961546592
result:
ok single line: '961546592'
Test #14:
score: 4
Accepted
time: 2ms
memory: 8056kb
input:
55350509444556236446489457193605524917606412551996706958914890427820 796633666119509733722829652894763207010933659369167574477416331616753023016195847061482606535760353 3
output:
397248010
result:
ok single line: '397248010'
Test #15:
score: 4
Accepted
time: 5ms
memory: 7720kb
input:
138251241998583033991903059942269415823959009536421286667258656732346100589418242462412182985595442 339919849080126654459066794007676689386443370060788033742828108084237457545776545503179913636573448 10
output:
490872962
result:
ok single line: '490872962'
Test #16:
score: 4
Accepted
time: 2ms
memory: 7764kb
input:
927685205033 5575536794982905806425239990181295597257250491679740441581830071525048803618361049491471087844784966092904923890699550851039804070930668638933439325066713794313510011632557255258734207703758205116560 3
output:
181409096
result:
ok single line: '181409096'
Test #17:
score: 4
Accepted
time: 7ms
memory: 7720kb
input:
999569029973884755522795328733368112701348002412014586524980987651729105371176117825904775046301128496282074900254576434321396687405471037392275646394389957155 49393920830259762616940813985547280476949470893658354774636263855848574111237652505416643006097489537167480334086006837361271286305913947797...
output:
702502233
result:
ok single line: '702502233'
Test #18:
score: 4
Accepted
time: 9ms
memory: 7716kb
input:
8540840719139702289624790878247741981564159768603249001614258402171061066624257393702969028823865341113180421296914121333005108958790526773803098324001402111775984503375201804466808904069942757613152652915985666815559894900047034786298234767440923006333478 5755814463185413341244886153840162411268212...
output:
276677763
result:
ok single line: '276677763'
Test #19:
score: 4
Accepted
time: 9ms
memory: 7760kb
input:
4592572524766435916679222618738837049416894166053936895395505912960526507177233771043086690771099275174800794066982280394148031787941300813966995625296415838677651245593193131241422420580434739398657652566305582300249242054203622475682953956814446479923856986575382208157521440213688872793 3167046349...
output:
677937038
result:
ok single line: '677937038'
Test #20:
score: 4
Accepted
time: 41ms
memory: 7760kb
input:
5898816329482189046579078953503480776749598982266875508818427728825444183245110497323545852497668081389765954149072577903205251532234533068434666170086759184744071894408172002010950054326919213467664796456001726377624972619008916368790429445799644337206714697033315020479072988802192631326 3908936160...
output:
497869023
result:
ok single line: '497869023'
Test #21:
score: 4
Accepted
time: 13ms
memory: 7852kb
input:
416077135638317445536943867628542913052302934955640979356188973157589460344101136852730724985718368477325961186386856456820487785898764746554458199915929556462027164812506567366986435575266469646656045545361554974712299613777621011165556685609748945707932215438082820583574624798718852124775398348916...
output:
148524071
result:
ok single line: '148524071'
Test #22:
score: 4
Accepted
time: 63ms
memory: 8044kb
input:
295826594914760304325907309934549158421387322296161141878089722717427179715455470814923006074231600118320692610400298841949944432204702490787104302799797853612986674970446486667294428879359091974427817224968674631416076953231368 36310609856600413922296328635208035277798081477841392427515969406353329...
output:
237151087
result:
ok single line: '237151087'
Test #23:
score: 4
Accepted
time: 360ms
memory: 7852kb
input:
381953364943939098902524953044328524168408632851913776188216901954517439038492667203820226478068621390897046782873629043791900541549306236673442157838603512468035618581682653840726911713269176012731482830013667977227042972776810686877451449181911604023475608546148703168105794003631353879536113541015...
output:
851817319
result:
ok single line: '851817319'
Test #24:
score: 4
Accepted
time: 1455ms
memory: 8056kb
input:
221023242555605899900491008196492231074996838791376676416505250708428133985961719817170395921283156815859622871496409152323312423080394045262737049242076015662416159078412501135147834250684751265051793836549465577791390688041874385541001362905309176109389407929581342753081530034330083207737699687155...
output:
330824991
result:
ok single line: '330824991'
Test #25:
score: 4
Accepted
time: 1453ms
memory: 7764kb
input:
72515968946069616137331569198803027671625997413892339448066260495238126267822345884881140842795833095368403765972532276506951555130898786623069616794 210332671198602929306963624682411927128766482093191016379462700590170137088740959831354360319659063897359912121098422514099790138088919941883046490370...
output:
57981270
result:
ok single line: '57981270'
Extra Test:
score: 0
Extra Test Passed