Good Bye 2017 G. New Year and Original Order

G. New Year and Original Order

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

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;
}
View Code

九月巨巨的滚动数组:

#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;
}
View Code

 

转载于:https://www.cnblogs.com/weimeiyuer/p/8214666.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值