codeforces 55D

来源:互联网 发布:sql server 2005安装 编辑:程序博客网 时间:2024/05/20 07:37


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


注意内存

2520是2~9的最大公约数,


#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;typedef __int64 ll;int num[20];ll dp[20][2520][256];//256的二进制表示2~9的有无bool check(int tra, int mod){    for(int i = 0; i < 8; i++)    {        if(mod&(1<<i))        {            if(tra%(i+2) != 0)                return false;        }    }    return true;}ll dfs(int len,int tra,int mod,int fp)//tra是len位之前取模2520的结果{    if(!len)    {        if(!tra || check(tra,mod))            return 1;        return 0;    }    if(!fp && dp[len][tra][mod]!=-1)        return dp[len][tra][mod];    int fpmax = fp ? num[len] : 9;    int tmp;//tmp暂时存放mod;    ll ret = 0;    for(int i = 0; i <= fpmax; i++)    {        tmp =  mod;        if(i > 1)            tmp|=(1<<(i-2));        ret += dfs(len-1,(tra*10+i)%2520,tmp,fp&&i==fpmax);    }    if(!fp)        dp[len][tra][mod] = ret;    return ret;}ll calc(ll x){    int len = 0;    while(x)    {        num[++len] = x%10;        x/=10;    }    return dfs(len,0,0,true);}int main(){    int t;    ll l,r;    scanf("%d",&t);    memset(dp,-1,sizeof(dp));    while(t--)    {        scanf("%I64d %I64d",&l,&r);        printf("%I64d\n",calc(r)-calc(l-1));    }    return 0;}


0 0
原创粉丝点击