QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#531791 | #6522. Digit Mode | hewanying | WA | 87ms | 3812kb | C++14 | 2.4kb | 2024-08-24 22:17:29 | 2024-08-24 22:17:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define poly vector<int>
const int N=55,H=1e9+7;
int n,ct[N],fac[N],ifac[N],c[N],ans=0,a[N];
string s;
int adc(int a,int b){return a+b>=H?a+b-H:a+b;}
int dec(int a,int b){return a<b?a-b+H:a-b;}
int mul(int a,int b){return 1ll*a*b%H;}
void add(int &a,int b){a=adc(a,b);}
void del(int &a,int b){a=dec(a,b);}
int qpow(int a,int b=H-2){
int res=1;
while(b){if(b&1) res=mul(res,a);a=mul(a,a),b>>=1;}
return res;
}
void init(){
fac[0]=1;
for(int i=1;i<N;i++) fac[i]=mul(fac[i-1],i);
ifac[N-1]=qpow(fac[N-1]);
for(int i=N-1;i>=1;i--) ifac[i-1]=mul(ifac[i],i);
}
int binom(int n,int m){
if(m<0||n<m) return 0;
return mul(fac[n],mul(ifac[m],ifac[n-m]));
}
poly operator *(poly a,poly b){
int len=(int)a.size();
poly c(len);
for(int i=0;i<len;i++) for(int j=0;j<len-i;j++) add(c[i+j],mul(a[i],b[j]));
return c;
}
int calc(int len){
poly res(len+1);res[0]=1;
for(int i=0;i<10;i++){
if(!ct[i]) continue;
poly cur(len+1);
cur[0]=1;
for(int j=1;j<=min(len,ct[i]);j++) cur[j]=ifac[j];
res=res*cur;
}
return mul(res[len],fac[len]);
}
int chk(int x,int y,int len){
for(int i=0;i<10;i++) if(c[i]>y||(c[i]==y&&i>x)) return 0;
if(len-y+c[x]<0) return 0;
for(int i=0;i<10;i++){
ct[i]=0;
if(i==x) continue;
if(i<x) ct[i]=y-c[i];
else ct[i]=y-1-c[i];
}
int res=calc(len-y+c[x]);
return mul(x,mul(res,binom(len,y-c[x])));
}
void Do(int id,int i){
c[i]++;
for(int j=0;j<10;j++)
for(int k=max(1,n/10);k<=n;k++)
add(ans,chk(j,k,n-id));
c[i]--;
}
void dfs(int id,bool lim){
if(id==n+1){
if(lim){
int nw=0,mx=0;
for(int i=0;i<10;i++) if(c[i]>=mx) mx=c[i],nw=i;
add(ans,nw);
}
return;
}
if(!lim){
for(int i=1;i<10;i++) Do(id,i);
return dfs(id+1,0);
}
if(id==1){
dfs(id+1,0);
for(int i=1;i<a[id];i++) Do(id,i);
}else{
for(int i=0;i<a[id];i++) Do(id,i);
}
c[a[id]]++,dfs(id+1,1),c[a[id]]--;
}
void SOLVE(){
cin>>s;n=(int)s.size();ans=0;
for(int i=0;i<10;i++) c[i]=ct[i]=0;
for(int i=1;i<=n;i++) a[i]=s[i-1]-'0';
dfs(1,1);
cout<<ans<<'\n';
}
int main(){
/*2024.8.24 H_W_Y P9640 [SNCPC2019] Digit Mode 数位 dp*/
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int _;cin>>_;init();
while(_--) SOLVE();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 3624kb
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: 3788kb
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: 3784kb
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: 0ms
memory: 3812kb
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: 5ms
memory: 3752kb
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: 6ms
memory: 3596kb
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: 10ms
memory: 3616kb
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: 37ms
memory: 3532kb
input:
3 5014252832385738 8762796162648653 919997886706385
output:
892033338 297722019 462512414
result:
ok 3 number(s): "892033338 297722019 462512414"
Test #9:
score: -100
Wrong Answer
time: 87ms
memory: 3608kb
input:
2 775701797726112292362823101 75927988177061355614
output:
292362741 487918037
result:
wrong answer 1st numbers differ - expected: '371275551', found: '292362741'