1799: [Ahoi2009]self 同类分布
Time Limit: 50 Sec Memory Limit: 64 MB
[Submit][Status][Discuss]
Description
给出a,b,求出[a,b]中各位数字之和能整除原数的数的个数。
Input
Output
Sample Input
10 19
Sample Output
3
HINT
【约束条件】1 ≤ a ≤ b ≤ 10^18
Source
类似hdu 4389
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-4
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
ll f[][][],bit[];
ll dp(int pos,int sum,int m,int p,int flag)
{
if(pos==)return (sum==p&&m==);
if(flag&&f[pos][sum][m]!=-)return f[pos][sum][m];
int x=flag?:bit[pos];
ll ans=;
for(int i=;i<=x;i++)
{
ans+=dp(pos-,sum+i,(m*+i)%p,p,flag||i<x);
}
if(flag)f[pos][sum][m]=ans;
return ans;
}
ll getans(ll x,int p)
{
int len=;
while(x)
{
bit[++len]=x%;
x/=;
}
return dp(len,,,p,);
}
int main()
{
ll l,r;
scanf("%lld%lld",&l,&r);
ll ans=;
for(int i=;i<=;i++)
{
memset(f,-,sizeof(f));
ans+=getans(r,i)-getans(l-,i);
}
printf("%lld\n",ans);
return ;
}