HDU 6093 Rikka with Number
阿新 • • 發佈:2019-02-09
#include<bits/stdc++.h> using namespace std; const int MAXN=5050; const long long mod=998244353; char ch[MAXN]; double L[MAXN]; int I[MAXN],A[MAXN],B[MAXN],C[MAXN],pd[MAXN]; int x[MAXN],y[MAXN],lenx,leny; int len,le; void change(int len,int m) { int x,pre,i; le=0; memcpy(B,A,sizeof(A)); while(len) { x=0; for(i=len;i;i--) { pre=x; x=(x*10+B[i])%m; B[i]=(pre*10+B[i])/m; } C[++le]=x; while(len&&B[len]==0) len--; } } int calc(int m) { change(len,m); if(le>m) return (I[m]-I[m-1]+mod)%mod; if(le<m) return 0; int i,j,ans; for(i=0;i<m;i++) pd[i]=0; ans=0; for(i=1;i<C[le];i++) ans=(ans+I[le-1])%mod; pd[C[le]]=1; for(i=le-1;i>0;i--) { for(j=0;j<C[i];j++) { if(pd[j]==0) ans=(ans+I[i-1])%mod; } if(pd[C[i]]) break; pd[C[i]]=1; if(i==1) ans=(ans+1)%mod; } return ans; } int calc() { if(len==0) return 0; int ans,where,i; ans=0; where=2; while(L[where]<=len-1) where++; if(where<=15) { for(i=2;i<=15;i++) ans=(ans+calc(i))%mod; return ans; } for(i=2;i<=where-2;i++) ans=((ans+I[i]-I[i-1])%mod+mod)%mod; ans=(ans+calc(where-1)+calc(where))%mod; return ans; } int main() { int i,T,lenx,ans; len=1; while(L[len]<MAXN) { len++; L[len]=log(len)/log(10)*(len-1); } I[0]=1; for(i=1;i<MAXN;i++) I[i]=I[i-1]*1ll*i%mod; scanf("%d",&T); while(T--) { scanf("%s",ch+1); len=strlen(ch+1); for(i=1;i<=len;i++) A[i]=ch[len-i+1]-'0'; lenx=len; for(i=1;i<=len;i++) x[i]=A[i]; A[1]--; for(i=1;i<=len;i++) { if(A[i]<0) { A[i+1]--; A[i]+=10; } } while(len&&A[len]==0) len--; ans=calc(); scanf("%s",ch+1); len=strlen(ch+1); for(i=1;i<=len;i++) A[i]=ch[len-i+1]-'0'; leny=len; for(i=1;i<=len;i++) y[i]=A[i]; ans=(calc()-ans+mod)%mod; printf("%d\n",ans); } }