注意完全平方公式的推导
#include <iostream> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define endl "\n" #define LOCAL #define pb push_back #define int long long int l,r; int a[40]; int mod=1e9+7; struct node { int c; int sum; int sumr; }; node f[40][12][12]; int pos[40]; node dfs(int len,int s,int k,int flag)//s每一位数位和取余7,这个整数取余7 { if(!len) { return (node){(s&&k),0,0}; } if(!flag&&f[len][s][k].c!=-1) return f[len][s][k]; int up=flag?a[len]:9; node ans={0,0,0}; for(int i=0;i<=up;i++) { if(i==7) continue; node te=dfs(len-1,(s+i)%7,(k*10+i)%7,flag&&i==a[len]); ans.c=(ans.c+te.c)%mod; ans.sum=((ans.sum+te.sum)%mod+pos[len-1]*te.c%mod*i%mod)%mod; ans.sumr=(ans.sumr+te.sumr+2*i%mod*pos[len-1]%mod*(te.sum)%mod+i*pos[len-1]%mod*i%mod*pos[len-1]%mod*te.c%mod)%mod; } if(!flag) f[len][s][k]=ans; return ans; } int cal(int x) { int len=0; memset(f,-1,sizeof(f)); while(x) { a[++len]=x%10; x/=10; } return dfs(len,0,0,1).sumr; } void solve() { cin>>l>>r; cout<<(cal(r)-cal(l-1)+mod)%mod<<endl; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); //#ifdef LOCAL //freopen("data.in.txt","r",stdin); //freopen("data.out.txt","w",stdout); //#endif int __ = 1; pos[0]=1; for(int i=1;i<=40;i++) { pos[i]=pos[i-1]*10%mod; } cin>>__; while (__--) { solve(); } return 0; }