题目梗概
如果一个数满足以下三个条件之一,我们称这个数与7有关:
1、整数中某一位是7;
2、整数的每一位加起来的和是7的整数倍;
3、这个整数是7的整数倍;
求一个区间和7无关的数字的平方和。
解题思路
如果这题统计的方式不是平方和,那么就是一道非常经典的数位DP的问题。
其实我们要解决的最主要的问题就是添加一个数后如何快速转移平方和。
储存每个状态的个数,加和,平方和就解决了这个问题。
#include<cstdio>
#define LL long long
using namespace std;
const int maxn=25,tt=1000000007;
int T,len,a[maxn];
struct jz{
LL x,sum,sqsum;
jz(){}
jz(LL a, LL b, LL c):x(a),sum(b),sqsum(c){}
}f[maxn][maxn][maxn];
LL L,R,c[maxn];
jz DFS(int len,int x1,int x2,int pd){
if (len<0) return jz(x1!=0&&x2!=0,0,0);
if (!pd&&f[len][x1][x2].x) return f[len][x1][x2];
int now;if (pd) now=a[len];else now=9;
jz ans=jz(0,0,0);
for (int i=0;i<=now;i++)if (i!=7){
jz lst=DFS(len-1,(x1+i)%7,(x2*10+i)%7,pd&&i==now);
(ans.x+=lst.x)%=tt;
(ans.sum+=c[len]*i%tt*lst.x%tt+lst.sum)%=tt;
(ans.sqsum+=lst.sqsum+c[len]*i*2%tt*lst.sum%tt+c[len]*i%tt*c[len]%tt*i%tt*lst.x%tt)%=tt;
}
if (!pd) f[len][x1][x2]=ans;
return ans;
}
LL work(LL x){
len=0;
while(x>0){
a[len++]=x%10;
x/=10;
}
jz ans=DFS(len-1,0,0,1);
return ans.sqsum;
}
void make(){
c[0]=1;
for (int i=1;i<=18;i++) c[i]=(c[i-1]*10)%tt;
}
int main(){
freopen("exam.in","r",stdin);
freopen("exam.out","w",stdout);
scanf("%d",&T);
make();
while(T--){
scanf("%lld%lld",&L,&R);
printf("%lld\n",(work(R)-work(L-1)+tt)%tt);
}
return 0;
}