1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #define mod(x, y) ((x) % (y) + (y)) % (y) using namespace std;
typedef long long ll; const int N = 25; const ll Mod = 1e9 + 7; struct Node { ll s0, s1, s2; inline void adjust() { s0 = mod(s0, Mod); s1 = mod(s1, Mod); s2 = mod(s2, Mod); } };
Node f[N][10][8][8]; ll p7[N], pm[N];
void merge(Node &v1, const Node &v2, int j, ll p) { v1.s0 = (v1.s0 + v2.s0) % Mod; v1.s1 = ((v1.s1 + v2.s1) % Mod + v2.s0 * p % Mod * (ll)j % Mod) % Mod; v1.s2 = (v1.s2 + v2.s2 +2ll * p % Mod * v2.s1 % Mod * (ll)j % Mod +((ll)j * p % Mod) * ((ll)j * p % Mod) % Mod * v2.s0 % Mod) % Mod; }
void init() { memset(f, 0, sizeof(f)); p7[0] = pm[0] = 1; for (int i = 1; i < N; ++ i) p7[i] = (p7[i - 1] * 10ll) % 7ll; for (int i = 1; i < N; ++ i) pm[i] = (pm[i - 1] * 10ll) % Mod; for (int i = 0; i <= 9; ++ i) { Node &v1 = f[1][i][i % 7][i % 7]; if (i == 7) continue; v1.s0 ++, v1.s1 += i, v1.s2 += i * i; } ll p = 10ll; for (int i = 2; i < N; ++ i, p *= 10ll, p %= Mod) for (int j = 0; j <= 9; ++ j) { if (j == 7) continue; for (int k = 0; k <= 9; ++ k) for (int x = 0; x < 7; ++ x) for (int y = 0; y < 7; ++ y) if (k != 7) merge(f[i][j][x][y], f[i - 1][k][mod(x - j, 7ll)][mod(y - p7[i - 1] * j, 7ll)], j, p); } }
Node calc(int i, int j, int x, int y) { ll s0 = 0, s1 = 0, s2 = 0; for (int k = 0; k < 7; ++ k) for (int l = 0; l < 7; ++ l) if (k != x && l != y) { Node &v = f[i][j][k][l]; s0 = (s0 + v.s0) % Mod; s1 = (s1 + v.s1) % Mod; s2 = (s2 + v.s2) % Mod; } return (Node){s0, s1, s2}; }
ll dp(ll n) { if (!n) return 0;
ll tmp = n;n %= Mod; vector <int> nums; while (tmp) nums.push_back(tmp % 10), tmp /= 10;
ll last0 = 0, last1 = 0, res = 0; for (int i = nums.size() - 1; i >= 0; -- i) { int &x = nums[i]; for (int j = 0; j < x; ++ j) { if (j == 7) continue; int a = mod(-last0, 7), b = mod(-(last1 % 7) * p7[i + 1], 7); Node v = calc(i + 1, j, a, b); res = (res + (last1 % Mod) * (last1 % Mod) % Mod * pm[i + 1] % Mod * pm[i + 1] % Mod * v.s0 % Mod + 2 * (last1 % Mod) % Mod * pm[i + 1] % Mod * v.s1 % Mod + v.s2) % Mod; }
if (x == 7) break;
last1 = last1 * 10 + x;last0 += x; if (!i && last1 % 7 && last0 % 7) res = (res + n * n % Mod) % Mod; } return res; }
signed main() { #ifndef ONLINE_JUDGE freopen("L10168.in", "r", stdin); freopen("myans.out", "w", stdout); #endif init();
ll t, l, r;cin >> t; while (t --) { cin >> l >> r; cout << mod(dp(r) - dp(l - 1), Mod) << endl; }
return 0; }
|