BZOJ 1799 同类分布(数位DP)

给出a,b,求出[a,b]中各位数字之和能整除原数的数的个数。1<=a<=b<=1e18.

注意到各位数字之和最大是153.考虑枚举这个东西。那么需要统计的是[0,a-1]和[0,b]内各位数字之和为x且能整除x的数字个数。

那么我们只需要数位dp一波即可。

令dp[pos][i][x]表示有pos位且数字之和为x的数mod P=i的数字个数。

则转移方程显然可得。

# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... LL dp[][][], p[];
int wei[]; LL dfs(int pos, int mod, int limit, int x, int P){
if (x<) return ;
if (pos==) return mod==&&x==;
if (!limit&&~dp[pos][mod][x]) return dp[pos][mod][x];
int up=limit?wei[pos]:;
LL res=;
FOR(i,,up) res+=dfs(pos-,(mod+P-(i*p[pos-]%P))%P,limit&&i==wei[pos],x-i,P);
if (!limit) dp[pos][mod][x]=res;
return res;
}
LL sol(LL x){
int pos=;
while (x) wei[++pos]=x%, x/=;
LL res=;
int top=min(,pos*);
FOR(i,,top) mem(dp,-), res+=dfs(pos,,,i,i);
return res;
}
int main ()
{
LL a, b;
p[]=; FO(i,,) p[i]=p[i-]*;
scanf("%lld%lld",&a,&b);
printf("%lld\n",sol(b)-sol(a-));
return ;
}
上一篇:20个可用于商业用途的免费 CC0 授权的高分辨率图库


下一篇:centos 安装cloud foundry CLI