dp过程主要是三个参数,第一个是位置pos,第二个是pre(数字取余7),第三个是sum(数位之和取余7)
递推的过程pre这一维要乘以10再加上i,sum这一维度加上i即可,注意都要取余7
然后注意要开long long,f数组存下能任意填以后的数值,以便加速后面的操作,不然会超时。
#include <bits/stdc++.h>
using namespace std;
#define int long long
int a[20];
int f[20][20][20];
int dp(int pos,int pre,int sum,int flag)
{
if(pos==0) return sum%7==0&&pre%7==0;
if(flag&&f[pos][pre][sum]!=-1) return f[pos][pre][sum];
int x=flag?9:a[pos];
int ans=0;
for(int i=0;i<=x;i++)
{
ans+=dp(pos-1,(i+10*pre)%7,(sum+i)%7,flag||i<x);
}
if(flag) f[pos][pre][sum]=ans;
return ans;
}
int cul(int x)
{
int pos=0;
while(x)
{
a[++pos]=x%10;
x/=10;
}
return dp(pos,0,0,false);
}
signed main()
{
int n,m;
while(scanf("%lld%lld",&n,&m)!=EOF)
{
if(n==0&&m==0) break;
memset(f,-1,sizeof(f));
printf("%lld\n",cul(m)-cul(n-1));
}
}


京公网安备 11010502036488号