题目连接: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
#include <cstdio>
#include <cstring>
#define F(i,a,b) for(int i=a;i<=b;++i)
typedef long long LL;
const LL mod=1e9+;
struct node{ LL cnt,sum,sqsum;}dp[][][];
int bit[],len;LL p[],l,r;
node dfs(int pos,int mo,int now,bool inf){
if(pos==-)return (node){(mo!=&&now!=&&!inf),,};
if(!inf && dp[pos][mo][now].cnt!=-)return dp[pos][mo][now];
int end=inf?bit[pos]:;
node ans,tmp;ans.cnt=ans.sqsum=ans.sum=;
F(i,,end){
if(i==)continue;
tmp=dfs(pos-,(mo+i)%,(now*+i)%,inf&&i==end),ans.cnt+=tmp.cnt,ans.cnt%=mod;
ans.sum+=(tmp.sum+((p[pos]*i)%mod)*tmp.cnt%mod)%mod,ans.sum%=mod;
ans.sqsum+=(tmp.sqsum+((*p[pos]*i)%mod)*tmp.sum)%mod,ans.sqsum%=mod;
ans.sqsum+=((tmp.cnt*p[pos])%mod*p[pos]%mod*i*i%mod),ans.sqsum%=mod;
}
if(!inf)dp[pos][mo][now]=ans;
return ans;
}
LL fuck(LL n){for(len=;n;n/=)bit[len++]=n%;return dfs(len-,,,).sqsum;}
int main(){
int T;p[]=;
F(i,,)p[i]=(p[i-]*)%mod;
F(i,,)F(j,,)F(k,,)dp[i][j][k].cnt=-;
scanf("%d",&T);
while(T--){
scanf("%I64d%I64d",&l,&r);
LL ans=fuck(r+);
ans-=fuck(l);
ans=(ans%mod+mod)%mod;
printf("%I64d\n",ans);
}
return ;
}