• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

P2602 [ZJOI2010]数字计数【数位dp】

互联网 diligentman 2周前 (04-07) 7次浏览

Link

数位dp

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
const int mod=998244353;
const long long inf=1e18;
const int base=131;
const double pi=3.1415926;
#define ll long long
#define int long long
#define ull unsigned long long
#define maxx(a,b) (a>b?a:b)
#define minx(a,b) (a<b?a:b)
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(...) fprintf(stderr, __VA_ARGS__)
inline ll qpow(ll base, ll n) { assert(n >= 0); ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
ll gcd(ll a,ll b) {return b==0?a:gcd(b,a%b);}
ll lcm(ll a,ll b) { return a*b/gcd(a,b); }
ll inv(ll a) {return a == 1 ? 1 : (ll)(mod - mod / a) * inv(mod % a) % mod;}
ll C(ll n,ll m){if (m>n) return 0;ll ans = 1;for (int i = 1; i <= m; ++i) ans=ans*inv(i)%mod*(n-i+1)%mod;return ans%mod;}
ll A(ll n,ll m){ll sum=1; for(int i=n;i>=n-m+1;i--) sum=(sum*i)%mod; return sum%mod;}
ll GetSum(ll L, ll R) {return (R - L + 1ll) * (L + R) / 2ll;} //等差数列求和 
 
/************/
int a,b,dig[15],pos;
int dp[15][15][2][2];
//dp[pos][cnt]:前pos位数字x出现了cnt次对应的答案 
int dfs(int pos,int cnt,int zero,int lim,int now){
	if(!pos) return cnt;
	if(dp[pos][cnt][zero][lim]!=-1) return dp[pos][cnt][zero][lim];
	int up=(lim?dig[pos]:9);
	int tmp=0;
	for(int i=0;i<=up;i++){
		if(zero&&i==0) tmp+=dfs(pos-1,cnt,zero&&i==0,lim&&i==up,now);
		else tmp+=dfs(pos-1,cnt+(i==now),zero&&i==0,lim&&i==up,now);
	}
	return dp[pos][cnt][zero][lim]=tmp;
}
int sol(int x,int i){
	memset(dp,-1,sizeof(dp));
	pos=0;
	while(x){
		dig[++pos]=x%10;
		x/=10;
	}
	return dfs(pos,0,1,1,i);
}
signed main()
{
	scanf("%lld%lld",&a,&b);
	for(int i=0;i<=9;i++) printf("%lld ",sol(b,i)-sol(a-1,i));
	return 0;
}


程序员灯塔
转载请注明原文链接:P2602 [ZJOI2010]数字计数【数位dp】
喜欢 (0)