CF908G. New Year and Original Order

CF908G. New Year and Original Order

Solution

对于一个数 x x x,它的贡献为排序之后的值,例如: S ( 50394 ) = 3459 = 3 ∗ 1 0 3 + 4 ∗ 1 0 2 + 5 ∗ 1 0 1 + 9 S(50394)=3459=3*10^3+4*10^2+5*10^1+9 S(50394)=3459=3103+4102+5101+9,也就是每一个数值乘以若干个 10 10 10的次幂的和。
事实上我们可以换一个方式看待这个过程, S ( 50394 ) = 1111 + 1111 + 1111 + 111 + 11 + 1 + 1 + 1 + 1 S(50394)=1111+1111+1111+111+11+1+1+1+1 S(50394)=1111+1111+1111+111+11+1+1+1+1,即对于每一个数值 t = 1..9 t=1..9 t=1..9,都有 111...11 111...11 111...11的贡献,其中 1 1 1的个数取决于不小于 t t t的数位的个数。

这样就可以简单地通过数位 D P DP DP解决了,设 f [ i ] [ j ] [ k ] [ 0 / 1 ] f[i][j][k][0/1] f[i][j][k][0/1]表示前 i i i位,比 j j j大的数位有 k k k个的数有多少个, 0 / 1 0/1 0/1表示是否顶住上界,转移显然。

时间复杂度 O ( n 2 ∗ 1 0 2 ) O(n^2*10^2) O(n2102)

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=1e9+7;
const int MAXN=705;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
	int f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
char st[MAXN]; 
int f[MAXN][10][MAXN][2];
void upd(int &x,int y) { x+y>=mods?x+=y-mods:x+=y; }
int main()
{
	scanf("%s",st+1);
	int n=strlen(st+1);
	for (int i=1;i<=st[1]-'0';i++) f[1][i][1][1]=1;
	for (int i=st[1]-'0'+1;i<=9;i++) f[1][i][0][1]=1;
	for (int i=1;i<=9;i++) f[1][i][1][0]=max(st[1]-'0'-i,0),f[1][i][0][0]=min(i,st[1]-'0');

	for (int i=1;i<n;i++)
		for (int j=1;j<=9;j++)
			for (int k=0;k<=i;k++)	
				for (int t=0;t<=9;t++)
				{
					upd(f[i+1][j][k+(t>=j)][0],f[i][j][k][0]);
					if (t<st[i+1]-'0') upd(f[i+1][j][k+(t>=j)][0],f[i][j][k][1]);
					if (t==st[i+1]-'0') upd(f[i+1][j][k+(t>=j)][1],f[i][j][k][1]);
				}
	int ans=0;
	for (int i=0;i<=9;i++)
		for (int j=1,num=1;j<=n;j++,num=(1ll*num*10+1)%mods) 
			upd(ans,1ll*(f[n][i][j][0]+f[n][i][j][1])*num%mods);
	printf("%d\n",ans);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值