(solve(r)+mod-solve(l-1))%mod
输出时因为求区间值的mod,所以一定要注意防止出现负数
题目链接
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int mod = 1e9+7;
int a[20],len;
LL w[20];
struct node{
LL num,key1,key2;
bool vis;
node(){num = 0;key1 = 0;key2 = 0;vis = 0;}
};
node dp[20][10][10];
node dfs(int pos,LL sum1,LL sum2,bool limit){
if(pos==0){
node ans;
if(sum1==0) return ans;
if(sum2==0) return ans;
ans.num = 1;
ans.vis = 1;
return ans;
}
if(!limit&&dp[pos][sum1][sum2].vis) return dp[pos][sum1][sum2];
int i;
node ans;
int up = limit?a[pos]:9;
for(i = 0;i <= up;i ++){
if(i==7) continue;
node next = dfs(pos-1,(sum1+i)%7,(sum2*10+i)%7,limit&&i==up);
ans.num = (ans.num+next.num)%mod;
LL num = next.num;
LL v =i*w[pos]%mod;
ans.key1 = (ans.key1+v*num%mod+next.key1)%mod;
ans.key2 = (ans.key2+v*v%mod*num+2*v%mod*next.key1+next.key2)%mod;
}
if(!limit) {
dp[pos][sum1][sum2] = ans;
dp[pos][sum1][sum2].vis = 1;
}
return ans;
}
LL solve(LL n){
len = 0;
while(n!=0){
a[++len] = n%10;
n /= 10;
}
return dfs(len,0,0,1).key2;
}
int main()
{
//freopen("a.txt","r",stdin);
ios::sync_with_stdio(0);
int T;
cin>>T;
w[1] = 1;
for(int i = 2;i <= 19;i ++)
w[i] = (w[i-1] * 10)%mod;
while(T --){
LL l,r;
cin>>l>>r;
cout<<(solve(r)+mod-solve(l-1))%mod<<endl;
}
return 0;
}