数位DP——Codeforces Beta Round #51 D. Beautiful numbers

  • 题目链接: http://codeforces.com/problemset/problem/55/D

  • 分析: 定义Beautiful numbers为一个数x,x能整除它的十进制表示的每一位上的数字。求区间[L,R]内有多少个完美数字

  • 题解:这是一道标准的数位DP,将问题转化为求[1,x]内有多少个beautiful numbers。我们可以通过搜索计算出不同前缀下的数能包含多少个beautiful numbers,然后之后的只要针对每一个数不断累加它的前缀对应的beautiful numbers即可。大致思路就是一遍深搜,重复利用

  • AC代码:

/*************************************************************************
    > File Name: test.cpp
    > Author: Akira 
    > Mail: qaq.febr2.qaq@gmail.com 
 ************************************************************************/

#include<bits/stdc++.h>
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) ((a)*(a))
using namespace std;

#define MaxN 100001
#define MaxM MaxN*10
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
const int mod = 1E9+7;
const double eps = 1e-6;
#define bug cout<<88888888<<endl;
#define debug(x) cout << #x" = " << x << endl;
int T;
LL L,R;
int digits[20];
LL DP[20][1<<8][2520];

LL dfs(int pos, int cnt, int sum, bool limit)
{
    if(pos==0)
    {
        for(int i=2;i<=9;i++)
        {
            if( (cnt&(1<<(i-2))) && (sum%i) ) return 0;
        }
        return 1;
    }
    if(!limit && DP[pos][cnt][sum]!=-1) return DP[pos][cnt][sum];
    int Max = limit ? digits[pos] : 9;
    LL ans = 0;
    for(int i=0;i<=Max;i++)
    {
        ans += dfs(pos-1, i<2 ? cnt : (cnt|(1<<(i-2))), ((sum*10)+i)%2520, limit&&(i==digits[pos]) );
    }
    if(!limit) DP[pos][cnt][sum] = ans;
    return ans;
}

LL find(LL x)
{
    int pos = 0;
    while(x)
    {
        digits[++pos] = x%10;
        x/=10;
    }
    return dfs(pos, 0, 0, true);
}
void solve()
{
    printf("%lld\n", find(R) - find(L-1));
}
int main()
{
    MST(DP,-1);
    //std::ios::sync_with_stdio(false);
    scanf("%d", &T);
    while(T--)
    {
        scanf("%lld%lld", &L, &R);
        solve();
    }
    //system("pause");
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值