J

 1 #include "cstdio"
 2 #include "math.h"
 3 #include "cstring"
 4 #define mod 1000000007LL
 5 #define LL long long
 6 
 7 struct node
 8 {
 9     LL cnt,sum,sqsum;
10     node() {cnt=-1;sum=sqsum=0;}
11     node(LL cnt,LL sum,LL sqsum):cnt(cnt),sum(sum),sqsum(sqsum) {}
12 }dp[20][10][10];
13 
14 LL a[20],p[25];
15 
16 node dfs(int pos,int re1,int re2,bool limit)
17 {
18     if(!pos) return re1!=0&&re2!=0?node(1,0,0):node(0,0,0);
19     if(!limit&&dp[pos][re1][re2].cnt!=-1) return dp[pos][re1][re2];
20     int up=limit?a[pos]:9;
21     node ans;ans.cnt = 0;
22     for(int i=0;i<=up;i++)
23     {
24         if(i==7) continue;
25         node next=dfs(pos-1,(re1+i)%7,(re2*10+i)%7,limit&&i==up);
26         ans.cnt+=next.cnt;
27         ans.cnt%=mod;
28         ans.sum+=(next.sum+((p[pos]*i)%mod)*next.cnt%mod)%mod;
29         ans.sum%=mod;
30         ans.sqsum+=(next.sqsum+((2*p[pos]*i)%mod)*next.sum)%mod;
31         ans.sqsum%=mod;
32         ans.sqsum+=((next.cnt*p[pos])%mod*p[pos]%mod*i*i%mod);
33         ans.sqsum%=mod;
34     }
35     if(!limit) dp[pos][re1][re2]=ans;
36     return ans;
37 }
38 
39 LL f(LL x)
40 {
41     int pos=0;
42     while(x)
43     {
44         a[++pos]=x%10;
45         x/=10;
46     }
47     node tt=dfs(pos,0,0,true);
48     return tt.sqsum;
49 }
50 
51 int main()
52 {
53     int T;
54     LL l,r;
55     scanf("%d",&T);
56     p[1]=1;
57     for(int i=2;i<=20;i++) p[i]=(p[i-1]*10)%mod;
58     while(T--)
59     {
60         scanf("%I64d%I64d",&l,&r);
61         LL ans=f(r);
62         ans-=f(l-1);
63         printf("%I64d
",(ans%mod+mod)%mod);
64     }
65 }
原文地址:https://www.cnblogs.com/ouyang_wsgwz/p/9135634.html