QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#325698 | #6522. Digit Mode | Lain | AC ✓ | 326ms | 3808kb | C++23 | 8.3kb | 2024-02-11 20:19:17 | 2024-02-11 20:19:18 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
// Source: neal
// Modulo arithmetic
template <const int &MOD>
struct _m_int {
int val;
_m_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = int(v);
}
_m_int(uint64_t v) {
if (v >= MOD) v %= MOD;
val = int(v);
}
_m_int(int v) : _m_int(int64_t(v)) {}
_m_int(unsigned v) : _m_int(uint64_t(v)) {}
static int inv_mod(int a, int m = MOD) {
// https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Example
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r;
swap(g, r);
x -= q * y;
swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
explicit operator unsigned() const { return val; }
explicit operator int64_t() const { return val; }
explicit operator uint64_t() const { return val; }
explicit operator double() const { return val; }
explicit operator long double() const { return val; }
_m_int &operator+=(const _m_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_m_int &operator-=(const _m_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
#if !defined(_WIN32) || defined(_WIN64)
return unsigned(x % m);
#endif
// Optimized mod for Codeforces 32-bit machines.
// x must be less than 2^32 * m for this to work, so that x / m fits in an
// unsigned 32-bit int.
unsigned x_high = unsigned(x >> 32), x_low = unsigned(x);
unsigned quot, rem;
asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
return rem;
}
_m_int &operator*=(const _m_int &other) {
val = fast_mod(uint64_t(val) * other.val);
return *this;
}
_m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }
friend _m_int operator+(const _m_int &a, const _m_int &b) {
return _m_int(a) += b;
}
friend _m_int operator-(const _m_int &a, const _m_int &b) {
return _m_int(a) -= b;
}
friend _m_int operator*(const _m_int &a, const _m_int &b) {
return _m_int(a) *= b;
}
friend _m_int operator/(const _m_int &a, const _m_int &b) {
return _m_int(a) /= b;
}
_m_int &operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_m_int &operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_m_int operator++(int) {
_m_int before = *this;
++*this;
return before;
}
_m_int operator--(int) {
_m_int before = *this;
--*this;
return before;
}
_m_int operator-() const { return val == 0 ? 0 : MOD - val; }
friend bool operator==(const _m_int &a, const _m_int &b) {
return a.val == b.val;
}
friend bool operator!=(const _m_int &a, const _m_int &b) {
return a.val != b.val;
}
friend bool operator<(const _m_int &a, const _m_int &b) {
return a.val < b.val;
}
friend bool operator>(const _m_int &a, const _m_int &b) {
return a.val > b.val;
}
friend bool operator<=(const _m_int &a, const _m_int &b) {
return a.val <= b.val;
}
friend bool operator>=(const _m_int &a, const _m_int &b) {
return a.val >= b.val;
}
_m_int inv() const { return inv_mod(val); }
_m_int pow(int64_t p) const {
if (p < 0) return inv().pow(-p);
_m_int a = *this, result = 1;
while (p > 0) {
if (p & 1) result *= a;
p >>= 1;
if (p > 0) a *= a;
}
return result;
}
friend ostream &operator<<(ostream &os, const _m_int &m) {
return os << m.val;
}
friend istream &operator>>(istream &is, _m_int &m) {
int64_t x;
is >> x;
m = _m_int(x);
return is;
}
};
const int MOD = 1e9+7;
using mod_int = _m_int<MOD>;
// Source: Based on neal's choose implementation
// Tested on: ABC 235 G
namespace internal_choose {
std::vector<mod_int> factorial = {1}, inv_factorial = {1};
int built_max = 0;
void prepare_factorials(int max) {
if (max <= built_max) return;
max += max / 100;
factorial.resize(max + 1);
inv_factorial.resize(max + 1);
for (int i = built_max + 1; i <= max; i++)
factorial[i] = i * factorial[i - 1];
inv_factorial[max] = factorial[max].inv();
for (int i = max - 1; i > built_max; i--)
inv_factorial[i] = inv_factorial[i + 1] * (i + 1);
built_max = max;
}
}; // namespace internal_choose
mod_int factorial(int n) {
if (n < 0) return 0;
internal_choose::prepare_factorials(n);
return internal_choose::factorial[n];
}
mod_int inv_factorial(int n) {
if (n < 0) return 0;
internal_choose::prepare_factorials(n);
return internal_choose::inv_factorial[n];
}
mod_int choose(int n, int r) {
if (r < 0 || r > n) return 0;
internal_choose::prepare_factorials(n);
return internal_choose::factorial[n] * internal_choose::inv_factorial[n - r] *
internal_choose::inv_factorial[r];
}
mod_int permute(int n, int r) {
if (r < 0 || r > n) return 0;
internal_choose::prepare_factorials(n);
return internal_choose::factorial[n] * internal_choose::inv_factorial[n - r];
}
mod_int inv_choose(int n, int r) {
assert(r >= 0 && r <= n);
internal_choose::prepare_factorials(n);
return internal_choose::inv_factorial[n] * internal_choose::factorial[n - r] *
internal_choose::factorial[r];
}
mod_int inv_permute(int n, int r) {
assert(r >= 0 && r <= n);
internal_choose::prepare_factorials(n);
return internal_choose::inv_factorial[n] * internal_choose::factorial[n - r];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tt;
cin >> tt;
while(tt--) {
string n;
cin >> n;
auto poly_mult = [&](vector<mod_int>& a, vector<mod_int>& b, int tot_size)->vector<mod_int> {
vector<mod_int> res(tot_size);
for (int i =0; i < a.size(); i++) {
for (int j =0; j < b.size(); j++) {
if (i + j >= tot_size) break;
res[i+j] += a[i] * b[j];
}
}
return res;
};
auto F = [&](auto& self, int digit_count, array<int, 10>& start_count, bool can_start_zero)->mod_int {
if (digit_count == 0) {
int mxidx = 0;
for (int i = 1; i < 10; i++) {
if (start_count[mxidx] <= start_count[i]) mxidx = i;
}
return mxidx;
}
mod_int ans = 0;
for (int mode = 1; mode < 10; mode++) {
for (int mode_cnt = 0; mode_cnt <= digit_count; mode_cnt++) {
int need = digit_count - mode_cnt;
vector<mod_int> res(need+1);
res[0] = 1;
for (int j = 0; j < 10; j++) {
if (j == mode) continue;
vector<mod_int> poly;
for (int jcnt = 0; jcnt+start_count[j] <= (j < mode?mode_cnt + start_count[mode]:mode_cnt + start_count[mode]-1); jcnt++) {
poly.push_back(inv_factorial(jcnt));
}
if (poly.empty()) {
res[need] = 0;
break;
}
res = poly_mult(res, poly, need+1);
}
ans += mode*factorial(digit_count) * res[need] * inv_factorial(mode_cnt);
}
}
if (!can_start_zero) {
start_count[0]++;
ans -= self(self, digit_count - 1, start_count, true);
start_count[0]--;
}
return ans;
};
mod_int ans =0 ;
for (int i = 1; i < n.size(); i++) {
array<int, 10> a = {0};
ans += F(F, i, a, false);
}
int curr_dig_cnt = n.size();
array<int, 10> digcnt = {0};
for (int i = 0; i < n.size(); i++) {
curr_dig_cnt--;
for (int add_dig = (i==0?1:0); add_dig < n[i] - '0'; add_dig++) {
digcnt[add_dig]++;
ans += F(F, curr_dig_cnt, digcnt, true);
digcnt[add_dig]--;
}
digcnt[n[i] - '0']++;
}
ans += F(F, curr_dig_cnt, digcnt, true);
cout << ans << '\n';
}
}
// Solve for f(
// number of digits,
// starting count of each digit
// is a starting zero allowed
// )
//
// This is called O(|n| + 8|n|)ish
// 10 * |n| * 10 (mode value) * |n| (node count) * |n| * |n| * |n| * 2 (for zero)
// 200 * |n|^4 * log |n|
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3528kb
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: 1ms
memory: 3756kb
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: 2ms
memory: 3584kb
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: 3ms
memory: 3528kb
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: 3ms
memory: 3524kb
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: 4ms
memory: 3808kb
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: 7ms
memory: 3792kb
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: 18ms
memory: 3536kb
input:
3 5014252832385738 8762796162648653 919997886706385
output:
892033338 297722019 462512414
result:
ok 3 number(s): "892033338 297722019 462512414"
Test #9:
score: 0
Accepted
time: 47ms
memory: 3524kb
input:
2 775701797726112292362823101 75927988177061355614
output:
371275551 566830847
result:
ok 2 number(s): "371275551 566830847"
Test #10:
score: 0
Accepted
time: 205ms
memory: 3760kb
input:
1 65760982925996012426370962570581226245366145016666
output:
661063035
result:
ok 1 number(s): "661063035"
Test #11:
score: 0
Accepted
time: 214ms
memory: 3608kb
input:
1 62597468169905757754175023836706426691470692832490
output:
9983261
result:
ok 1 number(s): "9983261"
Test #12:
score: 0
Accepted
time: 218ms
memory: 3800kb
input:
1 78912847369504885593964702297317051208901751786824
output:
817123221
result:
ok 1 number(s): "817123221"
Test #13:
score: 0
Accepted
time: 326ms
memory: 3604kb
input:
1 99999999999999999999999999999999999999999999999999
output:
25251932
result:
ok 1 number(s): "25251932"
Test #14:
score: 0
Accepted
time: 233ms
memory: 3544kb
input:
1 999999999999999999999999999999999999999999999
output:
439421821
result:
ok 1 number(s): "439421821"
Test #15:
score: 0
Accepted
time: 162ms
memory: 3532kb
input:
1 9999999999999999999999999999999999999999
output:
387537647
result:
ok 1 number(s): "387537647"
Test #16:
score: 0
Accepted
time: 318ms
memory: 3756kb
input:
1 99999999999999999999999998889999898988888889998888
output:
909431898
result:
ok 1 number(s): "909431898"
Test #17:
score: 0
Accepted
time: 323ms
memory: 3596kb
input:
1 99999999999999999999999998989899988889989889999888
output:
289727470
result:
ok 1 number(s): "289727470"
Test #18:
score: 0
Accepted
time: 321ms
memory: 3536kb
input:
1 99999999999999999999999998998988898888898889898999
output:
962896416
result:
ok 1 number(s): "962896416"
Test #19:
score: 0
Accepted
time: 160ms
memory: 3556kb
input:
1 9999999999999999999989988898888989888899
output:
995903330
result:
ok 1 number(s): "995903330"
Test #20:
score: 0
Accepted
time: 162ms
memory: 3528kb
input:
1 9999999999999999999989999889889998998898
output:
385460258
result:
ok 1 number(s): "385460258"
Test #21:
score: 0
Accepted
time: 325ms
memory: 3756kb
input:
1 99999999999999999999999999999999999999999999999999
output:
25251932
result:
ok 1 number(s): "25251932"