G. New Year and Original Order
time limit per test
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputLet S(n) denote the number that represents the digits of n in sorted order. For example, S(1) = 1, S(5) = 5, S(50394) = 3459, S(353535) = 333555.
Given a number X, compute modulo 109 + 7.
Input
The first line of input will contain the integer X (1 ≤ X ≤ 10700).
Output
Print a single integer, the answer to the question.
Examples
Input
21
Output
195
Input
345342
Output
390548434
Note
The first few values of S are 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 12. The sum of these values is 195.
tls原话:最简单的数位dp,出题人成功骗过验题人放在了G。
Oh...uuu...原来是简单的数位dp...等等...tls的简单,莫不是.....
#include<bits/stdc++.h> #define LL long long using namespace std; char s[705]; int dp[705][705][2],res; const int mod=1e9+7; int main() { cin>>s; int len=strlen(s); int ll,kk,i,j,k,l,m; for(i=1;i<=9;i++) { dp[0][0][1]=1; for(j=0;j<len;j++) for(k=0;k<=j;k++) for(l=0;l<=1;l++) for(m=0;m<=9;m++) { if(m<s[j]-'0') ll=0; else if(m==s[j]-'0') ll=l; else if(l) continue; else ll=0; if(m>=i) kk=k+1; else kk=k; dp[j+1][kk][ll]=(dp[j+1][kk][ll]+dp[j][k][l])%mod; } for(j=1,k=1;j<=len;j++) { res=(res+(LL)k*(dp[len][j][0]+dp[len][j][1]))%mod; k=(k*10ll+1)%mod; } for(j=0;j<=len;j++) for(k=0;k<=j;k++) for(l=0;l<=1;l++) dp[j][k][l]=0; } cout<<res<<endl; return 0; }
九月巨巨的滚动数组:
#include<bits/stdc++.h> using namespace std; const int mod=1e9+7,maxn=710; #define LL long long LL sum[maxn][2],dp[maxn][2]; string s; inline void Update(LL &x,LL y) { x=(x+y)%mod; } void Clear() { memset(dp,0,sizeof(dp)); memset(sum,0,sizeof(sum)); dp[0][0]=1; } int main() { cin>>s; int len=s.length(); LL ans=0; for(int l=1;l<=9;l++) { Clear(); for(int i=0;i<len;i++) for(int j=0;j<2;j++) for(int k=0;k<=9;k++) { if(!j&&k>s[i]-'0') continue; Update(dp[i+1][j|k<s[i]-'0'],dp[i][j]); ///j和k<s[i] 任意一个是1就为1 if(k<l) Update(sum[i+1][j|k<s[i]-'0'],sum[i][j]); else Update(sum[i+1][j|k<s[i]-'0'],(sum[i][j]*10+dp[i][j])%mod); } Update(ans,sum[len][0]); Update(ans,sum[len][1]); } cout<<ans<<endl; return 0; }