codeforces 55D


题目链接: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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值