New Year and Original Order codeforces908G

Translation of the meaning of problems

For n <= 10 700 , n and Q 1 to each of the number of each digit in the number of sorted obtained. The answer membrane 1e9 + 7.

Title Description

Let 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 and output formats

Input formats:

 

The first line of input will contain the integer X ( 1<=X<=10700 ).

 

Output formats:

 

Print a single integer, the answer to the question.

 

Sample input and output

Input Sample # 1:  Copy
21
Output Sample # 1:  Copy
195
Input Sample # 2:  Copy
345342
Output Sample # 2:  Copy
390548434

Explanation

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 .

 

These days made several road digit dp title, yet still feel a little awkward, something always around for a long time on the details ...... it could really stupid comparison

May assume dp [i] [j] [ k] [0/1] represents i bit number to meet stringent less than k digits exactly j digits th

dp[i][j][k][]=∑dp[i-1][j-(l<k)][k][]

Finally, statistical answers, we can not directly use dp [the n-] [j] [k] [1] * k * 10 the n--j , because in fact we do not know k if there is, just know that is strictly less than k numbers have j th

Then there is a ingenious statistical methods:

For the sorted equal to 177 numbers, than a smaller have 0 number than 2-7 small has a number, more than 8 to 9, setting small 2 Number

111*1+11*(7-2+1)+11*(9-8+1)=177

In fact, it can be seen as the 177 per bit apart

Therefore, the final answer is ΣΣdp [n-] [J] [K] [. 1] * [Sigma NJ I =. 1 10 I

All in all digital dp still have to practice more and more mindless, to think clearly before you start writing

 

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=705,mod=1e9+7;
char ss[N];
int n,lim[N];
long long dp[N][N][10][2],fac[N],ans;
long long dfs(int pos,int k,int num,bool limit)
{
    if(k<0) return 0;
    if
    
        
      1,k,num,limit&(i==lim[pos])))%mod;
    dp[pos][k][num][limit]=ret;
    //cout<<pos<<" "<<k<<" "<<num<<" "<<limit<<" "<<ret<<endl;
    return ret;
}
int main()
{
    scanf("%s",ss+1);
    n=strlen(ss+1);
    for(int i=1;i<=n;++i) lim[n-i+1]=ss[i]-'0';
    memset(dp,-1,sizeof(dp));
    for(int i=0;i<=9;++i) dp[0][0][i][0]=dp[0][0][i][1]=1;
    for(int i=1;i<=9;++i)
        for(int j=0;j<=n;++j)
            long long tmp=dfs(n,j,i,1);
    fac[1]=1;
    for(int i=2;i<=n;++i) fac[i]=(fac[i-1]*10+1)%mod;
    for(int i=1;i<=9;++i)
        for(int j=0;j<=n;++j)
            ans=(ans+fac[n-j]*dp[n][j][i][1]%mod)%mod;
    printf("%lld",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/w19567/p/11240870.html