标签:
// hdu 5564 // 考虑dp,令d(i,j,k)表示长度为i第i位为j余数为k的方案数 // 则d(1,j,j%7) = 1, 0<j<10 d(i+1,x,(k*10+x)%7)+=d(i,j,k) // 发现转移相同,所以我们用矩阵快速幂来计算即可。 // dp[k][j] 首位为j 余数为k的方案数 // 把dp写成一维 dp[k*10+j] #include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD = 1000000007; typedef vector<ll> vec; typedef vector<vec> mat; mat mul(mat &A, mat &B) { mat C(A.size(), vec(B[0].size())); for (int i = 0; i < A.size(); ++i) { for (int k = 0; k < B.size(); ++k) { for (int j = 0; j < B[0].size(); ++j) { C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD; } } } return C; } mat pow(mat A, int n) { mat B(A.size(), vec(A.size())); for (int i = 0; i < A.size(); ++i) B[i][i] = 1; while (n > 0) { if (n & 1) B = mul(B, A); A = mul(A, A); n >>= 1; } return B; } ll cal(int num, int n) { mat f(71, vec(71)); for (int i = 0; i < 7; ++i) { for (int j = 0; j <= 9; ++j) { for (int k = 0; k <= 9; ++k) { if (j + k == num) continue; int x = (i * 10 + k) % 7; f[x * 10 + k][i * 10 + j]++; } } } f[70][70] = 1; for (int i = 0; i <= 9; ++i) f[70][0 * 10 + i] = 1; mat v(71, vec(1)); for (int i = 1; i <= 9; ++i) { v[(i % 7) * 10 + i][0] = 1; } f = pow(f, n); v = mul(f, v); return v[70][0]; } int main() { int t; scanf("%d", &t); while (t--) { //l,r,k(1≤l≤r≤109,0≤k≤18) int l, r, k; scanf("%d%d%d", &l, &r, &k); ll ans = (cal(k, r) - cal(k, l - 1) + MOD) % MOD; printf("%lld\n", ans); } return 0; }
hdu5564--Clarke and digits(数位dp+矩阵快速幂)
标签:
原文地址:http://www.cnblogs.com/wenruo/p/5153790.html