码迷,mamicode.com
首页 > 其他好文 > 详细

数位dp整理

时间:2015-04-18 21:59:19      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

1.CodeForces 55DBeautiful numbers

题目大意:一个数是幸运数当且仅当这个数能整除所有位数,求[a,b]有多少幸运数

技术分享
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxa = 20;
const int mod = 2520;
long long  dp[maxa][1<<8][mod];
#define LL long long
//当前位置,限制,有哪些数,模
int num[maxa];
LL dfs(int pos, int limit, int val, int numb){
    if(pos == 0){
        for(int i = 2; i < 10; i++){
            if((1<<(i-2)) & val){
                if(numb % i) return 0;
            }
        }return 1;
    }
    if(!limit && dp[pos][val][numb] != -1) return dp[pos][val][numb];
    int nn = limit?num[pos]:9;
    LL res = 0;
    for(int i = 0; i <= nn; i++){
        res += dfs(pos-1, limit & (i ==nn), i<2?val:val|(1<<(i-2)), (numb*10+i)%mod);
    }
    if(!limit) dp[pos][val][numb] = res;
    return res;
}
LL ANS(LL n){
    int leng = 0;
    while(n){
        num[++leng] = n %10;
        n /= 10;
    }
    return dfs(leng, 1, 0, 0);
}
int main(){
    int t;
    LL a, b;
    memset(dp, -1, sizeof(dp));
    scanf("%d", &t);
    while(t--){
        cin>>a>>b;
        if(a > b)swap(a, b);
        cout<<ANS(b) - ANS(a-1)<<endl;
    }
}
View Code

 

数位dp整理

标签:

原文地址:http://www.cnblogs.com/icodefive/p/4437999.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!