您的位置:首页 > 其它

HYSBZ 1799 self 同类分布 数位DP -

2016-10-09 20:05 260 查看
题目地址:http://vjudge.net/problem/19309

很明显是数位DP的题目,因为时间很大,所以可以暴力

但是怎么判断能否被整除,这比较麻烦,因为枚举的时候,总数字num在变,每个数和mod也在变

但是由于时间宽裕,而且就一组数据,那么直接枚举mod就好了,mod 1~9+9+9+9+9+9+9...

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxp=21;
typedef long long LL;
LL d[maxp][165][165];
int digit[maxp];
LL DFS(int pos,int sum,int val,int mod,bool limit)
{
if((pos+1)*9<sum) return 0;
if(pos==-1) return sum==0&&val==0;

LL& ans=d[pos][sum][val];
if(!limit&&ans!=-1) return ans;

int up=limit?digit[pos]:9;
LL temp=0;
for(int i=0;i<=up;++i){
if(sum-i<0) break;
temp+=DFS(pos-1,sum-i,(val*10+i)%mod,mod,limit&&i==up);
}
if(!limit) ans=temp;
return temp;
}
LL solve(LL x){
int len=0;
while(x){
digit[len++]=x%10;
x/=10;
}
LL ans=0;
for(int i=1;i<=len*9;i++){
memset(d,-1,sizeof(d));
ans+=DFS(len-1,i,0,i,true);
}
return ans;
}
int main(int argc, char const *argv[])
{
LL a,b;
scanf("%lld%lld",&a,&b);
cout<<solve(b)-solve(a-1)<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: