跳到正文

题解

题解:U675277 恨7不成妻

abc123_cn约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年4月5日
更新
2026年4月5日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1
C++
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
int digit[20]; ll dp[20][10][10];

ll dfs(int pos, int sum, int num, bool limit)
{
	if (pos == 0) return sum != 0 && num != 0;
	if (!limit && dp[pos][sum][num] != -1) return dp[pos][sum][num];
	ll res = 0; int up = limit ? digit[pos] : 9;
	for (int i = 0; i <= up; i ++)
	{
		if (i == 7) continue;
		res = (res + dfs(pos - 1, (sum + i) % 7, (num * 10 + i) % 7, limit && (i == up))) % MOD;
	}
	if (!limit) dp[pos][sum][num] = res;
	return res;
}

ll solve(ll x)
{
	int pos = 0;
	while (x)
	{
		digit[++ pos] = x % 10;
		x /= 10;
 	}
 	return dfs(pos, 0, 0, true);
}

int main()
{
	int t;
	cin >> t;
	memset(dp, -1, sizeof dp);
	while (t --)
	{
		ll l, r;
		cin >> l >> r;
		cout << solve(r) - solve(l - 1) << "\n";
	}
	return 0;
}
原文