您的位置:首页 > 其它

codeforces 55D

2016-02-24 19:34 337 查看
题目链接: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: