AcWing 1086. 恨7不成妻(递推写法)
原题链接
困难
作者:
ytczy666
,
2020-07-26 20:54:45
,
所有人可见
,
阅读 615
这是一篇用递推方式写的数位DP,大家可以借鉴参考~
#include <bits/stdc++.h>
#define LL long long
const int N = 20;
const LL mod = 1000000007;
int T;
LL L, R, nums[N], cnt, f1[N][7][7][2][2], f2[N][7][7][2][2], f3[N][7][7][2][2];
LL Solve(LL n)
{
cnt = 0; memset(nums, 0, sizeof(nums));
memset(f1, 0, sizeof(f1)); memset(f2, 0, sizeof(f2)); memset(f3, 0, sizeof(f3));
while(n)
{
nums[++cnt] = n % 10;
n /= 10;
}
std:: reverse(nums + 1, nums + cnt + 1);
f1[0][0][0][0][1] = 1;
for(int i=0;i<cnt;i++)
{
for(int j=0;j<7;j++)
{
for(int k=0;k<7;k++)
{
for(int l=0;l<=1;l++)
{
for(int r=0;r<=1;r++)
{
if(f1[i][j][k][l][r] || f2[i][j][k][l][r] || f3[i][j][k][l][r])
{
int up = (r == 0) ? 9 : nums[i + 1];
for(int p=0;p<=up;p++)
{
f1[i + 1][(10 * j + p) % 7][(k + p) % 7][l || (p == 7)][r && (p == up)]
= (f1[i + 1][(10 * j + p) % 7][(k + p) % 7][l || (p == 7)][r && (p == up)] + f1[i][j][k][l][r]) % mod;
f2[i + 1][(10 * j + p) % 7][(k + p) % 7][l || (p == 7)][r && (p == up)]
= (f2[i + 1][(10 * j + p) % 7][(k + p) % 7][l || (p == 7)][r && (p == up)] + 10 * f2[i][j][k][l][r] + p * f1[i][j][k][l][r]) % mod;
f3[i + 1][(10 * j + p) % 7][(k + p) % 7][l || (p == 7)][r && (p == up)]
= (f3[i + 1][(10 * j + p) % 7][(k + p) % 7][l || (p == 7)][r && (p == up)] + 100 * f3[i][j][k][l][r] + 20 * p * f2[i][j][k][l][r] + p * p * f1[i][j][k][l][r]) % mod;
}
}
}
}
}
}
}
LL res = 0;
for(int j=1;j<7;j++)
for(int k=1;k<7;k++)
for(int r=0;r<=1;r++)
{
res = (res + f3[cnt][j][k][0][r]) % mod;
}
return res;
}
int main()
{
scanf("%d", &T);
while(T--)
{
scanf("%lld%lld", &L, &R);
printf("%lld\n", (Solve(R) - Solve(L - 1) + mod) % mod);
}
return 0;
}
%%%