标签:
Description
Input
Output
Sample Input
Sample Output
#include <cstdio> #include <iostream> #include <cstring> #include <string> using namespace std; typedef long long LL; struct P{ LL num, sum, sqrt; P(LL a=-1, LL b=0, LL c=0):num(a),sum(b),sqrt(c){} }; const int MOD = (int)1e9+7; int a[20]; LL base[20]; P dp[20][7][7]; P dfs(int len, int mod1, int mod2, bool limit) { if(len < 1) return P(mod1 != 0 && mod2 != 0, 0, 0); if(!limit && dp[len][mod1][mod2].num!=-1) return dp[len][mod1][mod2]; int maxn = limit ? a[len] : 9; P ret(0,0,0); for(int i=0; i<=maxn; i++) { if(i==7) continue; P cur = dfs(len-1, (mod1+i%7)%7, (mod2*(10%7)+i)%7, limit&&i==maxn); ret.num = (ret.num + cur.num) % MOD; ret.sum = (ret.sum + cur.sum + i * base[len] % MOD * cur.num % MOD) % MOD; LL temp = i*base[len]%MOD; ret.sqrt = (ret.sqrt + cur.sqrt + cur.num*temp%MOD*temp%MOD + 2*temp%MOD*cur.sum%MOD)%MOD; } if(!limit) dp[len][mod1][mod2] = ret; return ret; } LL f(LL x) { int len = 0; while(x) { a[++len] = x%10; x /= 10; } return dfs(len, 0, 0, 1).sqrt; } void Init() { base[1]=1; for(int i=2; i<20; i++) { base[i] = base[i-1]*10; // printf("%lld\n", base[i]); // } } int main () { Init(); int T; LL l, r; scanf("%d", &T); while(T--) { scanf("%I64d%I64d", &l, &r); printf("%I64d\n", (f(r) - f(l-1) + MOD) % MOD); } return 0; }
标签:
原文地址:http://www.cnblogs.com/AcIsFun/p/5393358.html