QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#137846 | #6522. Digit Mode | PetroTarnavskyi# | WA | 2ms | 3660kb | C++17 | 2.8kb | 2023-08-10 18:17:27 | 2023-08-10 18:17:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define FOR(i, a, b) for (int i = (a); i<(b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i>=(a); --i)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define FILL(a, b) memset(a, b, sizeof(a))
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int mod = 1e9 + 7;
int add(int a, int b){
return (a + b < mod) ? (a + b) : (a + b - mod);
}
void ADD(int &a, int b){
a = add(a, b);
}
int mult(int a, int b){
return 1LL * a * b % mod;
}
int binpow(int a, int n){
int res = 1;
while(n){
if(n & 1)
res = mult(res, a);
a = mult(a, a);
n /= 2;
}
return res;
}
const int N = 50 + 2;
const int A = 10 + 1;
int res0[N] = {0, 45, 570, 5955, 57087, 533943, 5092230, 50327850, 508658835, 141577340, 346748073, 63605721, 691146974, 747499077, 538394750, 265137024, 856024632, 427621971, 516162438, 440472972, 712880662, 791425298, 982743029, 438780983, 383079911, 230599376, 905706960, 197714458, 341824438, 954125005, 98726828, 948875929, 829350644, 341637655, 685873588, 261300047, 133758564, 474399230, 421324133, 863646999, 790696542, 382317337, 207636000, 134734713, 4883420, 322312711, 935473927, 97027071, 538107255, 825526842, 189695037, 799178340};
int fact[N], ober[N];
int dp[A][N];
int DP(int len, VI cnts, int digit, int mx){
FOR(i, 0, digit)
cnts[i] = mx - cnts[i];
int ml = mult(fact[len], ober[mx - cnts[digit]]);
len -= (mx - cnts[digit]);
cnts[digit] = 0;
FOR(i, digit + 1, 10)
cnts[i] = mx - 1 - cnts[i];
FOR(i, 0, 10)
if(cnts[i] < 0)
return 0;
FOR(i, 0, 11)
FOR(j, 0, len + 1)
dp[i][j] = 0;
dp[0][len] = 1;
FOR(i, 0, 11){
FOR(j, 0, len + 1){
if(dp[i][j] == 0)
continue;
RFOR(cur, min(j, cnts[i]) + 1, 0)
ADD(dp[i + 1][j - cur], mult(dp[i][j], ober[cur]));
}
}
return mult(dp[10][0], ml);
}
int solve(int len, VI cnts){
int res = 0;
FOR(bit, 0, 10){
FOR(mx, 0, len + 1)
res = add(res, mult(DP(len, cnts, bit, mx + cnts[bit]), bit));
}
return res;
}
void solve(){
string n;
cin >> n;
VI cnts(10);
int ans = 0;
FOR(i, 0, SZ(n)){
FOR(j, (i == 0), n[i] - '0'){
cnts[j]++;
ADD(ans, solve(SZ(n) - 1 - i, cnts));
cnts[j]--;
}
cnts[n[i] - '0']++;
ans = add(ans, res0[i]);
}
ADD(ans, solve(0, cnts));
cout << ans << "\n";
}
void prec(){
fact[0] = 1;
FOR(i, 1, N)
fact[i] = mult(fact[i - 1], i);
ober[N - 1] = binpow(fact[N - 1], mod - 2);
RFOR(i, N, 1)
ober[i - 1] = mult(ober[i], i);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
prec();
int t;
cin >> t;
while(t--)
solve();
cerr << 1.0 * clock() / CLOCKS_PER_SEC << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 3660kb
input:
5 9 99 999 99999 999999
output:
45 615 6570 155620322 784620024
result:
wrong answer 4th numbers differ - expected: '597600', found: '155620322'