题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4507
题意:中文,不解释,注意的是求的是合法数字的平方和,即(a+b+c+……)^2
题解:数位DP,要维护三个值,cnt为合法的个数,sum为这cnt个合法个数的和,sqsum为这cnt个数的平方和dp[i][j][k]为第i位前i位的数字和%7的余数为j,前i位的真实值%7为k的状态,求平方和 要展开 如:(a+b)^2=a^2+2*a*b+b^2
1 #include <cstdio> 2 #include <cstring> 3 #define F(i,a,b) for(int i=a;i<=b;++i) 4 typedef long long LL; 5 const LL mod=1e9+7; 6 struct node{ LL cnt,sum,sqsum;}dp[20][10][10]; 7 int bit[20],len;LL p[20],l,r; 8 node dfs(int pos,int mo,int now,bool inf){ 9 if(pos==-1)return (node){(mo!=0&&now!=0&&!inf),0,0}; 10 if(!inf && dp[pos][mo][now].cnt!=-1)return dp[pos][mo][now]; 11 int end=inf?bit[pos]:9; 12 node ans,tmp;ans.cnt=ans.sqsum=ans.sum=0; 13 F(i,0,end){ 14 if(i==7)continue; 15 tmp=dfs(pos-1,(mo+i)%7,(now*10+i)%7,inf&&i==end),ans.cnt+=tmp.cnt,ans.cnt%=mod; 16 ans.sum+=(tmp.sum+((p[pos]*i)%mod)*tmp.cnt%mod)%mod,ans.sum%=mod; 17 ans.sqsum+=(tmp.sqsum+((2*p[pos]*i)%mod)*tmp.sum)%mod,ans.sqsum%=mod; 18 ans.sqsum+=((tmp.cnt*p[pos])%mod*p[pos]%mod*i*i%mod),ans.sqsum%=mod; 19 } 20 if(!inf)dp[pos][mo][now]=ans; 21 return ans; 22 } 23 LL fuck(LL n){for(len=0;n;n/=10)bit[len++]=n%10;return dfs(len-1,0,0,1).sqsum;} 24 int main(){ 25 int T;p[0]=1; 26 F(i,1,19)p[i]=(p[i-1]*10)%mod; 27 F(i,0,19)F(j,0,9)F(k,0,9)dp[i][j][k].cnt=-1; 28 scanf("%d",&T); 29 while(T--){ 30 scanf("%I64d%I64d",&l,&r); 31 LL ans=fuck(r+1); 32 ans-=fuck(l); 33 ans=(ans%mod+mod)%mod; 34 printf("%I64d ",ans); 35 } 36 return 0; 37 }